加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
package_category.go 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
jimmykuu 提交于 2016-08-13 21:49 . 去掉 defer panic
package gopher
import (
"net/http"
"github.com/gorilla/mux"
"github.com/jimmykuu/wtforms"
"gopkg.in/mgo.v2/bson"
)
// URL: /admin/package_categories
// 列出所有的包分类
func adminListPackageCategoriesHandler(handler *Handler) {
var categories []PackageCategory
c := handler.DB.C(PACKAGE_CATEGORIES)
c.Find(nil).All(&categories)
handler.renderTemplate("admin/package_categories.html", ADMIN, map[string]interface{}{"categories": categories})
}
// URL: /admin/package_category/new
// 新建包分类
func adminNewPackageCategoryHandler(handler *Handler) {
form := wtforms.NewForm(
wtforms.NewTextField("id", "ID", "", wtforms.Required{}),
wtforms.NewTextField("name", "名称", "", wtforms.Required{}),
)
if handler.Request.Method == "POST" {
if !form.Validate(handler.Request) {
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{"form": form})
return
}
c := handler.DB.C(PACKAGE_CATEGORIES)
var category PackageCategory
err := c.Find(bson.M{"name": form.Value("name")}).One(&category)
if err == nil {
form.AddError("name", "该名称已经有了")
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{"form": form})
return
}
err = c.Insert(&PackageCategory{
Id_: bson.NewObjectId(),
Id: form.Value("id"),
Name: form.Value("name"),
})
if err != nil {
panic(err)
}
http.Redirect(handler.ResponseWriter, handler.Request, "/admin/package_category/new", http.StatusFound)
}
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{
"form": form,
"isNew": true,
})
}
// URL: /admin/package_category/{id}/edit
// 修改包分类
func adminEditPackageCategoryHandler(handler *Handler) {
id := mux.Vars(handler.Request)["id"]
c := handler.DB.C(PACKAGE_CATEGORIES)
var category PackageCategory
c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&category)
form := wtforms.NewForm(
wtforms.NewTextField("id", "ID", category.Id, wtforms.Required{}),
wtforms.NewTextField("name", "名称", category.Name, wtforms.Required{}),
)
if handler.Request.Method == "POST" {
if !form.Validate(handler.Request) {
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{"form": form})
return
}
c.Update(bson.M{"_id": bson.ObjectIdHex(id)}, bson.M{"$set": bson.M{
"id": form.Value("id"),
"name": form.Value("name"),
}})
http.Redirect(handler.ResponseWriter, handler.Request, "/admin/package_categories", http.StatusFound)
}
handler.renderTemplate("package_category/form.html", ADMIN, map[string]interface{}{
"form": form,
"isNew": false,
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化