JM-WechatMini/service/system/article.go

39 lines
1.2 KiB
Go
Raw Normal View History

2023-11-02 04:34:46 +08:00
package system
import (
"gorm.io/gorm"
"miniapp/global"
"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 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
return
}