2023-11-02 04:34:46 +08:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"miniapp/global"
|
2023-11-02 05:11:27 +08:00
|
|
|
"miniapp/model/app"
|
2023-11-02 04:34:46 +08:00
|
|
|
"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获取文章
|
2023-11-02 05:11:27 +08:00
|
|
|
func (a ArticleService) GetArticleById(id, userId string) (article *common.Article, err error) {
|
2023-11-02 04:34:46 +08:00
|
|
|
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
|
2023-11-02 05:11:27 +08:00
|
|
|
|
2023-12-11 18:56:34 +08:00
|
|
|
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
|
|
|
|
}
|
2023-11-02 05:11:27 +08:00
|
|
|
}
|
2023-12-11 18:56:34 +08:00
|
|
|
|
2023-11-02 04:34:46 +08:00
|
|
|
return
|
|
|
|
}
|