forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
92 lines (75 loc) · 2.25 KB
/
admin.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
//后台管理
package gopher
import (
"net/http"
"time"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// URL: /admin
// 后台管理首页
func adminHandler(handler *Handler) {
now := time.Now()
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
today = today.Add(-time.Duration(Config.TimeZoneOffset) * time.Hour)
c := handler.DB.C(USERS)
newUserCount, err := c.Find(bson.M{"joinedat": bson.M{"$gt": today}}).Count()
if err != nil {
panic(err)
}
totalUserCount, err := c.Find(nil).Count()
if err != nil {
panic(err)
}
c = handler.DB.C(CONTENTS)
newTopicCount, err := c.Find(bson.M{"content.createdat": bson.M{"$gt": today}}).Count()
if err != nil {
panic(err)
}
c = handler.DB.C(COMMENTS)
newCommentCount, err := c.Find(bson.M{"createdat": bson.M{"$gt": today}}).Count()
if err != nil {
panic(err)
}
handler.renderTemplate("admin/index.html", ADMIN, map[string]interface{}{
"newUserCount": newUserCount,
"newTopicCount": newTopicCount,
"newCommentCount": newCommentCount,
"totalUserCount": totalUserCount,
})
}
// URL: /admin/users
// 列出所有用户
func adminListUsersHandler(handler *Handler) {
page, err := getPage(handler.Request)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
var users []User
c := handler.DB.C(USERS)
pagination := NewPagination(c.Find(nil).Sort("-joinedat"), "/admin/users", PerPage)
query, err := pagination.Page(page)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
q, ok := query.(*mgo.Query)
if !ok {
panic("query的类型不是 *mgo.Query")
}
err = q.All(&users)
if err != nil {
message(handler, "查询错误", "查询错误", "error")
}
handler.renderTemplate("admin/users.html", ADMIN, map[string]interface{}{"users": users, "pagination": pagination, "total": pagination.Count(), "page": page})
}
// URL: /admin/user/{userId}/activate
// 激活用户
func adminActivateUserHandler(handler *Handler) {
userId := mux.Vars(handler.Request)["userId"]
c := handler.DB.C(USERS)
c.Update(bson.M{"_id": bson.ObjectIdHex(userId)}, bson.M{"$set": bson.M{"isactive": true}})
http.Redirect(handler.ResponseWriter, handler.Request, "/admin/users", http.StatusFound)
}