第一版完成
This commit is contained in:
227
internal/dao/news_methods.go
Normal file
227
internal/dao/news_methods.go
Normal file
@@ -0,0 +1,227 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"cms-api/internal/model"
|
||||
)
|
||||
|
||||
// GetById 根据ID获取新闻
|
||||
func (dao *NewsDao) GetById(ctx context.Context, id int) (*model.News, error) {
|
||||
var news *model.News
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Scan(&news)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return news, nil
|
||||
}
|
||||
|
||||
// GetBySlug 根据URL别名获取新闻
|
||||
func (dao *NewsDao) GetBySlug(ctx context.Context, slug string) (*model.News, error) {
|
||||
var news *model.News
|
||||
err := dao.Ctx(ctx).Where(dao.Columns().Slug, slug).Scan(&news)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return news, nil
|
||||
}
|
||||
|
||||
// IncrementViewCount 增加浏览次数
|
||||
func (dao *NewsDao) 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 *NewsDao) List(ctx context.Context, req *model.NewsListRequest) ([]*model.News, int, error) {
|
||||
var (
|
||||
newsList []*model.News
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加查询条件
|
||||
if req.Category != "" {
|
||||
db = db.Where(dao.Columns().Category, req.Category)
|
||||
}
|
||||
if req.Author != "" {
|
||||
db = db.Where(dao.Columns().Author, req.Author)
|
||||
}
|
||||
if req.Source != "" {
|
||||
db = db.Where(dao.Columns().Source, req.Source)
|
||||
}
|
||||
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+"%")
|
||||
}
|
||||
if req.StartDate != "" {
|
||||
db = db.Where(dao.Columns().CreatedAt+" >= ?", req.StartDate)
|
||||
}
|
||||
if req.EndDate != "" {
|
||||
db = db.Where(dao.Columns().CreatedAt+" <= ?", req.EndDate)
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
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(&newsList)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return newsList, total, nil
|
||||
}
|
||||
|
||||
// Create 创建新闻
|
||||
func (dao *NewsDao) Create(ctx context.Context, news *model.News) (int64, error) {
|
||||
result, err := dao.Ctx(ctx).Data(news).Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
id, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update 更新新闻
|
||||
func (dao *NewsDao) 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 *NewsDao) Delete(ctx context.Context, id int) error {
|
||||
_, err := dao.Ctx(ctx).Where(dao.Columns().Id, id).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
// GetCount 获取新闻总数
|
||||
func (dao *NewsDao) GetCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetPublishedCount 获取已发布新闻数
|
||||
func (dao *NewsDao) GetPublishedCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().IsPublished, 1).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetFeaturedCount 获取推荐新闻数
|
||||
func (dao *NewsDao) GetFeaturedCount(ctx context.Context) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().IsFeatured, 1).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetCountByCategory 根据分类获取新闻数
|
||||
func (dao *NewsDao) GetCountByCategory(ctx context.Context, category string) (int, error) {
|
||||
count, err := dao.Ctx(ctx).Where(dao.Columns().Category, category).Count()
|
||||
return count, err
|
||||
}
|
||||
|
||||
// GetFeatured 获取推荐新闻
|
||||
func (dao *NewsDao) GetFeatured(ctx context.Context, limit int) ([]*model.News, error) {
|
||||
var newsList []*model.News
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().IsPublished, 1).
|
||||
Where(dao.Columns().IsFeatured, 1).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&newsList)
|
||||
return newsList, err
|
||||
}
|
||||
|
||||
// GetLatest 获取最新新闻
|
||||
func (dao *NewsDao) GetLatest(ctx context.Context, limit int) ([]*model.News, error) {
|
||||
var newsList []*model.News
|
||||
err := dao.Ctx(ctx).
|
||||
Where(dao.Columns().IsPublished, 1).
|
||||
Order(dao.Columns().CreatedAt + " DESC").
|
||||
Limit(limit).
|
||||
Scan(&newsList)
|
||||
return newsList, err
|
||||
}
|
||||
|
||||
// GetByCategory 根据分类获取新闻
|
||||
func (dao *NewsDao) GetByCategory(ctx context.Context, category string, page, pageSize int) ([]*model.News, int, error) {
|
||||
var (
|
||||
newsList []*model.News
|
||||
total int
|
||||
db = dao.Ctx(ctx)
|
||||
)
|
||||
|
||||
// 添加分类条件
|
||||
db = db.Where(dao.Columns().Category, category).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(&newsList)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return newsList, total, nil
|
||||
}
|
||||
|
||||
// Search 搜索新闻
|
||||
func (dao *NewsDao) Search(ctx context.Context, keyword string, page, pageSize int) ([]*model.News, int, error) {
|
||||
var (
|
||||
newsList []*model.News
|
||||
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(&newsList)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return newsList, total, nil
|
||||
}
|
||||
Reference in New Issue
Block a user