码农笔录博客源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 rivejä
2.2 KiB

6 vuotta sitten
package controllers
import (
"beeblog/models"
"beeblog/service"
4 vuotta sitten
beego "github.com/beego/beego/v2/server/web"
6 vuotta sitten
"strconv"
)
type CommentController struct {
beego.Controller
}
func (this *CommentController) Save() {
6 vuotta sitten
commentService := service.CommentService{}
blogService := service.BlogService{}
userService := service.UserService{}
6 vuotta sitten
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")
6 vuotta sitten
blog, err := blogService.ReadBlog(blogId)
6 vuotta sitten
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}
6 vuotta sitten
if err := commentService.ReadComment(parent); err == nil {
6 vuotta sitten
comm.BuserId = parent.CuserId
}
comm.Pid = pid
}
6 vuotta sitten
err = commentService.SaveComment(comm)
6 vuotta sitten
if err == nil {
this.Data["json"] = models.ReurnData("", comm)
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
4 vuotta sitten
userService.CountComments(uid.(int64), blogId)
6 vuotta sitten
return
}
func (this *CommentController) Del() {
6 vuotta sitten
commentService := service.CommentService{}
userService := service.UserService{}
6 vuotta sitten
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}
6 vuotta sitten
err := commentService.ReadComment(comm)
6 vuotta sitten
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
}
6 vuotta sitten
err = commentService.DelComment(id)
6 vuotta sitten
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
4 vuotta sitten
userService.CountComments(uid.(int64), id)
6 vuotta sitten
return
}