70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package dto
|
|
|
|
import (
|
|
"time"
|
|
|
|
"goyco/internal/database"
|
|
)
|
|
|
|
type PostDTO struct {
|
|
ID uint `json:"id"`
|
|
Title string `json:"title"`
|
|
URL string `json:"url"`
|
|
Content string `json:"content,omitempty"`
|
|
AuthorID *uint `json:"author_id,omitempty"`
|
|
AuthorName string `json:"author_name,omitempty"`
|
|
Author *UserDTO `json:"author,omitempty"`
|
|
UpVotes int `json:"up_votes"`
|
|
DownVotes int `json:"down_votes"`
|
|
Score int `json:"score"`
|
|
CurrentVote string `json:"current_vote,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
type PostListDTO struct {
|
|
Posts []PostDTO `json:"posts"`
|
|
Count int `json:"count"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
|
|
func ToPostDTO(post *database.Post) PostDTO {
|
|
if post == nil {
|
|
return PostDTO{}
|
|
}
|
|
|
|
dto := PostDTO{
|
|
ID: post.ID,
|
|
Title: post.Title,
|
|
URL: post.URL,
|
|
Content: post.Content,
|
|
AuthorID: post.AuthorID,
|
|
AuthorName: post.AuthorName,
|
|
UpVotes: post.UpVotes,
|
|
DownVotes: post.DownVotes,
|
|
Score: post.Score,
|
|
CreatedAt: post.CreatedAt,
|
|
UpdatedAt: post.UpdatedAt,
|
|
}
|
|
|
|
if post.CurrentVote != "" {
|
|
dto.CurrentVote = string(post.CurrentVote)
|
|
}
|
|
|
|
if post.Author.ID != 0 {
|
|
authorDTO := ToUserDTO(&post.Author)
|
|
dto.Author = &authorDTO
|
|
}
|
|
|
|
return dto
|
|
}
|
|
|
|
func ToPostDTOs(posts []database.Post) []PostDTO {
|
|
dtos := make([]PostDTO, len(posts))
|
|
for i := range posts {
|
|
dtos[i] = ToPostDTO(&posts[i])
|
|
}
|
|
return dtos
|
|
}
|