88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
package dto
|
||
|
||
import (
|
||
"fmt"
|
||
"vote-video-api/api/model"
|
||
)
|
||
|
||
// ArticleDto 文章表
|
||
type ArticleDto struct {
|
||
ArticleId string `json:"article_id"` // 主键id
|
||
ArticleTitle string `json:"article_title"` // 文章标题
|
||
ArticleStatus int `json:"article_status"` // 文章状态(1:正常 2:禁用)
|
||
ArticleNumber string `json:"article_number"` // 文章编号
|
||
VoteNum uint `json:"vote_num"` // 总票数
|
||
ArticleContent string `json:"article_content"` // 文章内容
|
||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间
|
||
ArticleAuthor []*ArticleAuthorDto `json:"article_author"` // 作者
|
||
Rank *int `json:"rank"` // 排名
|
||
IsVote bool `json:"is_vote"` // 是否已投票(false:否 true:是)
|
||
}
|
||
|
||
// GetArticleListDto 列表-分页
|
||
func GetArticleListDto(m []*model.Article) []*ArticleDto {
|
||
// 处理返回值
|
||
responses := make([]*ArticleDto, len(m))
|
||
|
||
if len(m) > 0 {
|
||
for i, v := range m {
|
||
response := &ArticleDto{
|
||
ArticleId: fmt.Sprintf("%d", v.ArticleId),
|
||
ArticleTitle: v.ArticleTitle,
|
||
ArticleStatus: v.ArticleStatus,
|
||
ArticleNumber: v.ArticleNumber,
|
||
VoteNum: v.VoteNum,
|
||
CreatedAt: v.CreatedAt,
|
||
UpdatedAt: v.UpdatedAt,
|
||
}
|
||
|
||
// 加载数据-作者
|
||
if v.ArticleAuthor != nil {
|
||
response = response.LoadArticleAuthor(v.ArticleAuthor)
|
||
}
|
||
|
||
// 将转换后的结构体添加到新切片中
|
||
responses[i] = response
|
||
}
|
||
}
|
||
|
||
return responses
|
||
}
|
||
|
||
// GetArticleDto 详情
|
||
func GetArticleDto(m *model.Article) *ArticleDto {
|
||
return &ArticleDto{
|
||
ArticleId: fmt.Sprintf("%d", m.ArticleId),
|
||
ArticleTitle: m.ArticleTitle,
|
||
ArticleStatus: m.ArticleStatus,
|
||
ArticleNumber: m.ArticleNumber,
|
||
VoteNum: m.VoteNum,
|
||
ArticleContent: m.ArticleContent,
|
||
CreatedAt: m.CreatedAt,
|
||
UpdatedAt: m.UpdatedAt,
|
||
}
|
||
}
|
||
|
||
// LoadArticleAuthor 加载数据-作者
|
||
func (r *ArticleDto) LoadArticleAuthor(m []*model.ArticleAuthor) *ArticleDto {
|
||
if len(m) > 0 {
|
||
r.ArticleAuthor = GetArticleAuthorListDto(m)
|
||
}
|
||
return r
|
||
}
|
||
|
||
// LoadRank 加载数据-排名
|
||
func (r *ArticleDto) LoadRank(m int) *ArticleDto {
|
||
if m > 0 {
|
||
r.Rank = &m
|
||
}
|
||
return r
|
||
}
|
||
|
||
// LoadVoteStatus 加载数据-投票状态
|
||
func (r *ArticleDto) LoadVoteStatus(m bool) *ArticleDto {
|
||
r.IsVote = m
|
||
return r
|
||
}
|