码农笔录博客源码
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.

57 rindas
1.4 KiB

pirms 6 gadiem
package controllers
import (
"beeblog/models"
pirms 4 gadiem
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
"strconv"
pirms 6 gadiem
)
type LikeController struct {
beego.Controller
}
func (this *LikeController) Save() {
pirms 6 gadiem
likeService := service.LikeService{}
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)
like := &models.Like{BlogId: id, UserId: uid.(int64)}
pirms 6 gadiem
if _, err := likeService.SaveLike(like); err != nil {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(500, "保存失败")
pirms 4 gadiem
} else {
pirms 6 gadiem
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
pirms 4 gadiem
userService.CountLike(uid.(int64), id)
pirms 6 gadiem
return
}
func (this *LikeController) Delete() {
pirms 6 gadiem
likeService := service.LikeService{}
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)
like := &models.Like{BlogId: id, UserId: uid.(int64)}
pirms 6 gadiem
if _, err := likeService.DelLike(like); err != nil {
pirms 6 gadiem
this.Data["json"] = models.ReurnError(500, "保存失败")
pirms 4 gadiem
} else {
pirms 6 gadiem
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
pirms 4 gadiem
userService.CountLike(uid.(int64), id)
pirms 6 gadiem
return
}