forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.go
168 lines (143 loc) · 4.89 KB
/
book.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
package gopher
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/jimmykuu/wtforms"
"gopkg.in/mgo.v2/bson"
)
// URL: /books
// 图书列表
func booksHandler(handler *Handler) {
c := handler.DB.C(BOOKS)
var chineseBooks []Book
c.Find(bson.M{"language": "中文"}).All(&chineseBooks)
var englishBooks []Book
c.Find(bson.M{"language": "英文"}).All(&englishBooks)
handler.renderTemplate("book/index.html", BASE, map[string]interface{}{
"chineseBooks": chineseBooks,
"englishBooks": englishBooks,
"active": "books",
})
}
// URL: /book/{id}
// 显示图书详情
func showBookHandler(handler *Handler) {
bookId := mux.Vars(handler.Request)["id"]
if !bson.IsObjectIdHex(bookId) {
http.NotFound(handler.ResponseWriter, handler.Request)
return
}
c := handler.DB.C(BOOKS)
var book Book
c.Find(bson.M{"_id": bson.ObjectIdHex(bookId)}).One(&book)
handler.renderTemplate("book/show.html", BASE, map[string]interface{}{
"book": book,
"active": "books",
})
}
// URL: /admin/book/{id}/edit
// 编辑图书
func editBookHandler(handler *Handler) {
bookId := mux.Vars(handler.Request)["id"]
c := handler.DB.C(BOOKS)
var book Book
c.Find(bson.M{"_id": bson.ObjectIdHex(bookId)}).One(&book)
form := wtforms.NewForm(
wtforms.NewTextField("title", "书名", book.Title, wtforms.Required{}),
wtforms.NewTextField("cover", "封面", book.Cover, wtforms.Required{}),
wtforms.NewTextField("author", "作者", book.Author, wtforms.Required{}),
wtforms.NewTextField("translator", "译者", book.Translator),
wtforms.NewTextArea("introduction", "简介", book.Introduction),
wtforms.NewTextField("pages", "页数", strconv.Itoa(book.Pages), wtforms.Required{}),
wtforms.NewTextField("language", "语言", book.Language, wtforms.Required{}),
wtforms.NewTextField("publisher", "出版社", book.Publisher),
wtforms.NewTextField("publication_date", "出版年月日", book.PublicationDate),
wtforms.NewTextField("isbn", "ISBN", book.ISBN),
)
if handler.Request.Method == "POST" {
if form.Validate(handler.Request) {
pages, _ := strconv.Atoi(form.Value("pages"))
err := c.Update(bson.M{"_id": book.Id_}, bson.M{"$set": bson.M{
"title": form.Value("title"),
"cover": form.Value("cover"),
"author": form.Value("author"),
"translator": form.Value("translator"),
"introduction": form.Value("introduction"),
"pages": pages,
"language": form.Value("language"),
"publisher": form.Value("publisher"),
"publication_date": form.Value("publication_date"),
"isbn": form.Value("isbn"),
}})
if err != nil {
panic(err)
}
http.Redirect(handler.ResponseWriter, handler.Request, "/admin/books", http.StatusFound)
return
}
}
handler.renderTemplate("book/form.html", ADMIN, map[string]interface{}{
"book": book,
"form": form,
"isNew": false,
})
}
// URL: /book/{id}/delete
// 删除图书
func deleteBookHandler(handler *Handler) {
id := mux.Vars(handler.Request)["id"]
c := handler.DB.C(BOOKS)
c.RemoveId(bson.ObjectIdHex(id))
handler.ResponseWriter.Write([]byte("true"))
}
func listBooksHandler(handler *Handler) {
c := handler.DB.C(BOOKS)
var books []Book
c.Find(nil).All(&books)
handler.renderTemplate("book/list.html", ADMIN, map[string]interface{}{
"books": books,
})
}
func newBookHandler(handler *Handler) {
form := wtforms.NewForm(
wtforms.NewTextField("title", "书名", "", wtforms.Required{}),
wtforms.NewTextField("cover", "封面", "", wtforms.Required{}),
wtforms.NewTextField("author", "作者", "", wtforms.Required{}),
wtforms.NewTextField("translator", "译者", ""),
wtforms.NewTextArea("introduction", "简介", ""),
wtforms.NewTextField("pages", "页数", "", wtforms.Required{}),
wtforms.NewTextField("language", "语言", "", wtforms.Required{}),
wtforms.NewTextField("publisher", "出版社", ""),
wtforms.NewTextField("publication_date", "出版年月日", ""),
wtforms.NewTextField("isbn", "ISBN", ""),
)
if handler.Request.Method == "POST" {
if form.Validate(handler.Request) {
pages, _ := strconv.Atoi(form.Value("pages"))
c := handler.DB.C(BOOKS)
err := c.Insert(&Book{
Id_: bson.NewObjectId(),
Title: form.Value("title"),
Cover: form.Value("cover"),
Author: form.Value("author"),
Translator: form.Value("translator"),
Pages: pages,
Language: form.Value("language"),
Publisher: form.Value("publisher"),
PublicationDate: form.Value("publication_date"),
Introduction: form.Value("introduction"),
ISBN: form.Value("isbn"),
})
if err != nil {
panic(err)
}
http.Redirect(handler.ResponseWriter, handler.Request, "/admin/books", http.StatusFound)
return
}
}
handler.renderTemplate("book/form.html", ADMIN, map[string]interface{}{
"form": form,
"isNew": true,
})
}