add article

This commit is contained in:
2025-09-01 18:29:59 +08:00
parent 34ff6ce916
commit 5d5372e359
59 changed files with 45435 additions and 1535 deletions

View File

@@ -0,0 +1,47 @@
package commands
// CreateArticleCommand 创建文章命令
type CreateArticleCommand struct {
Title string `json:"title" binding:"required" comment:"文章标题"`
Content string `json:"content" binding:"required" comment:"文章内容"`
Summary string `json:"summary" comment:"文章摘要"`
CoverImage string `json:"cover_image" comment:"封面图片"`
CategoryID string `json:"category_id" comment:"分类ID"`
TagIDs []string `json:"tag_ids" comment:"标签ID列表"`
IsFeatured bool `json:"is_featured" comment:"是否推荐"`
}
// UpdateArticleCommand 更新文章命令
type UpdateArticleCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"文章ID"`
Title string `json:"title" comment:"文章标题"`
Content string `json:"content" comment:"文章内容"`
Summary string `json:"summary" comment:"文章摘要"`
CoverImage string `json:"cover_image" comment:"封面图片"`
CategoryID string `json:"category_id" comment:"分类ID"`
TagIDs []string `json:"tag_ids" comment:"标签ID列表"`
IsFeatured bool `json:"is_featured" comment:"是否推荐"`
}
// DeleteArticleCommand 删除文章命令
type DeleteArticleCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"文章ID"`
}
// PublishArticleCommand 发布文章命令
type PublishArticleCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"文章ID"`
}
// ArchiveArticleCommand 归档文章命令
type ArchiveArticleCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"文章ID"`
}
// SetFeaturedCommand 设置推荐状态命令
type SetFeaturedCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"文章ID"`
IsFeatured bool `json:"is_featured" binding:"required" comment:"是否推荐"`
}

View File

@@ -0,0 +1,19 @@
package commands
// CreateCategoryCommand 创建分类命令
type CreateCategoryCommand struct {
Name string `json:"name" binding:"required" validate:"required,min=1,max=50" message:"分类名称不能为空且长度在1-50之间"`
Description string `json:"description" validate:"max=200" message:"分类描述长度不能超过200"`
}
// UpdateCategoryCommand 更新分类命令
type UpdateCategoryCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"分类ID"`
Name string `json:"name" binding:"required" validate:"required,min=1,max=50" message:"分类名称不能为空且长度在1-50之间"`
Description string `json:"description" validate:"max=200" message:"分类描述长度不能超过200"`
}
// DeleteCategoryCommand 删除分类命令
type DeleteCategoryCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"分类ID"`
}

View File

@@ -0,0 +1,9 @@
package commands
import "time"
// SchedulePublishCommand 定时发布文章命令
type SchedulePublishCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"文章ID"`
ScheduledTime time.Time `json:"scheduled_time" binding:"required" comment:"定时发布时间"`
}

View File

@@ -0,0 +1,19 @@
package commands
// CreateTagCommand 创建标签命令
type CreateTagCommand struct {
Name string `json:"name" binding:"required" validate:"required,min=1,max=30" message:"标签名称不能为空且长度在1-30之间"`
Color string `json:"color" validate:"required,hexcolor" message:"标签颜色不能为空且必须是有效的十六进制颜色"`
}
// UpdateTagCommand 更新标签命令
type UpdateTagCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"标签ID"`
Name string `json:"name" binding:"required" validate:"required,min=1,max=30" message:"标签名称不能为空且长度在1-30之间"`
Color string `json:"color" validate:"required,hexcolor" message:"标签颜色不能为空且必须是有效的十六进制颜色"`
}
// DeleteTagCommand 删除标签命令
type DeleteTagCommand struct {
ID string `json:"-" uri:"id" binding:"required" comment:"标签ID"`
}

View File

@@ -0,0 +1,54 @@
package queries
import "tyapi-server/internal/domains/article/entities"
// ListArticleQuery 文章列表查询
type ListArticleQuery struct {
Page int `form:"page" binding:"min=1" comment:"页码"`
PageSize int `form:"page_size" binding:"min=1,max=100" comment:"每页数量"`
Status entities.ArticleStatus `form:"status" comment:"文章状态"`
CategoryID string `form:"category_id" comment:"分类ID"`
TagID string `form:"tag_id" comment:"标签ID"`
Title string `form:"title" comment:"标题关键词"`
Summary string `form:"summary" comment:"摘要关键词"`
IsFeatured *bool `form:"is_featured" comment:"是否推荐"`
OrderBy string `form:"order_by" comment:"排序字段"`
OrderDir string `form:"order_dir" comment:"排序方向"`
}
// SearchArticleQuery 文章搜索查询
type SearchArticleQuery struct {
Page int `form:"page" binding:"min=1" comment:"页码"`
PageSize int `form:"page_size" binding:"min=1,max=100" comment:"每页数量"`
Keyword string `form:"keyword" comment:"搜索关键词"`
CategoryID string `form:"category_id" comment:"分类ID"`
AuthorID string `form:"author_id" comment:"作者ID"`
Status entities.ArticleStatus `form:"status" comment:"文章状态"`
OrderBy string `form:"order_by" comment:"排序字段"`
OrderDir string `form:"order_dir" comment:"排序方向"`
}
// GetArticleQuery 获取文章详情查询
type GetArticleQuery struct {
ID string `uri:"id" binding:"required" comment:"文章ID"`
}
// GetArticlesByAuthorQuery 获取作者文章查询
type GetArticlesByAuthorQuery struct {
AuthorID string `uri:"author_id" binding:"required" comment:"作者ID"`
Page int `form:"page" binding:"min=1" comment:"页码"`
PageSize int `form:"page_size" binding:"min=1,max=100" comment:"每页数量"`
}
// GetArticlesByCategoryQuery 获取分类文章查询
type GetArticlesByCategoryQuery struct {
CategoryID string `uri:"category_id" binding:"required" comment:"分类ID"`
Page int `form:"page" binding:"min=1" comment:"页码"`
PageSize int `form:"page_size" binding:"min=1,max=100" comment:"每页数量"`
}
// GetFeaturedArticlesQuery 获取推荐文章查询
type GetFeaturedArticlesQuery struct {
Page int `form:"page" binding:"min=1" comment:"页码"`
PageSize int `form:"page_size" binding:"min=1,max=100" comment:"每页数量"`
}

View File

@@ -0,0 +1,6 @@
package queries
// GetCategoryQuery 获取分类详情查询
type GetCategoryQuery struct {
ID string `json:"-" uri:"id" binding:"required" comment:"分类ID"`
}

View File

@@ -0,0 +1,6 @@
package queries
// GetTagQuery 获取标签详情查询
type GetTagQuery struct {
ID string `json:"-" uri:"id" binding:"required" comment:"标签ID"`
}

View File

@@ -0,0 +1,135 @@
package responses
import (
"time"
"tyapi-server/internal/domains/article/entities"
)
// ArticleInfoResponse 文章详情响应
type ArticleInfoResponse struct {
ID string `json:"id" comment:"文章ID"`
Title string `json:"title" comment:"文章标题"`
Content string `json:"content" comment:"文章内容"`
Summary string `json:"summary" comment:"文章摘要"`
CoverImage string `json:"cover_image" comment:"封面图片"`
CategoryID string `json:"category_id" comment:"分类ID"`
Category *CategoryInfoResponse `json:"category,omitempty" comment:"分类信息"`
Status string `json:"status" comment:"文章状态"`
IsFeatured bool `json:"is_featured" comment:"是否推荐"`
PublishedAt *time.Time `json:"published_at" comment:"发布时间"`
ViewCount int `json:"view_count" comment:"阅读量"`
Tags []TagInfoResponse `json:"tags" comment:"标签列表"`
CreatedAt time.Time `json:"created_at" comment:"创建时间"`
UpdatedAt time.Time `json:"updated_at" comment:"更新时间"`
}
// ArticleListResponse 文章列表响应
type ArticleListResponse struct {
Total int64 `json:"total" comment:"总数"`
Page int `json:"page" comment:"页码"`
Size int `json:"size" comment:"每页数量"`
Items []ArticleInfoResponse `json:"items" comment:"文章列表"`
}
// CategoryInfoResponse 分类信息响应
type CategoryInfoResponse struct {
ID string `json:"id" comment:"分类ID"`
Name string `json:"name" comment:"分类名称"`
Description string `json:"description" comment:"分类描述"`
SortOrder int `json:"sort_order" comment:"排序"`
CreatedAt time.Time `json:"created_at" comment:"创建时间"`
}
// TagInfoResponse 标签信息响应
type TagInfoResponse struct {
ID string `json:"id" comment:"标签ID"`
Name string `json:"name" comment:"标签名称"`
Color string `json:"color" comment:"标签颜色"`
CreatedAt time.Time `json:"created_at" comment:"创建时间"`
}
// CategoryListResponse 分类列表响应
type CategoryListResponse struct {
Items []CategoryInfoResponse `json:"items" comment:"分类列表"`
Total int `json:"total" comment:"总数"`
}
// TagListResponse 标签列表响应
type TagListResponse struct {
Items []TagInfoResponse `json:"items" comment:"标签列表"`
Total int `json:"total" comment:"总数"`
}
// ArticleStatsResponse 文章统计响应
type ArticleStatsResponse struct {
TotalArticles int64 `json:"total_articles" comment:"文章总数"`
PublishedArticles int64 `json:"published_articles" comment:"已发布文章数"`
DraftArticles int64 `json:"draft_articles" comment:"草稿文章数"`
ArchivedArticles int64 `json:"archived_articles" comment:"归档文章数"`
TotalViews int64 `json:"total_views" comment:"总阅读量"`
}
// FromArticleEntity 从文章实体转换为响应对象
func FromArticleEntity(article *entities.Article) *ArticleInfoResponse {
if article == nil {
return nil
}
response := &ArticleInfoResponse{
ID: article.ID,
Title: article.Title,
Content: article.Content,
Summary: article.Summary,
CoverImage: article.CoverImage,
CategoryID: article.CategoryID,
Status: string(article.Status),
IsFeatured: article.IsFeatured,
PublishedAt: article.PublishedAt,
ViewCount: article.ViewCount,
CreatedAt: article.CreatedAt,
UpdatedAt: article.UpdatedAt,
}
// 转换分类信息
if article.Category != nil {
response.Category = &CategoryInfoResponse{
ID: article.Category.ID,
Name: article.Category.Name,
Description: article.Category.Description,
SortOrder: article.Category.SortOrder,
CreatedAt: article.Category.CreatedAt,
}
}
// 转换标签信息
if len(article.Tags) > 0 {
response.Tags = make([]TagInfoResponse, len(article.Tags))
for i, tag := range article.Tags {
response.Tags[i] = TagInfoResponse{
ID: tag.ID,
Name: tag.Name,
Color: tag.Color,
CreatedAt: tag.CreatedAt,
}
}
}
return response
}
// FromArticleEntities 从文章实体列表转换为响应对象列表
func FromArticleEntities(articles []*entities.Article) []ArticleInfoResponse {
if len(articles) == 0 {
return []ArticleInfoResponse{}
}
responses := make([]ArticleInfoResponse, len(articles))
for i, article := range articles {
if response := FromArticleEntity(article); response != nil {
responses[i] = *response
}
}
return responses
}