60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package mappers
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/author"
|
|
"RenewCMS/internal/infrastructure/persistence/models"
|
|
"encoding/json"
|
|
)
|
|
|
|
const emptyLinks = "[]"
|
|
|
|
func decodeLinks(raw string) []author.SocialLink {
|
|
links := []author.SocialLink{}
|
|
if raw == "" {
|
|
return links
|
|
}
|
|
if err := json.Unmarshal([]byte(raw), &links); err != nil || links == nil {
|
|
return []author.SocialLink{}
|
|
}
|
|
return links
|
|
}
|
|
|
|
func encodeLinks(links []author.SocialLink) string {
|
|
if len(links) == 0 {
|
|
return emptyLinks
|
|
}
|
|
encoded, err := json.Marshal(links)
|
|
if err != nil {
|
|
return emptyLinks
|
|
}
|
|
return string(encoded)
|
|
}
|
|
|
|
func AuthorToDomain(m models.Author, articleCount int) author.Author {
|
|
return author.Author{
|
|
ID: m.ID,
|
|
Name: m.Name,
|
|
Slug: m.Slug,
|
|
Bio: m.Bio,
|
|
AvatarID: m.AvatarID,
|
|
Website: m.Website,
|
|
SocialLinks: decodeLinks(m.SocialLinks),
|
|
UserID: m.UserID,
|
|
IsLinked: m.UserID != nil,
|
|
ArticleCount: articleCount,
|
|
}
|
|
}
|
|
|
|
func AuthorToModel(a author.Author) models.Author {
|
|
return models.Author{
|
|
ID: a.ID,
|
|
Name: a.Name,
|
|
Slug: a.Slug,
|
|
Bio: a.Bio,
|
|
AvatarID: a.AvatarID,
|
|
Website: a.Website,
|
|
SocialLinks: encodeLinks(a.SocialLinks),
|
|
UserID: a.UserID,
|
|
}
|
|
}
|