This commit is contained in:
2025-09-01 20:46:56 +08:00
parent 5d5372e359
commit 5c5c2abfcd
8 changed files with 1370 additions and 1187 deletions

View File

@@ -261,7 +261,8 @@ func (r *GormArticleRepository) ListArticles(ctx context.Context, query *repoQue
var articles []entities.Article
var total int64
dbQuery := r.db.WithContext(ctx).Model(&entities.Article{})
dbQuery := r.db.WithContext(ctx).Model(&entities.Article{}).
Select("id, title, summary, cover_image, category_id, status, is_featured, published_at, view_count, created_at, updated_at, scheduled_at")
// 应用筛选条件
if query.Status != "" {

View File

@@ -38,7 +38,7 @@ func NewArticleHandler(
// CreateArticle 创建文章
// @Summary 创建文章
// @Description 创建新的文章
// @Tags 文章管理
// @Tags 文章管理-管理端
// @Accept json
// @Produce json
// @Security Bearer
@@ -47,7 +47,7 @@ func NewArticleHandler(
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/articles [post]
// @Router /api/v1/admin/articles [post]
func (h *ArticleHandler) CreateArticle(c *gin.Context) {
var cmd commands.CreateArticleCommand
if err := h.validator.BindAndValidate(c, &cmd); err != nil {
@@ -72,14 +72,12 @@ func (h *ArticleHandler) CreateArticle(c *gin.Context) {
// GetArticleByID 获取文章详情
// @Summary 获取文章详情
// @Description 根据ID获取文章详情
// @Tags 文章管理
// @Tags 文章管理-用户端
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path string true "文章ID"
// @Success 200 {object} responses.ArticleInfoResponse "获取文章详情成功"
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 404 {object} map[string]interface{} "文章不存在"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/articles/{id} [get]
@@ -104,7 +102,7 @@ func (h *ArticleHandler) GetArticleByID(c *gin.Context) {
// ListArticles 获取文章列表
// @Summary 获取文章列表
// @Description 分页获取文章列表,支持多种筛选条件
// @Tags 文章管理
// @Tags 文章管理-用户端
// @Accept json
// @Produce json
// @Param page query int false "页码" default(1)
@@ -153,7 +151,7 @@ func (h *ArticleHandler) ListArticles(c *gin.Context) {
// UpdateArticle 更新文章
// @Summary 更新文章
// @Description 更新文章信息
// @Tags 文章管理
// @Tags 文章管理-管理端
// @Accept json
// @Produce json
// @Security Bearer
@@ -164,7 +162,7 @@ func (h *ArticleHandler) ListArticles(c *gin.Context) {
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 404 {object} map[string]interface{} "文章不存在"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/articles/{id} [put]
// @Router /api/v1/admin/articles/{id} [put]
func (h *ArticleHandler) UpdateArticle(c *gin.Context) {
var cmd commands.UpdateArticleCommand
cmd.ID = c.Param("id")
@@ -188,7 +186,7 @@ func (h *ArticleHandler) UpdateArticle(c *gin.Context) {
// DeleteArticle 删除文章
// @Summary 删除文章
// @Description 删除指定文章
// @Tags 文章管理
// @Tags 文章管理-管理端
// @Accept json
// @Produce json
// @Security Bearer
@@ -198,7 +196,7 @@ func (h *ArticleHandler) UpdateArticle(c *gin.Context) {
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 404 {object} map[string]interface{} "文章不存在"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/articles/{id} [delete]
// @Router /api/v1/admin/articles/{id} [delete]
func (h *ArticleHandler) DeleteArticle(c *gin.Context) {
var cmd commands.DeleteArticleCommand
if err := h.validator.ValidateParam(c, &cmd); err != nil {
@@ -217,7 +215,7 @@ func (h *ArticleHandler) DeleteArticle(c *gin.Context) {
// PublishArticle 发布文章
// @Summary 发布文章
// @Description 将草稿文章发布
// @Tags 文章管理
// @Tags 文章管理-管理端
// @Accept json
// @Produce json
// @Security Bearer
@@ -227,7 +225,7 @@ func (h *ArticleHandler) DeleteArticle(c *gin.Context) {
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 404 {object} map[string]interface{} "文章不存在"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/articles/{id}/publish [post]
// @Router /api/v1/admin/articles/{id}/publish [post]
func (h *ArticleHandler) PublishArticle(c *gin.Context) {
var cmd commands.PublishArticleCommand
if err := h.validator.ValidateParam(c, &cmd); err != nil {
@@ -246,7 +244,7 @@ func (h *ArticleHandler) PublishArticle(c *gin.Context) {
// SchedulePublishArticle 定时发布文章
// @Summary 定时发布文章
// @Description 设置文章的定时发布时间
// @Tags 文章管理
// @Tags 文章管理-管理端
// @Accept json
// @Produce json
// @Security Bearer
@@ -279,7 +277,7 @@ func (h *ArticleHandler) SchedulePublishArticle(c *gin.Context) {
// ArchiveArticle 归档文章
// @Summary 归档文章
// @Description 将已发布文章归档
// @Tags 文章管理
// @Tags 文章管理-管理端
// @Accept json
// @Produce json
// @Security Bearer
@@ -289,7 +287,7 @@ func (h *ArticleHandler) SchedulePublishArticle(c *gin.Context) {
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 404 {object} map[string]interface{} "文章不存在"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/articles/{id}/archive [post]
// @Router /api/v1/admin/articles/{id}/archive [post]
func (h *ArticleHandler) ArchiveArticle(c *gin.Context) {
var cmd commands.ArchiveArticleCommand
if err := h.validator.ValidateParam(c, &cmd); err != nil {
@@ -308,7 +306,7 @@ func (h *ArticleHandler) ArchiveArticle(c *gin.Context) {
// SetFeatured 设置推荐状态
// @Summary 设置推荐状态
// @Description 设置文章的推荐状态
// @Tags 文章管理
// @Tags 文章管理-管理端
// @Accept json
// @Produce json
// @Security Bearer
@@ -319,7 +317,7 @@ func (h *ArticleHandler) ArchiveArticle(c *gin.Context) {
// @Failure 401 {object} map[string]interface{} "未认证"
// @Failure 404 {object} map[string]interface{} "文章不存在"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /api/v1/articles/{id}/featured [put]
// @Router /api/v1/admin/articles/{id}/featured [put]
func (h *ArticleHandler) SetFeatured(c *gin.Context) {
var cmd commands.SetFeaturedCommand
if err := h.validator.ValidateParam(c, &cmd); err != nil {
@@ -341,7 +339,7 @@ func (h *ArticleHandler) SetFeatured(c *gin.Context) {
// GetArticleStats 获取文章统计
// @Summary 获取文章统计
// @Description 获取文章相关统计数据
// @Tags 文章管理
// @Tags 文章管理-管理端
// @Accept json
// @Produce json
// @Security Bearer
@@ -366,7 +364,7 @@ func (h *ArticleHandler) GetArticleStats(c *gin.Context) {
// ListCategories 获取分类列表
// @Summary 获取分类列表
// @Description 获取所有文章分类
// @Tags 文章分类
// @Tags 文章分类-用户端
// @Accept json
// @Produce json
// @Success 200 {object} responses.CategoryListResponse "获取分类列表成功"
@@ -386,7 +384,7 @@ func (h *ArticleHandler) ListCategories(c *gin.Context) {
// GetCategoryByID 获取分类详情
// @Summary 获取分类详情
// @Description 根据ID获取分类详情
// @Tags 文章分类
// @Tags 文章分类-用户端
// @Accept json
// @Produce json
// @Param id path string true "分类ID"
@@ -416,7 +414,7 @@ func (h *ArticleHandler) GetCategoryByID(c *gin.Context) {
// CreateCategory 创建分类
// @Summary 创建分类
// @Description 创建新的文章分类
// @Tags 文章分类管理
// @Tags 文章分类-管理
// @Accept json
// @Produce json
// @Security Bearer
@@ -444,7 +442,7 @@ func (h *ArticleHandler) CreateCategory(c *gin.Context) {
// UpdateCategory 更新分类
// @Summary 更新分类
// @Description 更新分类信息
// @Tags 文章分类管理
// @Tags 文章分类-管理
// @Accept json
// @Produce json
// @Security Bearer
@@ -479,7 +477,7 @@ func (h *ArticleHandler) UpdateCategory(c *gin.Context) {
// DeleteCategory 删除分类
// @Summary 删除分类
// @Description 删除指定分类
// @Tags 文章分类管理
// @Tags 文章分类-管理
// @Accept json
// @Produce json
// @Security Bearer
@@ -510,7 +508,7 @@ func (h *ArticleHandler) DeleteCategory(c *gin.Context) {
// ListTags 获取标签列表
// @Summary 获取标签列表
// @Description 获取所有文章标签
// @Tags 文章标签
// @Tags 文章标签-用户端
// @Accept json
// @Produce json
// @Success 200 {object} responses.TagListResponse "获取标签列表成功"
@@ -530,7 +528,7 @@ func (h *ArticleHandler) ListTags(c *gin.Context) {
// GetTagByID 获取标签详情
// @Summary 获取标签详情
// @Description 根据ID获取标签详情
// @Tags 文章标签
// @Tags 文章标签-用户端
// @Accept json
// @Produce json
// @Param id path string true "标签ID"
@@ -560,7 +558,7 @@ func (h *ArticleHandler) GetTagByID(c *gin.Context) {
// CreateTag 创建标签
// @Summary 创建标签
// @Description 创建新的文章标签
// @Tags 文章标签管理
// @Tags 文章标签-管理
// @Accept json
// @Produce json
// @Security Bearer
@@ -588,7 +586,7 @@ func (h *ArticleHandler) CreateTag(c *gin.Context) {
// UpdateTag 更新标签
// @Summary 更新标签
// @Description 更新标签信息
// @Tags 文章标签管理
// @Tags 文章标签-管理
// @Accept json
// @Produce json
// @Security Bearer
@@ -623,7 +621,7 @@ func (h *ArticleHandler) UpdateTag(c *gin.Context) {
// DeleteTag 删除标签
// @Summary 删除标签
// @Description 删除指定标签
// @Tags 文章标签管理
// @Tags 文章标签-管理
// @Accept json
// @Produce json
// @Security Bearer

View File

@@ -66,8 +66,8 @@ func (r *ArticleRoutes) Register(router *sharedhttp.GinRouter) {
adminArticleGroup.Use(r.admin.Handle())
{
// 统计信息
adminArticleGroup.GET("/stats", r.handler.GetArticleStats) // 获取文章统计
adminArticleGroup.GET("/stats", r.handler.GetArticleStats) // 获取文章统计
// 文章管理
adminArticleGroup.POST("", r.handler.CreateArticle) // 创建文章
adminArticleGroup.PUT("/:id", r.handler.UpdateArticle) // 更新文章