220 lines
7.5 KiB
Go
220 lines
7.5 KiB
Go
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:"发布时间"`
|
||
ScheduledAt *time.Time `json:"scheduled_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:"更新时间"`
|
||
}
|
||
|
||
// 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:"发布时间"`
|
||
ScheduledAt *time.Time `json:"scheduled_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 []ArticleListItemResponse `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,
|
||
ScheduledAt: article.ScheduledAt,
|
||
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
|
||
}
|
||
|
||
// 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,
|
||
ScheduledAt: article.ScheduledAt,
|
||
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
|
||
}
|
||
|
||
// 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
|
||
}
|