初始化提交

This commit is contained in:
2024-08-28 18:31:18 +08:00
parent 9c1e56d5af
commit 70bac9077e
27 changed files with 1608 additions and 42 deletions
+84
View File
@@ -0,0 +1,84 @@
package dto
import (
"fmt"
"vote-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:禁用)
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,
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,
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
}
+42
View File
@@ -0,0 +1,42 @@
package dto
import (
"vote-api/api/model"
)
type ArticleAuthorDto struct {
AuthorName string `json:"author_name"` // 作者姓名
HospitalName string `json:"hospital_name"` // 作者所属医院
}
// GetArticleAuthorListDto 列表-分页
func GetArticleAuthorListDto(m []*model.ArticleAuthor) []*ArticleAuthorDto {
// 处理返回值
responses := make([]*ArticleAuthorDto, len(m))
if len(m) > 0 {
for i, v := range m {
response := &ArticleAuthorDto{
AuthorName: v.AuthorName,
}
// 加载数据-医院属性
if v.BaseHospital != nil {
response = response.LoadBaseHospitalAttr(v.BaseHospital)
}
// 将转换后的结构体添加到新切片中
responses[i] = response
}
}
return responses
}
// LoadBaseHospitalAttr 加载数据-医院属性
func (r *ArticleAuthorDto) LoadBaseHospitalAttr(m *model.BaseHospital) *ArticleAuthorDto {
if m != nil {
r.HospitalName = m.HospitalName
}
return r
}
+85
View File
@@ -0,0 +1,85 @@
package dto
import (
"fmt"
"vote-api/api/model"
)
// VideoDto 视频表
type VideoDto struct {
VideoId string `json:"video_id"` // 主键id
VideoTitle string `json:"video_title"` // 视频标题
VideoStatus int `json:"article_status"` // 视频状态(1:正常 2:禁用)
VoteNum uint `json:"vote_num"` // 总票数
VideoUrl string `json:"video_url"` // 视频地址
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间
VideoAuthor []*VideoAuthorDto `json:"video_author"` // 作者
Rank *int `json:"rank"` // 排名
IsVote bool `json:"is_vote"` // 是否已投票(false:否 true:是)
}
// GetVideoListDto 列表-分页
func GetVideoListDto(m []*model.Video) []*VideoDto {
// 处理返回值
responses := make([]*VideoDto, len(m))
if len(m) > 0 {
for i, v := range m {
response := &VideoDto{
VideoId: fmt.Sprintf("%d", v.VideoId),
VideoTitle: v.VideoTitle,
VideoStatus: v.VideoStatus,
VoteNum: v.VoteNum,
VideoUrl: v.VideoUrl,
CreatedAt: v.CreatedAt,
UpdatedAt: v.UpdatedAt,
}
// 加载数据-作者
if v.VideoAuthor != nil {
response = response.LoadVideoAuthor(v.VideoAuthor)
}
// 将转换后的结构体添加到新切片中
responses[i] = response
}
}
return responses
}
// GetVideoDto 详情
func GetVideoDto(m *model.Video) *VideoDto {
return &VideoDto{
VideoId: fmt.Sprintf("%d", m.VideoId),
VideoTitle: m.VideoTitle,
VideoStatus: m.VideoStatus,
VoteNum: m.VoteNum,
VideoUrl: m.VideoUrl,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
}
}
// LoadVideoAuthor 加载数据-作者
func (r *VideoDto) LoadVideoAuthor(m []*model.VideoAuthor) *VideoDto {
if len(m) > 0 {
r.VideoAuthor = GetVideoAuthorListDto(m)
}
return r
}
// LoadRank 加载数据-排名
func (r *VideoDto) LoadRank(m int) *VideoDto {
if m > 0 {
r.Rank = &m
}
return r
}
// LoadVoteStatus 加载数据-投票状态
func (r *VideoDto) LoadVoteStatus(m bool) *VideoDto {
r.IsVote = m
return r
}
+42
View File
@@ -0,0 +1,42 @@
package dto
import (
"vote-api/api/model"
)
type VideoAuthorDto struct {
AuthorName string `json:"author_name"` // 作者姓名
HospitalName string `json:"hospital_name"` // 作者所属医院
}
// GetVideoAuthorListDto 列表-分页
func GetVideoAuthorListDto(m []*model.VideoAuthor) []*VideoAuthorDto {
// 处理返回值
responses := make([]*VideoAuthorDto, len(m))
if len(m) > 0 {
for i, v := range m {
response := &VideoAuthorDto{
AuthorName: v.AuthorName,
}
// 加载数据-医院属性
if v.BaseHospital != nil {
response = response.LoadBaseHospitalAttr(v.BaseHospital)
}
// 将转换后的结构体添加到新切片中
responses[i] = response
}
}
return responses
}
// LoadBaseHospitalAttr 加载数据-医院属性
func (r *VideoAuthorDto) LoadBaseHospitalAttr(m *model.BaseHospital) *VideoAuthorDto {
if m != nil {
r.HospitalName = m.HospitalName
}
return r
}