fix
This commit is contained in:
@@ -210,7 +210,7 @@ func (s *ArticleApplicationServiceImpl) ListArticles(ctx context.Context, query
|
||||
}
|
||||
|
||||
// 3. 转换为响应对象
|
||||
items := responses.FromArticleEntities(articles)
|
||||
items := responses.FromArticleEntitiesToListItemList(articles)
|
||||
|
||||
response := &responses.ArticleListResponse{
|
||||
Total: total,
|
||||
|
||||
@@ -23,12 +23,29 @@ type ArticleInfoResponse struct {
|
||||
UpdatedAt time.Time `json:"updated_at" comment:"更新时间"`
|
||||
}
|
||||
|
||||
// ArticleListItemResponse 文章列表项响应(不包含content)
|
||||
type ArticleListItemResponse struct {
|
||||
ID string `json:"id" comment:"文章ID"`
|
||||
Title string `json:"title" 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:"文章列表"`
|
||||
Total int64 `json:"total" comment:"总数"`
|
||||
Page int `json:"page" comment:"页码"`
|
||||
Size int `json:"size" comment:"每页数量"`
|
||||
Items []ArticleListItemResponse `json:"items" comment:"文章列表"`
|
||||
}
|
||||
|
||||
// CategoryInfoResponse 分类信息响应
|
||||
@@ -118,6 +135,53 @@ func FromArticleEntity(article *entities.Article) *ArticleInfoResponse {
|
||||
return response
|
||||
}
|
||||
|
||||
// FromArticleEntityToListItem 从文章实体转换为列表项响应对象(不包含content)
|
||||
func FromArticleEntityToListItem(article *entities.Article) *ArticleListItemResponse {
|
||||
if article == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
response := &ArticleListItemResponse{
|
||||
ID: article.ID,
|
||||
Title: article.Title,
|
||||
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 {
|
||||
@@ -133,3 +197,19 @@ func FromArticleEntities(articles []*entities.Article) []ArticleInfoResponse {
|
||||
|
||||
return responses
|
||||
}
|
||||
|
||||
// FromArticleEntitiesToListItemList 从文章实体列表转换为列表项响应对象列表(不包含content)
|
||||
func FromArticleEntitiesToListItemList(articles []*entities.Article) []ArticleListItemResponse {
|
||||
if len(articles) == 0 {
|
||||
return []ArticleListItemResponse{}
|
||||
}
|
||||
|
||||
responses := make([]ArticleListItemResponse, len(articles))
|
||||
for i, article := range articles {
|
||||
if response := FromArticleEntityToListItem(article); response != nil {
|
||||
responses[i] = *response
|
||||
}
|
||||
}
|
||||
|
||||
return responses
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user