码农笔录博客源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 line
1.8 KiB

6 年之前
package service
import (
"beeblog/models"
6 年之前
"beeblog/utils"
4 年之前
"fmt"
"github.com/beego/beego/v2/client/orm"
6 年之前
)
6 年之前
type LikeService struct {
}
func (this *LikeService) SaveLike(like *models.Like) (int64, error) {
6 年之前
return orm.NewOrm().Insert(like)
}
6 年之前
func (this *LikeService) DelLike(like *models.Like) (int64, error) {
6 年之前
return orm.NewOrm().QueryTable(models.Like{}).Filter("BlogId", like.BlogId).Filter("UserId", like.UserId).Delete()
6 年之前
}
6 年之前
func (this *LikeService) IsLike(bid int64, uid int64) (bool, error) {
6 年之前
totalCount, err := orm.NewOrm().QueryTable(&models.Like{}).Filter("BlogId", bid).Filter("UserId", uid).Count()
if err == nil {
6 年之前
fmt.Println(totalCount, "like count")
6 年之前
if totalCount > 0 {
return true, nil
} else {
return false, nil
}
6 年之前
} else {
6 年之前
fmt.Println(err)
6 年之前
return false, err
6 年之前
}
}
6 年之前
6 年之前
func (this *LikeService) MeLikes(num int, size int, uid int64) (*utils.Page, error) {
page, err := this.countLike(num, size, uid)
6 年之前
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
}
6 年之前
func (this *LikeService) countLike(num int, size int, uid int64) (*utils.Page, error) {
6 年之前
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
}
6 年之前
func (likeService LikeService) DelLikeByBlog(id int64) {
orm.NewOrm().QueryTable(models.Like{}).Filter("BlogId", id).Delete()
}