-
Notifications
You must be signed in to change notification settings - Fork 2
/
record.go
189 lines (155 loc) · 3.76 KB
/
record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"errors"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
)
var (
PermissionDenied = errors.New("你沒有權限進行此操作")
RecordNotFound = errors.New("record not found")
)
type TempType uint32
const (
UnknownType TempType = iota
Ear
Forehead
)
func (t TempType) String() string {
switch t {
case Ear:
return "耳溫"
case Forehead:
return "額溫"
}
return ""
}
func parseType(str string) (TempType, error) {
tempType, err := strconv.Atoi(str)
if err != nil || tempType < 0 || tempType > 3 {
return UnknownType, fmt.Errorf("Cannot parse '%s' as role", str)
}
return TempType(tempType), nil
}
type Record struct {
gorm.Model
Temperature float64
Type TempType
Reason string // If reason is not empty, exclude from stats
Account Account
AccountID string
RecordedBy Account `gorm:"foreignkey:RecorderID"`
RecorderID string
}
func (r Record) Fever() bool {
switch r.Type {
case Forehead:
return r.Temperature >= 37.5
case Ear:
return r.Temperature >= 38
}
return false
}
// insert a new record into database
func (h Handler) newRecord(w http.ResponseWriter, r *http.Request) {
var err error
var account Account
err = h.db.Preload("Class").First(&account, "id = ?", r.FormValue("account_id")).Error
if gorm.IsRecordNotFoundError(err) {
http.Error(w, AccountNotFound.Error(), 404)
return
}
acct, _ := session(r)
if !recordPermission(acct, account) {
http.Error(w, "permission denied", 403)
return
}
record := Record{
Account: account,
RecordedBy: acct,
Reason: r.FormValue("reason"),
}
record.Temperature, err = strconv.ParseFloat(r.FormValue("temperature"), 64)
if err != nil {
http.Error(w, err.Error(), 415)
return
}
record.Type, err = parseType(r.FormValue("type"))
if err != nil {
http.Error(w, err.Error(), 415)
return
}
err = h.db.Create(&record).Error
if err != nil {
panic(err)
}
if err = h.tpls["new.htm"].ExecuteTemplate(w, "row", record); err != nil {
http.Error(w, err.Error(), 500)
}
}
func (h Handler) listRecord(acct Account) (tx *gorm.DB) {
tx = h.db.Table("records").Order("id desc").Set("gorm:auto_preload", true)
tx = tx.Joins("JOIN accounts on records.account_id = accounts.id")
switch acct.Authority.Record {
case All:
return tx
case Group:
return tx.Where("accounts.class_id = ?", acct.ClassID)
case Self:
return tx.Where("account_id = ?", acct.ID)
}
return nil
}
func (h Handler) deleteRecord(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
http.Error(w, err.Error(), 415)
return
}
var record Record
err = h.db.Preload("Account").First(&record, id).Error
if gorm.IsRecordNotFoundError(err) {
http.Error(w, RecordNotFound.Error(), 404)
return
} else if err != nil {
panic(err)
}
acct := r.Context().Value(KeyAccount).(Account)
if acct.Authority.Record != All && acct.ID != record.RecorderID {
http.Error(w, PermissionDenied.Error(), 403)
return
}
err = h.db.Where("id = ?", id).Delete(&record).Error
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
func (h Handler) newSelfRecord(w http.ResponseWriter, r *http.Request) {
acct := r.Context().Value(KeyAccount).(Account)
record := Record{
Account: acct,
RecordedBy: acct,
Reason: r.FormValue("reason"),
}
var err error
record.Temperature, err = strconv.ParseFloat(r.FormValue("temperature"), 64)
if err != nil || record.Temperature > 41 || record.Temperature < 34 {
r = addMessage(r, "無效體溫資料")
h.index(w, r)
return
}
record.Type, err = parseType(r.FormValue("type"))
if err != nil {
r = addMessage(r, "無效體溫類別")
h.index(w, r)
return
}
err = h.db.Create(&record).Error
if err != nil {
panic(err)
}
h.index(w, r)
}