Você não pode selecionar mais de 25 tópicos
Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
56 linhas
1.4 KiB
56 linhas
1.4 KiB
package controllers
|
|
|
|
import (
|
|
"strconv"
|
|
"beeblog/service"
|
|
"github.com/astaxie/beego"
|
|
"beeblog/models"
|
|
)
|
|
|
|
type LikeController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
func (this *LikeController) Save() {
|
|
likeService := service.LikeService{}
|
|
userService := service.UserService{}
|
|
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)}
|
|
if _, err := likeService.SaveLike(like); err != nil {
|
|
this.Data["json"] = models.ReurnError(500, "保存失败")
|
|
}else{
|
|
this.Data["json"] = models.ReurnSuccess("")
|
|
}
|
|
this.ServeJSON()
|
|
userService.CountLike(uid.(int64),id)
|
|
return
|
|
}
|
|
|
|
func (this *LikeController) Delete() {
|
|
likeService := service.LikeService{}
|
|
userService := service.UserService{}
|
|
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)}
|
|
if _, err := likeService.DelLike(like); err != nil {
|
|
this.Data["json"] = models.ReurnError(500, "保存失败")
|
|
}else{
|
|
this.Data["json"] = models.ReurnSuccess("")
|
|
}
|
|
this.ServeJSON()
|
|
userService.CountLike(uid.(int64),id)
|
|
return
|
|
}
|
|
|