Commit 45a12773293033179815995579e4630f568219f4

Authored by Administrator
1 parent 715c6ab2
Exists in master

category function ok

controllers/category.go 0 → 100644
... ... @@ -0,0 +1,91 @@
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/astaxie/beego"
  5 + "go_blog/models"
  6 +)
  7 +
  8 +type CategoryController struct {
  9 + beego.Controller
  10 +}
  11 +
  12 +
  13 +func (this *CategoryController) Get() {
  14 + this.TplName = "category.html"
  15 + this.Data["IsCategory"] = true
  16 + this.Data["IsLogin"] = checkAccount(this.Ctx)
  17 +
  18 + op := this.Input().Get("op")
  19 + switch op {
  20 + case "add":
  21 + name := this.Input().Get("name")
  22 + if len(name) == 0 {
  23 + break
  24 + }
  25 + err := models.AddCategory(name)
  26 + if err != nil {
  27 + beego.Error(err)
  28 + }
  29 + this.Redirect("/category",302)
  30 + return
  31 + case "del":
  32 + id := this.Input().Get("id")
  33 + if len(id) == 0 {
  34 + break
  35 + }
  36 + err := models.DelCategory(id)
  37 + if err != nil {
  38 + beego.Error(err)
  39 + }
  40 + this.Redirect("/category",302)
  41 + return
  42 + }
  43 +
  44 + var err error
  45 + this.Data["Categories"], err = models.GetAllCategories()
  46 + if err != nil {
  47 + beego.Error(err)
  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 +
... ...
controllers/home.go
... ... @@ -13,6 +13,4 @@ func (this *HomeController) Get() {
13 13 this.TplName = "home.html"
14 14 this.Data["IsHome"] = true
15 15 this.Data["IsLogin"] = checkAccount(this.Ctx)
16   -
17   -
18 16 }
... ...
data/beeblog.db
No preview for this file type
models/category.go 0 → 100644
... ... @@ -0,0 +1,80 @@
  1 +package models
  2 +
  3 +import (
  4 + "github.com/astaxie/beego/orm"
  5 + "time"
  6 + "strconv"
  7 +)
  8 +
  9 +func AddCategory(name string) error {
  10 + o := orm.NewOrm()
  11 +
  12 + cate := &Category{
  13 + Title: name,
  14 + Created: time.Now(),
  15 + TopicTime: time.Now(),
  16 + }
  17 + qs := o.QueryTable("category")
  18 + //根据tile字段查找是否有name这一条目,有的话放到cate结构体
  19 + err := qs.Filter("title",name).One(cate)
  20 + //如果找不到,返回错误,找到了,返回nil
  21 + if err == nil {
  22 + return err
  23 + }
  24 +
  25 + _,err = o.Insert(cate)
  26 + if err != nil {
  27 + return err
  28 + }
  29 + return nil
  30 +}
  31 +
  32 +func DelCategory(id string) error {
  33 + cid,err := strconv.ParseInt(id,10,64)
  34 + if err != nil {
  35 + return err
  36 + }
  37 +
  38 + o := orm.NewOrm()
  39 + cate := &Category{Id:cid}
  40 + _,err = o.Delete(cate)
  41 + return nil
  42 +}
  43 +
  44 +func GetAllCategories() ([]*Category, error) {
  45 + o := orm.NewOrm()
  46 +
  47 + cates := make([]*Category,0)
  48 + qs := o.QueryTable("category")
  49 + _,err := qs.All(&cates)
  50 + return cates, err
  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 +
... ...
routers/router.go
... ... @@ -8,4 +8,5 @@ import (
8 8 func init() {
9 9 beego.Router("/", &controllers.HomeController{})
10 10 beego.Router("/login", &controllers.LoginController{})
  11 + beego.Router("/category", &controllers.CategoryController{})
11 12 }
... ...
views/category.html 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +{{template "header"}}
  2 + <title>分类 - 我的 beego 博客</title>
  3 + </head>
  4 +
  5 + <body>
  6 + <div class="navbar navbar-default navbar-fixed-top">
  7 + <div class="container">
  8 + {{template "navbar" .}}
  9 + </div>
  10 + </div>
  11 + <div class="container">
  12 + <h1>分类列表</h1>
  13 + <form class="form-inline" method="Get" action="/category">
  14 + <div class="form-group">
  15 + <input id="name" type="text" class="form-control" name="name" placeholder="Enter category name">
  16 + <input type="hidden" name="op" value="add">
  17 + </div>
  18 + <button type="submit" class="btn btn-default" onclick="return checkInput();">添加分类</button>
  19 + </form>
  20 + <script type="text/javascript">
  21 + function checkInput() {
  22 + var uname = document.getElementById("name");
  23 + if (uname.value.length ==0) {
  24 + alert("请输入分类名称");
  25 + return false;
  26 + }
  27 + }
  28 + </script>
  29 + <table class="table table-striped">
  30 + <thead>
  31 + <tr>
  32 + <th>编号</th>
  33 + <th>分类名称</th>
  34 + <th>文章数</th>
  35 + <th>操作</th>
  36 + </tr>
  37 + </thead>
  38 +
  39 + <tbody>
  40 + {{range .Categories}}
  41 + <tr>
  42 + <th>{{.Id}}</th>
  43 + <th>{{.Title}}</th>
  44 + <th>{{.TopicCount}}</th>
  45 + <th><a href="/category?op=del&id={{.Id}}">删除</a></th>
  46 + </tr>
  47 + {{end}}
  48 + </tbody>
  49 + </table>
  50 + </div>
  51 + <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script>
  52 + <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
  53 + </body>
  54 +</html>
... ...