yirenyishi
6 år sedan
11 ändrade filer med 153 tillägg och 24 borttagningar
@ -0,0 +1,52 @@ |
|||
package controllers |
|||
|
|||
import ( |
|||
"strconv" |
|||
"beeblog/service" |
|||
"github.com/astaxie/beego" |
|||
"beeblog/models" |
|||
) |
|||
|
|||
type LikeController struct { |
|||
beego.Controller |
|||
} |
|||
|
|||
func (this *LikeController) Save() { |
|||
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 := service.SaveLike(like); err != nil { |
|||
this.Data["json"] = models.ReurnError(500, "保存失败") |
|||
}else{ |
|||
this.Data["json"] = models.ReurnSuccess("") |
|||
} |
|||
this.ServeJSON() |
|||
service.CountLike(uid.(int64)) |
|||
return |
|||
} |
|||
|
|||
func (this *LikeController) Delete() { |
|||
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 := service.DelLike(like); err != nil { |
|||
this.Data["json"] = models.ReurnError(500, "保存失败") |
|||
}else{ |
|||
this.Data["json"] = models.ReurnSuccess("") |
|||
} |
|||
this.ServeJSON() |
|||
service.CountLike(uid.(int64)) |
|||
return |
|||
} |
Binary file not shown.
@ -0,0 +1,11 @@ |
|||
package routers |
|||
|
|||
import ( |
|||
"beeblog/controllers" |
|||
"github.com/astaxie/beego" |
|||
) |
|||
|
|||
func init() { |
|||
beego.Router("/like/:id([0-9]+)", &controllers.LikeController{}, "get:Save") |
|||
beego.Router("/unlike/:id([0-9]+)", &controllers.LikeController{}, "get:Delete") |
|||
} |
@ -0,0 +1,29 @@ |
|||
package service |
|||
|
|||
import ( |
|||
"beeblog/models" |
|||
"github.com/astaxie/beego/orm" |
|||
"fmt" |
|||
) |
|||
|
|||
func SaveLike(like *models.Like) (int64, error) { |
|||
return orm.NewOrm().Insert(like) |
|||
} |
|||
func DelLike(like *models.Like) (int64, error) { |
|||
return orm.NewOrm().QueryTable(models.Like{}).Filter("BlogId",like.BlogId).Filter("UserId", like.UserId).Delete() |
|||
} |
|||
|
|||
func IsLike(bid int64, uid int64) (bool, error) { |
|||
totalCount, err := orm.NewOrm().QueryTable(&models.Like{}).Filter("BlogId", bid).Filter("UserId", uid).Count() |
|||
if err == nil { |
|||
fmt.Println(totalCount,"like count") |
|||
if totalCount > 0 { |
|||
return true, nil |
|||
} else { |
|||
return false, nil |
|||
} |
|||
}else{ |
|||
fmt.Println(err) |
|||
return false,err |
|||
} |
|||
} |
Efter Bredd: | Höjd: | Storlek: 600 B |
Laddar…
Reference in new issue