JM-WechatMini/service/system/article.go
2023-12-11 18:56:34 +08:00

50 lines
1.4 KiB
Go

package system
import (
"gorm.io/gorm"
"miniapp/global"
"miniapp/model/app"
"miniapp/model/common"
"miniapp/model/common/request"
)
type ArticleService struct{}
// GetArticleList 获取文章列表
func (a ArticleService) GetArticleList(p request.PageInfo) (list []common.Article, total int64, err error) {
err = global.GVA_DB.Scopes(Page(p.Page, p.PageSize)).Find(&list).Offset(-1).Limit(-1).Count(&total).Error
return
}
// CreateArticle 创建文章
func (a ArticleService) CreateArticle(article *common.Article) (err error) {
return global.GVA_DB.Create(&article).Error
}
// UpdateArticle 更新文章
func (a ArticleService) UpdateArticle(article *common.Article) (err error) {
return global.GVA_DB.Updates(&article).Error
}
// DeleteArticle 删除文章
func (a ArticleService) DeleteArticle(article *common.Article) (err error) {
return global.GVA_DB.Delete(&article).Error
}
// GetArticleById 根据id获取文章
func (a ArticleService) GetArticleById(id, userId string) (article *common.Article, err error) {
err = global.GVA_DB.Where("id = ?", id).First(&article).Error
err = global.GVA_DB.Table("articles").Where("id = ?", id).Update("reading_num", gorm.Expr("reading_num + ?", 1)).Error
if userId != "" {
// 判断是否收藏
var favorite app.Favorite
global.GVA_DB.Where("wz_id = ? AND user_id = ?", id, userId).First(&favorite)
if favorite.ID != 0 {
article.IsFavorite = 1
}
}
return
}