63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package dto
|
|
|
|
import (
|
|
"fmt"
|
|
"hepa-calc-api/api/model"
|
|
)
|
|
|
|
type UserCollectionDto struct {
|
|
CollectionId string `json:"collection_id"` // 主键id
|
|
UserId string `json:"user_id"` // 用户id
|
|
QuestionId string `json:"question_id"` // 问题id
|
|
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
|
UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间
|
|
Question *QuestionDto `json:"question"` // 问题
|
|
}
|
|
|
|
// GetUserCollectionDto 用户收藏详情
|
|
func GetUserCollectionDto(m *model.UserCollection) *UserCollectionDto {
|
|
return &UserCollectionDto{
|
|
CollectionId: fmt.Sprintf("%d", m.CollectionId),
|
|
UserId: fmt.Sprintf("%d", m.UserId),
|
|
QuestionId: fmt.Sprintf("%d", m.QuestionId),
|
|
CreatedAt: m.CreatedAt,
|
|
UpdatedAt: m.UpdatedAt,
|
|
}
|
|
}
|
|
|
|
// GetUserCollectionListDto 列表
|
|
func GetUserCollectionListDto(m []*model.UserCollection) []*UserCollectionDto {
|
|
// 处理返回值
|
|
responses := make([]*UserCollectionDto, len(m))
|
|
|
|
if len(m) > 0 {
|
|
for i, v := range m {
|
|
response := &UserCollectionDto{
|
|
CollectionId: fmt.Sprintf("%d", v.CollectionId),
|
|
UserId: fmt.Sprintf("%d", v.UserId),
|
|
QuestionId: fmt.Sprintf("%d", v.QuestionId),
|
|
CreatedAt: v.CreatedAt,
|
|
UpdatedAt: v.UpdatedAt,
|
|
}
|
|
|
|
// 加载问题数据
|
|
if v.Question != nil {
|
|
response = response.LoadQuestion(v.Question)
|
|
}
|
|
|
|
// 将转换后的结构体添加到新切片中
|
|
responses[i] = response
|
|
}
|
|
}
|
|
|
|
return responses
|
|
}
|
|
|
|
// LoadQuestion 加载题目数据
|
|
func (r *UserCollectionDto) LoadQuestion(m *model.Question) *UserCollectionDto {
|
|
if m != nil {
|
|
r.Question = GetQuestionDto(m)
|
|
}
|
|
return r
|
|
}
|