forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
84 lines (64 loc) · 1.95 KB
/
node.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
/*
节点
*/
package gopher
import (
"net/http"
"github.com/jimmykuu/wtforms"
"gopkg.in/mgo.v2/bson"
)
// URL: /nodes
// 列出所有节点及其详细信息
func nodesHandler(handler *Handler) {
var nodes []Node
c := handler.DB.C(NODES)
c.Find(nil).Sort("-topiccount").All(&nodes)
handler.renderTemplate("node/list.html", BASE, map[string]interface{}{"nodes": nodes})
}
// URL: /admin/node/new
// 新建节点
func adminNewNodeHandler(handler *Handler) {
defer dps.Persist()
form := wtforms.NewForm(
wtforms.NewTextField("id", "ID", "", &wtforms.Required{}),
wtforms.NewTextField("name", "名称", "", &wtforms.Required{}),
wtforms.NewTextArea("description", "描述", "", &wtforms.Required{}),
)
if handler.Request.Method == "POST" {
if form.Validate(handler.Request) {
c := handler.DB.C(NODES)
node := Node{}
err := c.Find(bson.M{"id": form.Value("id")}).One(&node)
if err == nil {
form.AddError("id", "该ID已经存在")
handler.renderTemplate("node/new.html", ADMIN, map[string]interface{}{"form": form})
return
}
err = c.Find(bson.M{"name": form.Value("name")}).One(&node)
if err == nil {
form.AddError("name", "该名称已经存在")
handler.renderTemplate("node/new.html", ADMIN, map[string]interface{}{"form": form})
return
}
Id_ := bson.NewObjectId()
err = c.Insert(&Node{
Id_: Id_,
Id: form.Value("id"),
Name: form.Value("name"),
Description: form.Value("description")})
if err != nil {
panic(err)
}
http.Redirect(handler.ResponseWriter, handler.Request, "/admin/node/new", http.StatusFound)
}
}
handler.renderTemplate("node/new.html", ADMIN, map[string]interface{}{"form": form})
}
// URL: /admin/nodes
// 列出所有的节点
func adminListNodesHandler(handler *Handler) {
var nodes []Node
c := handler.DB.C(NODES)
c.Find(nil).All(&nodes)
handler.renderTemplate("admin/nodes.html", ADMIN, map[string]interface{}{"nodes": nodes})
}