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

72 rindas
1.8 KiB

pirms 6 gadiem
package service
import (
"beeblog/models"
pirms 6 gadiem
"beeblog/utils"
pirms 4 gadiem
"fmt"
"github.com/beego/beego/v2/client/orm"
pirms 6 gadiem
)
pirms 6 gadiem
type LikeService struct {
}
func (this *LikeService) SaveLike(like *models.Like) (int64, error) {
pirms 6 gadiem
return orm.NewOrm().Insert(like)
}
pirms 6 gadiem
func (this *LikeService) DelLike(like *models.Like) (int64, error) {
pirms 6 gadiem
return orm.NewOrm().QueryTable(models.Like{}).Filter("BlogId", like.BlogId).Filter("UserId", like.UserId).Delete()
pirms 6 gadiem
}
pirms 6 gadiem
func (this *LikeService) IsLike(bid int64, uid int64) (bool, error) {
pirms 6 gadiem
totalCount, err := orm.NewOrm().QueryTable(&models.Like{}).Filter("BlogId", bid).Filter("UserId", uid).Count()
if err == nil {
pirms 6 gadiem
fmt.Println(totalCount, "like count")
pirms 6 gadiem
if totalCount > 0 {
return true, nil
} else {
return false, nil
}
pirms 6 gadiem
} else {
pirms 6 gadiem
fmt.Println(err)
pirms 6 gadiem
return false, err
pirms 6 gadiem
}
}
pirms 6 gadiem
pirms 6 gadiem
func (this *LikeService) MeLikes(num int, size int, uid int64) (*utils.Page, error) {
page, err := this.countLike(num, size, uid)
pirms 6 gadiem
if err != nil {
return nil, err
}
var likes []*models.Like
o := orm.NewOrm()
qs := o.QueryTable(&models.Like{})
qs = qs.Filter("UserId", uid)
qs = qs.Limit(size, (page.PageNo-1)*size)
if _, err = qs.All(&likes); err != nil {
return nil, err
}
if len(likes) > 0 {
for i := 0; i < len(likes); i++ {
blog := &models.Blog{Id: likes[i].BlogId}
if err := o.Read(blog); err == nil {
likes[i].Blog = blog
}
}
}
page.List = likes
return page, nil
}
pirms 6 gadiem
func (this *LikeService) countLike(num int, size int, uid int64) (*utils.Page, error) {
pirms 6 gadiem
o := orm.NewOrm()
qs := o.QueryTable(&models.Like{})
totalCount, err := qs.Filter("UserId", uid).Count()
if err != nil {
return nil, err
}
return utils.PageUtil(totalCount, num, size), nil
}
pirms 6 gadiem
func (likeService LikeService) DelLikeByBlog(id int64) {
orm.NewOrm().QueryTable(models.Like{}).Filter("BlogId", id).Delete()
}