码农笔录博客源码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

89 rindas
2.2 KiB

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