Commit 4ee3309d891775e4f68f96d1206b891d7bc21234

Authored by Administrator
1 parent cbe74f46
Exists in master

topic function ok

controllers/home.go
... ... @@ -2,6 +2,7 @@ package controllers
2 2  
3 3 import (
4 4 "github.com/astaxie/beego"
  5 + "go_blog/models"
5 6 )
6 7  
7 8 type HomeController struct {
... ... @@ -13,4 +14,10 @@ func (this *HomeController) Get() {
13 14 this.TplName = "home.html"
14 15 this.Data["IsHome"] = true
15 16 this.Data["IsLogin"] = checkAccount(this.Ctx)
  17 +
  18 + topics, err := models.GetAllTopics(true)
  19 + if err != nil {
  20 + beego.Error(err)
  21 + }
  22 + this.Data["Topics"] = topics
16 23 }
... ...
controllers/topic.go 0 → 100644
... ... @@ -0,0 +1,141 @@
  1 +package controllers
  2 +
  3 +import (
  4 + "go_blog/models"
  5 + "github.com/astaxie/beego"
  6 +)
  7 +
  8 +type TopicController struct {
  9 + beego.Controller
  10 +}
  11 +
  12 +func (this *TopicController) Get() {
  13 + this.TplName = "topic.html"
  14 + this.Data["IsTopic"] = true
  15 + this.Data["IsLogin"] = checkAccount(this.Ctx)
  16 +
  17 + topics, err := models.GetAllTopics(false)
  18 + if err != nil {
  19 + beego.Error(err)
  20 + }
  21 +
  22 + this.Data["Topics"] = topics
  23 +}
  24 +
  25 +func (this *TopicController) Post() {
  26 + if !checkAccount(this.Ctx) {
  27 + this.Redirect("/login",302)
  28 + return
  29 + }
  30 +
  31 + tid := this.Input().Get("tid")
  32 + title := this.Input().Get("title")
  33 + content := this.Input().Get("content")
  34 +
  35 + var err error
  36 + if len(tid) == 0 {
  37 + err = models.AddTopic(title,content)
  38 + } else {
  39 + err = models.ModifyTopic(tid,title,content)
  40 + }
  41 +
  42 + if err != nil {
  43 + beego.Error(err)
  44 + }
  45 +
  46 + this.Redirect("/topic",302)
  47 +}
  48 +
  49 +
  50 +//下面三个方法会匹配到自动路由
  51 +func (this *TopicController) Add() {
  52 + this.TplName = "topic_add.html"
  53 + this.Data["IsTopic"] = true
  54 + this.Data["IsLogin"] = checkAccount(this.Ctx)
  55 +
  56 + if !checkAccount(this.Ctx) {
  57 + this.Redirect("/login", 302)
  58 + return
  59 + }
  60 +}
  61 +
  62 +func (this *TopicController) View() {
  63 + this.TplName = "topic_view.html"
  64 + this.Data["IsTopic"] = true
  65 + this.Data["IsLogin"] = checkAccount(this.Ctx)
  66 +
  67 + params := this.Ctx.Input.Params()
  68 + topic, err := models.GetTopic(params["0"])
  69 + if err != nil {
  70 + beego.Error(err)
  71 + this.Redirect("/", 302)
  72 + return
  73 + }
  74 + this.Data["Topic"] = topic
  75 +}
  76 +
  77 +func (this *TopicController) Modify() {
  78 + this.TplName = "topic_modify.html"
  79 + this.Data["IsTopic"] = true
  80 + this.Data["IsLogin"] = checkAccount(this.Ctx)
  81 +
  82 + if !checkAccount(this.Ctx) {
  83 + this.Redirect("/login", 302)
  84 + return
  85 + }
  86 +
  87 + tid := this.Input().Get("tid")
  88 + topic, err := models.GetTopic(tid)
  89 + if err != nil {
  90 + beego.Error(err)
  91 + this.Redirect("/", 302)
  92 + return
  93 + }
  94 + this.Data["Topic"] = topic
  95 +}
  96 +
  97 +func (this *TopicController) Delete() {
  98 + if !checkAccount(this.Ctx) {
  99 + this.Redirect("/login", 302)
  100 + return
  101 + }
  102 +
  103 + err := models.DeleteTopic(this.Input().Get("tid"))
  104 + if err != nil {
  105 + beego.Error(err)
  106 + }
  107 +
  108 + this.Redirect("/topic", 302)
  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 +
... ...
data/beeblog.db
No preview for this file type
models/models.go
... ... @@ -34,7 +34,7 @@ type Topic struct {
34 34 Updated time.Time `orm:"index"`
35 35 Views int64 `orm:"index"`
36 36 Author string
37   - ReplayTime time.Time
  37 + ReplayTime time.Time `orm:"index"`
38 38 ReplayCount int64
39 39 ReplayLastUserId int64
40 40 }
... ...
models/topic.go 0 → 100644
... ... @@ -0,0 +1,90 @@
  1 +package models
  2 +
  3 +import (
  4 + "github.com/astaxie/beego/orm"
  5 + "time"
  6 + "strconv"
  7 +)
  8 +
  9 +func GetAllTopics(isDesc bool) (topics []*Topic, err error) {
  10 + o := orm.NewOrm()
  11 + topics = make([]*Topic,0)
  12 +
  13 + qs := o.QueryTable("topic")
  14 +
  15 + if isDesc {
  16 + _, err = qs.OrderBy("-created").All(&topics)
  17 + } else {
  18 + _, err = qs.All(&topics)
  19 + }
  20 +
  21 + return
  22 +}
  23 +
  24 +func AddTopic(title, content string) error {
  25 + o := orm.NewOrm()
  26 + topic := &Topic{
  27 + Title: title,
  28 + Content: content,
  29 + Created: time.Now(),
  30 + Updated: time.Now(),
  31 + ReplayTime: time.Now(),
  32 + }
  33 +
  34 + _, err := o.Insert(topic)
  35 + return err
  36 +}
  37 +
  38 +func GetTopic(tid string) (*Topic, error) {
  39 + tidNum, err := strconv.ParseInt(tid,10,64)
  40 + if err != nil {
  41 + return nil, err
  42 + }
  43 +
  44 + topic := new(Topic)
  45 +
  46 + o := orm.NewOrm()
  47 + qs := o.QueryTable("topic")
  48 + err = qs.Filter("id",tidNum).One(topic)
  49 + if err != nil {
  50 + return nil, err
  51 + }
  52 +
  53 + topic.Views++
  54 + _, err = o.Update(topic)
  55 + return topic, err
  56 +}
  57 +
  58 +func ModifyTopic(tid, title, content string) error {
  59 + tidNum, err := strconv.ParseInt(tid,10,64)
  60 + if err != nil {
  61 + return err
  62 + }
  63 +
  64 + topic := &Topic{Id:tidNum}
  65 +
  66 + o := orm.NewOrm()
  67 + //如果数据库里有这个文章的id,才操作
  68 + if o.Read(topic) == nil {
  69 + topic.Title = title
  70 + topic.Content = content
  71 + topic.Updated = time.Now()
  72 + o.Update(topic)
  73 + }
  74 +
  75 + return nil
  76 +}
  77 +
  78 +func DeleteTopic(tid string) error {
  79 + tidNum, err := strconv.ParseInt(tid,10,64)
  80 + if err != nil {
  81 + return err
  82 + }
  83 +
  84 + topic := &Topic{Id:tidNum}
  85 +
  86 + o := orm.NewOrm()
  87 + _, err = o.Delete(topic)
  88 + return err
  89 +}
  90 +
... ...
routers/router.go
... ... @@ -9,4 +9,9 @@ func init() {
9 9 beego.Router("/", &controllers.HomeController{})
10 10 beego.Router("/login", &controllers.LoginController{})
11 11 beego.Router("/category", &controllers.CategoryController{})
  12 + beego.Router("/topic", &controllers.TopicController{})
  13 + //此处的自动路由为更全面演示beego的功能
  14 + ///object/login 调用 ObjectController 中的 Login 方法
  15 + //https://beego.me/docs/mvc/controller/router.md
  16 + beego.AutoRouter(&controllers.TopicController{})
12 17 }
... ...
views/home.html
... ... @@ -9,13 +9,15 @@
9 9 </div>
10 10 </div>
11 11 <div class="container">
  12 + {{range .Topics}}
12 13 <div class="page-header">
13   - <h1>我的第一篇博客</h1>
14   - <h6 class="text-muted">文章发布于x年x月,共有100个浏览,100个评论</h6>
  14 + <h1><a href="/topic/view/{{.Id}}">{{.Title}}</a></h1>
  15 + <h6 class="text-muted">文章发表于 {{.Created}},共有 {{.Views}} 次浏览, {{.ReplayCount}} 个评论</h6>
15 16 <p>
16   - 大家好,这是我的第一篇博客
  17 + {{.Content}}
17 18 </p>
18 19 </div>
  20 + {{end}}
19 21 </div>
20 22 <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script>
21 23 <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
... ...
views/login.html
... ... @@ -3,7 +3,7 @@
3 3 </head>
4 4  
5 5 <body>
6   - <div class="container" style="width: 500px;">
  6 + <div class="container" style="width: 250px;">
7 7 <form class="form-horizontal" method="POST" action="/login">
8 8 <div class="form-group">
9 9 <label class="col-1g-4 control-label">请输入管理员账号:</label>
... ...
views/topic.html 0 → 100644
... ... @@ -0,0 +1,47 @@
  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 +
  12 + <div class="container">
  13 + <h1>文章列表</h1>
  14 + <a type="submit" class="btn btn-default" href="/topic/add">添加文章</a>
  15 + <table class="table table-striped">
  16 + <thead>
  17 + <tr>
  18 + <th>#</th>
  19 + <th>标题</th>
  20 + <th>更新时间</th>
  21 + <th>浏览数</th>
  22 + <th>回复数</th>
  23 + <th>操作</th>
  24 + </tr>
  25 + </thead>
  26 + <tbody>
  27 + {{range .Topics}}
  28 + <tr>
  29 + <th>{{.Id}}</th>
  30 + <th><a href="/topic/view/{{.Id}}">{{.Title}}</a></th>
  31 + <th>{{.Updated}}</th>
  32 + <th>{{.Views}}</th>
  33 + <th>{{.ReplayCount}}</th>
  34 + <th>
  35 + <a href="/topic/modify?tid={{.Id}}">修改</a>
  36 + <a href="/topic/delete?tid={{.Id}}">删除</a>
  37 + </th>
  38 + </tr>
  39 + {{end}}
  40 + </tbody>
  41 + </table>
  42 + </div>
  43 +
  44 + <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script>
  45 + <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
  46 + </body>
  47 +</html>
0 48 \ No newline at end of file
... ...
views/topic_add.html 0 → 100644
... ... @@ -0,0 +1,31 @@
  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 +
  12 + <div class="container">
  13 + <h1>添加文章</h1>
  14 + <form method="post" action="/topic">
  15 + <div class="form-group">
  16 + <label>文章标题:</label>
  17 + <input type="text" class="form-control" name="title">
  18 + </div>
  19 +
  20 + <div class="form-group">
  21 + <label>文章内容:</label>
  22 + <textarea name="content" cols="30" rows="10" class="form-control"></textarea>
  23 + </div>
  24 + <button type="submit" class="btn btn-default">添加文章</button>
  25 + </form>
  26 + </div>
  27 +
  28 + <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script>
  29 + <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
  30 + </body>
  31 +</html>
0 32 \ No newline at end of file
... ...
views/topic_modify.html 0 → 100644
... ... @@ -0,0 +1,32 @@
  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 +
  12 + <div class="container">
  13 + <h1>修改文章</h1>
  14 + <form method="post" action="/topic">
  15 + <input type="hidden" name="tid" value="{{.Topic.Id}}">
  16 + <div class="form-group">
  17 + <label>文章标题:</label>
  18 + <input type="text" class="form-control" name="title" value="{{.Topic.Title}}">
  19 + </div>
  20 +
  21 + <div class="form-group">
  22 + <label>文章内容:</label>
  23 + <textarea name="content" cols="30" rows="10" class="form-control">{{.Topic.Content}}</textarea>
  24 + </div>
  25 + <button type="submit" class="btn btn-default">修改文章</button>
  26 + </form>
  27 + </div>
  28 +
  29 + <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script>
  30 + <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
  31 + </body>
  32 +</html>
0 33 \ No newline at end of file
... ...
views/topic_view.html 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +{{template "header"}}
  2 + <title>{{.Topic.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 +
  12 + <div class="container">
  13 + {{with .Topic}}
  14 + <h1>{{.Title}}</h1>
  15 + {{.Content}}
  16 + {{end}}
  17 + </div>
  18 +
  19 + <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script>
  20 + <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
  21 + </body>
  22 +</html>
0 23 \ No newline at end of file
... ...