Commit df7bfe335065c7601270c5a140b5e8cfb1db863f

Authored by Administrator
1 parent 4ee3309d
Exists in master

reply function ok

controllers/reply.go 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +package controllers
  2 +
  3 +import (
  4 + "go_blog/models"
  5 + "github.com/astaxie/beego"
  6 +)
  7 +
  8 +type ReplyController struct {
  9 + beego.Controller
  10 +}
  11 +
  12 +//url/reply/add
  13 +func (this *ReplyController) Add() {
  14 + tid := this.Input().Get("tid")
  15 + nickname := this.Input().Get("nickname")
  16 + content := this.Input().Get("content")
  17 +
  18 + err := models.AddReply(tid, nickname, content)
  19 + if err != nil {
  20 + beego.Error(err)
  21 + }
  22 +
  23 + this.Redirect("/topic/view/"+tid,302)
  24 +}
  25 +
  26 +//url/reply/delete
  27 +func (this *ReplyController) Delete() {
  28 + if !checkAccount(this.Ctx) {
  29 + return
  30 + }
  31 +
  32 + tid := this.Input().Get("tid")
  33 + rid := this.Input().Get("rid")
  34 +
  35 + err := models.DeleteReply(rid)
  36 + if err != nil {
  37 + beego.Error(err)
  38 + }
  39 +
  40 + this.Redirect("/topic/view/"+tid, 302)
  41 +}
  42 +
... ...
controllers/topic.go
... ... @@ -31,12 +31,13 @@ func (this *TopicController) Post() {
31 31 tid := this.Input().Get("tid")
32 32 title := this.Input().Get("title")
33 33 content := this.Input().Get("content")
  34 + category := this.Input().Get("category")
34 35  
35 36 var err error
36 37 if len(tid) == 0 {
37   - err = models.AddTopic(title,content)
  38 + err = models.AddTopic(title,category,content)
38 39 } else {
39   - err = models.ModifyTopic(tid,title,content)
  40 + err = models.ModifyTopic(tid,title,category,content)
40 41 }
41 42  
42 43 if err != nil {
... ... @@ -64,14 +65,24 @@ func (this *TopicController) View() {
64 65 this.Data["IsTopic"] = true
65 66 this.Data["IsLogin"] = checkAccount(this.Ctx)
66 67  
67   - params := this.Ctx.Input.Params()
68   - topic, err := models.GetTopic(params["0"])
  68 + tid := this.Ctx.Input.Params()["0"]
  69 + topic, err := models.GetTopic(tid)
69 70 if err != nil {
70 71 beego.Error(err)
71 72 this.Redirect("/", 302)
72 73 return
73 74 }
74 75 this.Data["Topic"] = topic
  76 +
  77 + replies, err := models.GetAllReplies(tid)
  78 + if err != nil {
  79 + beego.Error(err)
  80 + return
  81 + }
  82 +
  83 + this.Data["Replies"] = replies
  84 + this.Data["IsLogin"] = checkAccount(this.Ctx)
  85 +
75 86 }
76 87  
77 88 func (this *TopicController) Modify() {
... ...
data/beeblog.db
No preview for this file type
models/models.go
... ... @@ -28,42 +28,36 @@ type Topic struct {
28 28 Id int64
29 29 Uid int64
30 30 Title string
  31 + Category string
31 32 Content string `orm:"size(5000)"`
32 33 Attachment string
33 34 Created time.Time `orm:"index"`
34 35 Updated time.Time `orm:"index"`
35 36 Views int64 `orm:"index"`
36 37 Author string
37   - ReplayTime time.Time `orm:"index"`
38   - ReplayCount int64
39   - ReplayLastUserId int64
  38 + ReplyTime time.Time `orm:"index"`
  39 + ReplyCount int64
  40 + ReplyLastUserId int64
40 41 }
41 42  
  43 +type Comment struct {
  44 + Id int64
  45 + Tid int64
  46 + Name string
  47 + Content string `orm:"size(1000)"`
  48 + Created time.Time `orm:"index"`
  49 +}
  50 +
  51 +
42 52 func RegisterDB() {
43 53 if !com.IsExist(_DB_NAME) {
44 54 os.MkdirAll(path.Dir(_DB_NAME),os.ModePerm)
45 55 os.Create(_DB_NAME)
46 56 }
47   - orm.RegisterModel(new(Category),new(Topic))
  57 + orm.RegisterModel(new(Category),new(Topic),new(Comment))
48 58 orm.RegisterDriver(_SQLITE3_DRIVER,orm.DRSqlite)
49 59  
50 60 //最后一个参数是最大连接数
51 61 orm.RegisterDataBase("default",_SQLITE3_DRIVER,_DB_NAME,10)
52 62 }
53 63  
54   -
55   -
56   -
57   -
58   -
59   -
60   -
61   -
62   -
63   -
64   -
65   -
66   -
67   -
68   -
69   -
... ...
models/reply.go 0 → 100644
... ... @@ -0,0 +1,55 @@
  1 +package models
  2 +
  3 +import (
  4 + "github.com/astaxie/beego/orm"
  5 +
  6 + "strconv"
  7 + "time"
  8 +)
  9 +
  10 +func AddReply(tid,nickname,content string) error {
  11 + tidNum, err := strconv.ParseInt(tid,10,64)
  12 + if err != nil {
  13 + return err
  14 + }
  15 +
  16 + reply := &Comment{
  17 + Tid: tidNum,
  18 + Name: nickname,
  19 + Content: content,
  20 + Created: time.Now(),
  21 + }
  22 +
  23 + o := orm.NewOrm()
  24 + _, err = o.Insert(reply)
  25 + return err
  26 +}
  27 +
  28 +func GetAllReplies(tid string) (replies []*Comment, err error) {
  29 + tidNum, err := strconv.ParseInt(tid,10,64)
  30 + if err != nil {
  31 + return nil,err
  32 + }
  33 +
  34 + replies = make([]*Comment,0)
  35 +
  36 + o := orm.NewOrm()
  37 + qs := o.QueryTable("comment")
  38 + _, err = qs.Filter("tid",tidNum).All(&replies)
  39 +
  40 + return replies,err
  41 +}
  42 +
  43 +func DeleteReply(rid string) error {
  44 + ridNum, err := strconv.ParseInt(rid,10,64)
  45 + if err != nil {
  46 + return err
  47 + }
  48 +
  49 + o := orm.NewOrm()
  50 +
  51 + reply := &Comment{Id: ridNum}
  52 + _, err = o.Delete(reply)
  53 + return err
  54 +}
  55 +
... ...
models/topic.go
... ... @@ -21,14 +21,15 @@ func GetAllTopics(isDesc bool) (topics []*Topic, err error) {
21 21 return
22 22 }
23 23  
24   -func AddTopic(title, content string) error {
  24 +func AddTopic(title, category, content string) error {
25 25 o := orm.NewOrm()
26 26 topic := &Topic{
27 27 Title: title,
  28 + Category: category,
28 29 Content: content,
29 30 Created: time.Now(),
30 31 Updated: time.Now(),
31   - ReplayTime: time.Now(),
  32 + ReplyTime: time.Now(),
32 33 }
33 34  
34 35 _, err := o.Insert(topic)
... ... @@ -55,7 +56,7 @@ func GetTopic(tid string) (*Topic, error) {
55 56 return topic, err
56 57 }
57 58  
58   -func ModifyTopic(tid, title, content string) error {
  59 +func ModifyTopic(tid, title, category, content string) error {
59 60 tidNum, err := strconv.ParseInt(tid,10,64)
60 61 if err != nil {
61 62 return err
... ... @@ -67,6 +68,7 @@ func ModifyTopic(tid, title, content string) error {
67 68 //如果数据库里有这个文章的id,才操作
68 69 if o.Read(topic) == nil {
69 70 topic.Title = title
  71 + topic.Category = category
70 72 topic.Content = content
71 73 topic.Updated = time.Now()
72 74 o.Update(topic)
... ...
routers/router.go
... ... @@ -6,7 +6,7 @@ import (
6 6 )
7 7  
8 8 func init() {
9   - beego.Router("/", &controllers.HomeController{})
  9 + beego.Router("/", &controllers.HomeController{})
10 10 beego.Router("/login", &controllers.LoginController{})
11 11 beego.Router("/category", &controllers.CategoryController{})
12 12 beego.Router("/topic", &controllers.TopicController{})
... ... @@ -14,4 +14,11 @@ func init() {
14 14 ///object/login 调用 ObjectController 中的 Login 方法
15 15 //https://beego.me/docs/mvc/controller/router.md
16 16 beego.AutoRouter(&controllers.TopicController{})
  17 +
  18 + beego.Router("/reply", &controllers.ReplyController{})
  19 + //当往/reply/add路径执行post方法,将执行Add函数
  20 + beego.Router("/reply/add", &controllers.ReplyController{},"post:Add")
  21 + //当往/reply/add路径执行post方法,将执行Delete函数
  22 + beego.Router("/reply/delete", &controllers.ReplyController{},"get:Delete")
  23 +
17 24 }
... ...
views/home.html
... ... @@ -12,7 +12,7 @@
12 12 {{range .Topics}}
13 13 <div class="page-header">
14 14 <h1><a href="/topic/view/{{.Id}}">{{.Title}}</a></h1>
15   - <h6 class="text-muted">文章发表于 {{.Created}},共有 {{.Views}} 次浏览, {{.ReplayCount}} 个评论</h6>
  15 + <h6 class="text-muted">文章发表于 {{.Created}},共有 {{.Views}} 次浏览, {{.ReplyCount}} 个评论</h6>
16 16 <p>
17 17 {{.Content}}
18 18 </p>
... ...
views/topic.html
... ... @@ -30,7 +30,7 @@
30 30 <th><a href="/topic/view/{{.Id}}">{{.Title}}</a></th>
31 31 <th>{{.Updated}}</th>
32 32 <th>{{.Views}}</th>
33   - <th>{{.ReplayCount}}</th>
  33 + <th>{{.ReplyCount}}</th>
34 34 <th>
35 35 <a href="/topic/modify?tid={{.Id}}">修改</a>
36 36 <a href="/topic/delete?tid={{.Id}}">删除</a>
... ...
views/topic_add.html
... ... @@ -18,6 +18,11 @@
18 18 </div>
19 19  
20 20 <div class="form-group">
  21 + <label>文章分类:</label>
  22 + <input type="text" class="form-control" name="category">
  23 + </div>
  24 +
  25 + <div class="form-group">
21 26 <label>文章内容:</label>
22 27 <textarea name="content" cols="30" rows="10" class="form-control"></textarea>
23 28 </div>
... ...
views/topic_modify.html
... ... @@ -19,6 +19,11 @@
19 19 </div>
20 20  
21 21 <div class="form-group">
  22 + <label>文章分类:</label>
  23 + <input type="text" class="form-control" name="category" value="{{.Topic.Category}}">
  24 + </div>
  25 +
  26 + <div class="form-group">
22 27 <label>文章内容:</label>
23 28 <textarea name="content" cols="30" rows="10" class="form-control">{{.Topic.Content}}</textarea>
24 29 </div>
... ...
views/topic_view.html
... ... @@ -16,6 +16,32 @@
16 16 {{end}}
17 17 </div>
18 18  
  19 + <hr />
  20 +
  21 + <div class="container">
  22 + {{$tid := .Topic.Id}}
  23 + {{$isLogin := .IsLogin}}
  24 + {{range .Replies}}
  25 + <h4>{{.Name}} <small>{{.Created}} {{if $isLogin}}<a href="/reply/delete?tid={{$tid}}&rid={{.Id}}">删除</a>{{end}}</small> </h4>
  26 + {{.Content}}
  27 + {{end}}
  28 +
  29 + <h4>本文回复</h4>
  30 + <form method="post" action="/reply/add">
  31 + <input type="hidden" name="tid" value="{{.Topic.Id}}">
  32 + <div class="form-group">
  33 + <label>显示昵称:</label>
  34 + <input type="text" class="form-control" name="nickname">
  35 + </div>
  36 +
  37 + <div class="form-group">
  38 + <label>内容:</label>
  39 + <textarea name="content" id="" cols="30" rows="10" class="form-control"></textarea>
  40 + </div>
  41 + <button class="btn btn-default" type="submit">提交回复</button>
  42 + </form>
  43 + </div>
  44 +
19 45 <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.3/jquery.min.js"></script>
20 46 <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
21 47 </body>
... ...