123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package mapper
- import (
- "database/sql"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/dto"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helpers/datetime"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
- )
- func ToArticlesList(lang i18n.Language, m []models.Article) []dto.Article {
- res := make([]dto.Article, 0, len(m))
- for i := range m {
- res = append(res, dto.Article{
- ID: m[i].ID,
- URL: m[i].URL,
- Title: m[i].Title,
- Text: m[i].Text,
- Image: m[i].Image,
- MetaKeywords: m[i].MetaKeywords.String,
- MetaDescription: m[i].MetaDescription.String,
- PublishTime: datetime.FormatDateStr(lang, m[i].PublishTime),
- Language: i18n.LanguageFromID(m[i].Language),
- })
- }
- return res
- }
- func ToArticle(lang i18n.Language, m models.Article) *dto.Article {
- return &dto.Article{
- ID: m.ID,
- URL: m.URL,
- Title: m.Title,
- Text: m.Text,
- Image: m.Image,
- MetaKeywords: m.MetaKeywords.String,
- MetaDescription: m.MetaDescription.String,
- PublishTime: datetime.FormatDateStr(lang, m.PublishTime),
- Language: i18n.LanguageFromID(m.Language),
- }
- }
- func ToArticlesPreview(lang i18n.Language, m []models.ArticlePreview) []dto.ArticlePreview {
- res := make([]dto.ArticlePreview, 0, len(m))
- for i := range m {
- res = append(res, dto.ArticlePreview{
- ID: m[i].ID,
- URL: m[i].URL,
- Title: m[i].Title,
- PreviewText: m[i].PreviewText.String,
- Image: m[i].Image,
- PublishTime: datetime.FormatDateStr(lang, m[i].PublishTime),
- })
- }
- return res
- }
- func ToArticleForm(a models.Article, tags []models.Tag) *dto.ArticleForm {
- tagMap := make(map[uint64]bool, len(tags))
- for i := range tags {
- tagMap[tags[i].ID] = true
- }
- return &dto.ArticleForm{
- ID: a.ID,
- Title: a.Title,
- Image: a.Image,
- URL: a.URL,
- IsActive: a.IsActive,
- Text: a.Text,
- PreviewText: a.PreviewText.String,
- MetaKeywords: a.MetaKeywords.String,
- MetaDescription: a.MetaDescription.String,
- PublishTime: datetime.FormatDateForm(a.PublishTime),
- ActiveTags: tagMap,
- }
- }
- func ToArticleModel(f dto.ArticleForm) (*models.Article, error) {
- var previewText, metaKeywords, metaDesc sql.NullString
- if f.PreviewText != "" {
- previewText = sql.NullString{
- String: f.PreviewText,
- Valid: true,
- }
- }
- if f.MetaKeywords != "" {
- metaKeywords = sql.NullString{
- String: f.MetaKeywords,
- Valid: true,
- }
- }
- if f.MetaDescription != "" {
- metaDesc = sql.NullString{
- String: f.MetaDescription,
- Valid: true,
- }
- }
- parsedDateTime, err := datetime.ParseDateTime(f.PublishTime)
- if err != nil {
- return nil, err
- }
- return &models.Article{
- ID: f.ID,
- URL: f.URL,
- Title: f.Title,
- PublishTime: parsedDateTime,
- Text: f.Text,
- IsActive: f.IsActive,
- Image: f.Image,
- PreviewText: previewText,
- MetaKeywords: metaKeywords,
- MetaDescription: metaDesc,
- Language: i18n.LanguageID(i18n.Language(f.Language)),
- }, nil
- }
|