diff --git a/controllers/CommentController.go b/controllers/CommentController.go new file mode 100644 index 0000000..d59eba1 --- /dev/null +++ b/controllers/CommentController.go @@ -0,0 +1,83 @@ +package controllers + +import ( + "github.com/astaxie/beego" + "beeblog/models" + "beeblog/service" + "strconv" +) + +type CommentController struct { + beego.Controller +} + +func (this *CommentController) Save() { + uid := this.GetSession("userid") + if uid == nil { + this.Data["json"] = models.ReurnError(401, "") + this.ServeJSON() + return + } + blogId, berr := this.GetInt64("blog") + if blogId == 0 || berr != nil { + this.Data["json"] = models.ReurnError(403, "") + this.ServeJSON() + return + } + commVal := this.GetString("commval") + blog, err := service.ReadBlog(blogId) + if err != nil { + this.Data["json"] = models.ReurnError(403, "") + this.ServeJSON() + return + } + comm := &models.Comment{BlogId: blogId, CuserId: uid.(int64), BuserId: blog.UserId, ComVal: commVal} + if pid, _ := this.GetInt64("pid"); pid != 0 { + parent := &models.Comment{Id: pid} + if err := service.ReadComment(parent); err == nil { + comm.BuserId = parent.CuserId + } + comm.Pid = pid + } + err = service.SaveComment(comm) + if err == nil { + this.Data["json"] = models.ReurnData("", comm) + } else { + this.Data["json"] = models.ReurnError(500, "保存失败") + } + this.ServeJSON() + service.CountComments(uid.(int64)) + return +} + +func (this *CommentController) Del() { + uid := this.GetSession("userid") + if uid == nil { + this.Data["json"] = models.ReurnError(401, "") + this.ServeJSON() + return + } + idStr := this.Ctx.Input.Param(":id") + id, _ := strconv.ParseInt(idStr, 10, 64) + comm := &models.Comment{Id: id} + err := service.ReadComment(comm) + if err != nil { + this.Data["json"] = models.ReurnError(500, "") + this.ServeJSON() + return + } + if comm.CuserId != uid.(int64) { + this.Data["json"] = models.ReurnError(403, "") + this.ServeJSON() + return + } + err = service.DelComment(id) + if err == nil { + this.Data["json"] = models.ReurnSuccess("") + } else { + this.Data["json"] = models.ReurnError(500, "保存失败") + } + this.ServeJSON() + service.CountComments(uid.(int64)) + return +} diff --git a/models/Blog.go b/models/Blog.go index f467c86..beaae52 100644 --- a/models/Blog.go +++ b/models/Blog.go @@ -25,4 +25,5 @@ type Blog struct { HeadImg string `orm:"-"` CateName string `orm:"-"` Lables []*NLabel `orm:"-"` + Comms []*Comment `orm:"-"` } diff --git a/models/Comment.go b/models/Comment.go index c0fdda4..8cead8d 100644 --- a/models/Comment.go +++ b/models/Comment.go @@ -4,10 +4,14 @@ import "time" type Comment struct { Id int64 - CuserId int64 - BuserId int64 - BlogId int64 - Ctime time.Time `orm:"datetime"` - Pid int64 + CuserId int64 `orm:"default(0)"` + BuserId int64 `orm:"default(0)"` + BlogId int64 `orm:"default(0)"` + Ctime time.Time `orm:"auto_now_add;type(datetime)"` + Pid int64 `orm:"default(0)"` ComVal string `orm:"type(text)"` + + Childs []*Comment `orm:"-"` + CUser *User `orm:"-"` + BUser *User `orm:"-"` } diff --git a/models/DataInit.go b/models/DataInit.go index 5cdcef1..01e4b7b 100644 --- a/models/DataInit.go +++ b/models/DataInit.go @@ -20,8 +20,9 @@ func RegistDB() { // os.Create(_DB_NAME) //} //orm.RegisterModel(new(Attachment),new(Topic)) - orm.RegisterModel(new(Attachment),new(User),new(Blog),new(NLabel),new(Note),new(NoteColl),new(Category),new(Like)) //orm.RegisterDriver(_SQLITE3_DRIVER,orm.DRSqlite) //orm.RegisterDataBase("default",_SQLITE3_DRIVER,_DB_NAME,10) + + orm.RegisterModel(new(Attachment),new(User),new(Blog),new(NLabel),new(Note),new(NoteColl),new(Category),new(Like),new(Comment)) orm.RegisterDataBase("default", "mysql", "root:booszy@tcp(127.0.0.1:3306)/beeblog?charset=utf8&loc=Local", 30) } \ No newline at end of file diff --git a/models/User.go b/models/User.go index 3d9ac97..1730824 100644 --- a/models/User.go +++ b/models/User.go @@ -15,6 +15,7 @@ type User struct { QQ string HomeUrl string Sex int `orm:"default(1)"` + Status int `orm:"default(0)"` DescInfo string Ctime time.Time `orm:"auto_now_add;type(datetime)"` diff --git a/routers/CommentRouter.go b/routers/CommentRouter.go new file mode 100644 index 0000000..941be64 --- /dev/null +++ b/routers/CommentRouter.go @@ -0,0 +1,11 @@ +package routers + +import ( + "github.com/astaxie/beego" + "beeblog/controllers" +) + +func init() { + beego.Router("/comms/save", &controllers.CommentController{}, "post:Save") + beego.Router("/comms/del/:id([0-9]+)", &controllers.CommentController{}, "get:Del") +} \ No newline at end of file diff --git a/service/BlogService.go b/service/BlogService.go index 9818110..385065d 100644 --- a/service/BlogService.go +++ b/service/BlogService.go @@ -47,6 +47,15 @@ func TopBlogByUser(uid int64) ([]*models.Blog, error) { return blogs, nil } +func ReadBlog(id int64) (*models.Blog, error) { + o := orm.NewOrm() + blog := &models.Blog{Id: id} + if err := o.Read(blog);err != nil { + return nil, err + } + return blog, nil +} + func GetBlog(id int64) (*models.Blog, error) { o := orm.NewOrm() blog := &models.Blog{Id: id} @@ -65,6 +74,10 @@ func GetBlog(id int64) (*models.Blog, error) { if err == nil { blog.Lables = labels } + comms , berr:= FindCommentByBlog(id) + if berr == nil{ + blog.Comms = comms + } return blog, nil } diff --git a/service/CommentService.go b/service/CommentService.go new file mode 100644 index 0000000..7597dc0 --- /dev/null +++ b/service/CommentService.go @@ -0,0 +1,80 @@ +package service + +import ( + "beeblog/models" + "github.com/astaxie/beego/orm" + "fmt" +) + +func FindCommentByBlog(bid int64) ([]*models.Comment, error) { + var comms []*models.Comment + o := orm.NewOrm() + _, err := o.QueryTable(&models.Comment{}).Filter("Pid", 0).OrderBy("-Ctime").All(&comms) + if err != nil { + return nil, err + } + if len(comms) > 0 { + for i := 0; i < len(comms); i++ { + var childs []*models.Comment + _, childerrr := o.QueryTable(&models.Comment{}).Filter("Pid", comms[i].Id).OrderBy("-Ctime").All(&childs) + if childerrr == nil { + if len(childs) > 0{ + comms[i].Childs = childs + for j:=0 ; j +