diff --git a/controllers/base.go b/controllers/base.go index 672a9aa..72f5ae8 100644 --- a/controllers/base.go +++ b/controllers/base.go @@ -3,9 +3,9 @@ package controllers import ( "errors" "github.com/astaxie/beego" + "github.com/satori/go.uuid" "liteblog/models" "liteblog/syserrors" - "github.com/satori/go.uuid" ) const SESSION_USER_KEY = "SESSION_USER_KEY" diff --git a/controllers/comment.go b/controllers/comment.go new file mode 100644 index 0000000..c2060ce --- /dev/null +++ b/controllers/comment.go @@ -0,0 +1,38 @@ +package controllers + +import ( + "liteblog/models" + "liteblog/syserrors" + "log" +) + +type CommentController struct { + BaseController +} + +// 新增评论 +// @router /new/:key [post] +func (c *CommentController) NewComment() { + log.Print("开始评论了....") + + c.MustLogin() + key := c.Ctx.Input.Param(":key") + content := c.GetMustString("content", "请输入评论内容") + k := c.UUID() + + comment := &models.Comment{ + Key: k, + NoteKey: key, + User: c.User, + UserId: int(c.User.ID), + Content: content, + } + + if err := models.SaveComment(comment); err != nil { + log.Print("保存失败了") + + c.Abort500(syserrors.NewError("保存失败", err)) + } + + c.JSONOkH("保存成功", nil) +} \ No newline at end of file diff --git a/controllers/index.go b/controllers/index.go index bb84198..c097823 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -46,7 +46,7 @@ func (c *IndexController) Get() { // 显示文章 // @router /details/:key [get] -func (c *IndexController) GetDetail() { +func (c *IndexController) GetDetail() { // 得到页面传过来的key key := c.Ctx.Input.Param(":key") // 到数据查询对应key的文章 @@ -56,10 +56,31 @@ func (c *IndexController) GetDetail() { c.Abort500(syserrors.NewError("文章不存在", err)) } + comments, err := models.QueryCommentByNoteKey(key) + if err != nil { + c.Abort500(syserrors.NewError("文章不存在", err)) + } + c.Data["note"] = note + c.Data["comments"] = comments c.TplName = "details.html" } + +// 显示文章评论 +// @router /comment/:key [get] +func (c *IndexController) GetComment() { + key := c.Ctx.Input.Param(":key") + note, err := models.QueryNoteByKey(key) + + if err != nil { + c.Abort500(syserrors.NewError("文章不存在", err)) + } + + c.Data["note"] = note + c.TplName = "comment.html" +} + //留言 // @router /message [get] func (c *IndexController) GetMessage() { diff --git a/models/comment.go b/models/comment.go new file mode 100644 index 0000000..d81de22 --- /dev/null +++ b/models/comment.go @@ -0,0 +1,19 @@ +package models + +type Comment struct { + Model + Key string `gorm:"unique_index; not null" json:"key"` + Content string `json:"content"` + UserId int `json:"user_id"` + User User `json:"user"` + NoteKey string `json:"note_key"` + Praise int `gorm:"default:0" json:"praise"` +} + +func QueryCommentByNoteKey(noteKey string) (comments []*Comment, err error) { + return comments, db.Preload("User").Where("`note_key` = ?", noteKey).Order("`updated_at` desc").Find(&comments).Error +} + +func SaveComment(comment *Comment) (err error) { + return db.Save(comment).Error +} \ No newline at end of file diff --git a/models/core.go b/models/core.go index b8810ad..12bbbb5 100644 --- a/models/core.go +++ b/models/core.go @@ -31,7 +31,7 @@ func init() { // 自动迁移模式 db.AutoMigrate(&User{}, &Note{}, - //&Model.UserAuthsModel{}, + &Comment{}, ) var count int diff --git a/models/note.go b/models/note.go index f9f42b1..92d5350 100644 --- a/models/note.go +++ b/models/note.go @@ -4,7 +4,6 @@ import ( "fmt" ) - type Note struct { Model Key string `gorm:"unique_index; not null"` // 文章唯一标识 diff --git a/routers/router.go b/routers/router.go index 7001168..c5a8b28 100644 --- a/routers/router.go +++ b/routers/router.go @@ -17,5 +17,9 @@ func init() { "note", beego.NSInclude(&controllers.NoteController{}), ), + beego.NewNamespace( + "comment", + beego.NSInclude(&controllers.CommentController{}), + ), ) } diff --git a/views/comment.html b/views/comment.html index 3ea17e5..a9ade87 100755 --- a/views/comment.html +++ b/views/comment.html @@ -12,26 +12,17 @@
拥有诗意的心态,才能拥有诗意的生活
-发布于:刚刚
-父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌,手把手教我走路、骑车,却会在该放手的时刻果断地放开让自己去大胆尝试,那个时候期望快快长大,能够做自己想做的事,不用受父亲的“控制”。父亲是智慧树,他无所不知、无所不晓,虽然你有十万个为什么,但是也难不倒他。
-