276 lines
7.0 KiB
Go
276 lines
7.0 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"cms-api/internal/model"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
// GetById 根据ID获取文章
|
|
func (dao *ArticleDao) GetById(ctx context.Context, id int) (*model.Article, error) {
|
|
var article *model.Article
|
|
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&article)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return article, nil
|
|
}
|
|
|
|
// GetBySlug 根据URL别名获取文章
|
|
func (dao *ArticleDao) GetBySlug(ctx context.Context, slug string) (*model.Article, error) {
|
|
var article *model.Article
|
|
err := dao.Ctx(ctx).Where(dao.Columns().Slug, slug).Scan(&article)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return article, nil
|
|
}
|
|
|
|
// IncrementViewCount 增加浏览次数
|
|
func (dao *ArticleDao) IncrementViewCount(ctx context.Context, id int) error {
|
|
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Increment(dao.Columns().ViewCount, 1)
|
|
return err
|
|
}
|
|
|
|
// List 获取文章列表
|
|
func (dao *ArticleDao) List(ctx context.Context, req *model.ArticleListRequest) ([]*model.Article, int, error) {
|
|
var (
|
|
articles []*model.Article
|
|
total int
|
|
db = dao.Ctx(ctx)
|
|
)
|
|
|
|
// 添加查询条件
|
|
if req.CategoryId > 0 {
|
|
db = db.Where(dao.Columns().CategoryId, req.CategoryId)
|
|
}
|
|
if req.AuthorId > 0 {
|
|
db = db.Where(dao.Columns().AuthorId, req.AuthorId)
|
|
}
|
|
if req.IsPublished >= 0 {
|
|
db = db.Where(dao.Columns().IsPublished, req.IsPublished)
|
|
}
|
|
if req.IsFeatured >= 0 {
|
|
db = db.Where(dao.Columns().IsFeatured, req.IsFeatured)
|
|
}
|
|
if req.IsTop >= 0 {
|
|
db = db.Where(dao.Columns().IsTop, req.IsTop)
|
|
}
|
|
if req.Keyword != "" {
|
|
db = db.Where(dao.Columns().Title+" LIKE ? OR "+dao.Columns().Content+" LIKE ?", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
|
}
|
|
|
|
// 获取总数
|
|
count, err := db.Count()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total = count
|
|
|
|
// 分页查询
|
|
offset := (req.Page - 1) * req.PageSize
|
|
err = db.Order(dao.Columns().IsTop + " DESC, " + dao.Columns().CreatedAt + " DESC").
|
|
Limit(req.PageSize).
|
|
Offset(offset).
|
|
Scan(&articles)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return articles, total, nil
|
|
}
|
|
|
|
// Create 创建文章
|
|
func (dao *ArticleDao) Create(ctx context.Context, article *model.Article) (int64, error) {
|
|
result, err := dao.Ctx(ctx).Data(article).Insert()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
id, err := result.LastInsertId()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
// Update 更新文章
|
|
func (dao *ArticleDao) Update(ctx context.Context, id int, data interface{}) error {
|
|
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Data(data).Update()
|
|
return err
|
|
}
|
|
|
|
// Delete 删除文章
|
|
func (dao *ArticleDao) Delete(ctx context.Context, id int) error {
|
|
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
|
return err
|
|
}
|
|
|
|
// GetCount 获取文章总数
|
|
func (dao *ArticleDao) GetCount(ctx context.Context) (int, error) {
|
|
count, err := dao.Ctx(ctx).Count()
|
|
return count, err
|
|
}
|
|
|
|
// GetPublishedCount 获取已发布文章数
|
|
func (dao *ArticleDao) GetPublishedCount(ctx context.Context) (int, error) {
|
|
count, err := dao.Ctx(ctx).Where(dao.Columns().IsPublished, 1).Count()
|
|
return count, err
|
|
}
|
|
|
|
// GetFeaturedCount 获取推荐文章数
|
|
func (dao *ArticleDao) GetFeaturedCount(ctx context.Context) (int, error) {
|
|
count, err := dao.Ctx(ctx).Where(dao.Columns().IsFeatured, 1).Count()
|
|
return count, err
|
|
}
|
|
|
|
// GetFeatured 获取推荐文章
|
|
func (dao *ArticleDao) GetFeatured(ctx context.Context, limit int) ([]*model.Article, error) {
|
|
var articles []*model.Article
|
|
err := dao.Ctx(ctx).
|
|
Where(dao.Columns().IsPublished, 1).
|
|
Where(dao.Columns().IsFeatured, 1).
|
|
Order(dao.Columns().CreatedAt + " DESC").
|
|
Limit(limit).
|
|
Scan(&articles)
|
|
return articles, err
|
|
}
|
|
|
|
// GetLatest 获取最新文章
|
|
func (dao *ArticleDao) GetLatest(ctx context.Context, limit int) ([]*model.Article, error) {
|
|
var articles []*model.Article
|
|
err := dao.Ctx(ctx).
|
|
Where(dao.Columns().IsPublished, 1).
|
|
Order(dao.Columns().CreatedAt + " DESC").
|
|
Limit(limit).
|
|
Scan(&articles)
|
|
return articles, err
|
|
}
|
|
|
|
// Search 搜索文章
|
|
func (dao *ArticleDao) Search(ctx context.Context, keyword string, page, pageSize int) ([]*model.Article, int, error) {
|
|
var (
|
|
articles []*model.Article
|
|
total int
|
|
db = dao.Ctx(ctx)
|
|
)
|
|
|
|
// 添加搜索条件
|
|
if keyword != "" {
|
|
db = db.Where(dao.Columns().IsPublished, 1).
|
|
Where(dao.Columns().Title+" LIKE ? OR "+dao.Columns().Content+" LIKE ? OR "+dao.Columns().Summary+" LIKE ?",
|
|
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
|
|
}
|
|
|
|
// 获取总数
|
|
count, err := db.Count()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total = count
|
|
|
|
// 分页查询
|
|
offset := (page - 1) * pageSize
|
|
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
|
Limit(pageSize).
|
|
Offset(offset).
|
|
Scan(&articles)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return articles, total, nil
|
|
}
|
|
|
|
// GetByCategory 根据分类获取文章
|
|
func (dao *ArticleDao) GetByCategory(ctx context.Context, categoryId int, page, pageSize int) ([]*model.Article, int, error) {
|
|
var (
|
|
articles []*model.Article
|
|
total int
|
|
db = dao.Ctx(ctx)
|
|
)
|
|
|
|
// 添加分类条件
|
|
db = db.Where(dao.Columns().CategoryId, categoryId).Where(dao.Columns().IsPublished, 1)
|
|
|
|
// 获取总数
|
|
count, err := db.Count()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total = count
|
|
|
|
// 分页查询
|
|
offset := (page - 1) * pageSize
|
|
err = db.Order(dao.Columns().IsTop + " DESC, " + dao.Columns().CreatedAt + " DESC").
|
|
Limit(pageSize).
|
|
Offset(offset).
|
|
Scan(&articles)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return articles, total, nil
|
|
}
|
|
|
|
// GetByAuthor 根据作者获取文章
|
|
func (dao *ArticleDao) GetByAuthor(ctx context.Context, authorId int, page, pageSize int) ([]*model.Article, int, error) {
|
|
var (
|
|
articles []*model.Article
|
|
total int
|
|
db = dao.Ctx(ctx)
|
|
)
|
|
|
|
// 添加作者条件
|
|
db = db.Where(dao.Columns().AuthorId, authorId).Where(dao.Columns().IsPublished, 1)
|
|
|
|
// 获取总数
|
|
count, err := db.Count()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
total = count
|
|
|
|
// 分页查询
|
|
offset := (page - 1) * pageSize
|
|
err = db.Order(dao.Columns().CreatedAt + " DESC").
|
|
Limit(pageSize).
|
|
Offset(offset).
|
|
Scan(&articles)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return articles, total, nil
|
|
}
|
|
|
|
// GetTopArticles 获取置顶文章
|
|
func (dao *ArticleDao) GetTopArticles(ctx context.Context, limit int) ([]*model.Article, error) {
|
|
var articles []*model.Article
|
|
err := dao.Ctx(ctx).
|
|
Where(dao.Columns().IsPublished, 1).
|
|
Where(dao.Columns().IsTop, 1).
|
|
Order(dao.Columns().CreatedAt + " DESC").
|
|
Limit(limit).
|
|
Scan(&articles)
|
|
return articles, err
|
|
}
|
|
|
|
// BatchUpdateStatus 批量更新文章状态
|
|
func (dao *ArticleDao) BatchUpdateStatus(ctx context.Context, ids []int, isPublished int) error {
|
|
_, err := dao.Ctx(ctx).
|
|
Where(dao.Columns().Id+" IN (?)", ids).
|
|
Data(g.Map{
|
|
dao.Columns().IsPublished: isPublished,
|
|
dao.Columns().UpdatedAt: gdb.Raw("NOW()"),
|
|
}).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
// BatchDelete 批量删除文章
|
|
func (dao *ArticleDao) BatchDelete(ctx context.Context, ids []int) error {
|
|
_, err := dao.Ctx(ctx).Where(dao.Columns().Id+" IN (?)", ids).Delete()
|
|
return err
|
|
} |