-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpages.go
184 lines (161 loc) · 4.21 KB
/
pages.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
package main
import (
"context"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
)
const (
PageLimit = 100
)
func (h Handler) index(w http.ResponseWriter, r *http.Request) {
acct, ok := session(r)
if ok {
records, _ := h.lastRecords(acct, 3)
h.HTML(w, r, "index.htm", records)
} else {
http.Redirect(w, r, "/login", 303)
}
}
// get the last record today of the account
func (h Handler) lastRecords(account Account, n int) (record []Record, err error) {
err = h.db.Set("gorm:auto_preload", true).Where("created_at > ?", today()).Order("id desc").Limit(n).Find(&record, "account_id = ?", account.ID).Error
if err != nil {
panic(err)
}
return
}
func (h Handler) newRecordPage(w http.ResponseWriter, r *http.Request) {
var records []Record
acct, ok := r.Context().Value(KeyAccount).(Account)
if ok {
err := h.listRecord(acct).Where("recorder_id = ?", acct.ID).Limit(100).Find(&records).Error
if err != nil {
http.Error(w, err.Error(), 500)
return
}
} else {
http.Error(w, "cannot read account from session", 500)
return
}
class := ""
if acct.Authority.Record == Group {
class = acct.Class.Name
}
page := struct {
Class string
PageLimit int
Records []Record
}{class, PageLimit, records}
h.HTML(w, r, "new.htm", page)
}
func (h Handler) listRecordsPage(w http.ResponseWriter, r *http.Request) {
records := make([]Record, 0, PageLimit)
p, err := strconv.Atoi(r.FormValue("page"))
if err != nil {
p = 1
}
// acct must have value
title := ""
acct, _ := session(r)
tx := h.listRecord(acct)
tx = joinClasses(tx)
if id := r.FormValue("account_id"); id != "" {
account, err := h.getAccount(id)
if err == nil {
title += account.Name + " "
tx = tx.Where("account_id = ?", id)
}
}
if class := r.FormValue("class"); class != "" {
tx = whereClass(tx, class)
title += class + "班 "
}
if number := r.FormValue("number"); number != "" {
tx = whereNumber(tx, number)
title += number + "號 "
}
date, err := time.ParseInLocation("2006-01-02", r.FormValue("date"), time.Local)
if err == nil {
tx = whereDate(tx, date)
title += date.Format("01/02 ")
}
err = tx.Offset((p - 1) * PageLimit).Limit(PageLimit).Find(&records).Error
if err != nil {
panic(err)
}
page := make(map[string]interface{})
page["Page"] = p
page["Title"] = title
page["PageLimit"] = PageLimit
page["Records"] = records
page["Count"] = pageCount(tx)
page["Class"] = r.FormValue("class")
page["Number"] = r.FormValue("number")
page["Date"] = r.FormValue("date")
h.HTML(w, r, "list.htm", page)
}
func pageCount(tx *gorm.DB) int {
var count int
tx.Count(&count)
if count%PageLimit == 0 {
count /= PageLimit
} else {
count /= PageLimit
count++
}
return count
}
func (h Handler) listAccountsPage(w http.ResponseWriter, r *http.Request) {
acct := r.Context().Value(KeyAccount).(Account)
var accounts []Account
p, err := strconv.Atoi(r.FormValue("page"))
if err != nil {
p = 1
}
title := ""
tx := h.listAccounts(acct)
if class := r.FormValue("class"); class != "" {
tx = whereClass(tx, class)
title += class + "班 "
}
if number := r.FormValue("number"); number != "" {
tx = whereNumber(tx, number)
title += number + "號 "
}
err = tx.Offset(100 * (p - 1)).Limit(100).Find(&accounts).Error
if err != nil {
panic(err)
}
page := make(map[string]interface{})
page["Page"] = p
page["Title"] = title
page["Accounts"] = accounts
page["Count"] = pageCount(tx)
h.HTML(w, r, "account_list.htm", page)
}
func addMessage(r *http.Request, msg string) *http.Request {
ctx := r.Context()
ctx = context.WithValue(ctx, KeyMessage, msg)
return r.WithContext(ctx)
}
func (h Handler) registerPage(w http.ResponseWriter, r *http.Request) {
page := make(map[string]interface{})
page["authorities"] = Authorities
h.HTML(w, r, "register.htm", page)
}
func (h Handler) profile(w http.ResponseWriter, r *http.Request) {
acct, _ := session(r)
account, err := h.getAccount(mux.Vars(r)["id"])
if err == AccountNotFound {
h.message(w, r, "此帳號不存在", "")
return
}
if !accountPermission(acct, account) && !recordPermission(acct, account) {
h.message(w, r, "權限不足", "你沒有權限查看此頁面")
return
}
h.HTML(w, r, "profile.htm", account)
}