package mapper import ( "database/sql" "github.com/dmitriygnatenko/internal/dto" "github.com/dmitriygnatenko/internal/helpers" "github.com/dmitriygnatenko/internal/models" ) func ConvertArticleModelsToDTO(m []models.Article) ([]dto.ArticleDTO, error) { res := make([]dto.ArticleDTO, 0, len(m)) for i := range m { res = append(res, dto.ArticleDTO{ ID: m[i].ID, URL: m[i].URL, Title: m[i].Title, Text: m[i].Text, Image: m[i].Image.String, MetaKeywords: m[i].MetaKeywords.String, MetaDescription: m[i].MetaDescription.String, PublishTime: helpers.FormatDateStr(m[i].PublishTime), }) } return res, nil } func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) { return &dto.ArticleDTO{ ID: m.ID, URL: m.URL, Title: m.Title, Text: m.Text, Image: m.Image.String, MetaKeywords: m.MetaKeywords.String, MetaDescription: m.MetaDescription.String, PublishTime: helpers.FormatDateStr(m.PublishTime), }, nil } func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) ([]dto.ArticlePreviewDTO, error) { res := make([]dto.ArticlePreviewDTO, 0, len(m)) for i := range m { res = append(res, dto.ArticlePreviewDTO{ ID: m[i].ID, URL: m[i].URL, Title: m[i].Title, PreviewText: m[i].PreviewText.String, Image: m[i].Image.String, PublishTime: helpers.FormatDateStr(m[i].PublishTime), }) } return res, nil } func ConvertArticleModelToForm(a models.Article, tags []models.Tag) (*models.ArticleForm, error) { tagMap := make(map[int]bool, len(tags)) for i := range tags { tagMap[tags[i].ID] = true } return &models.ArticleForm{ ID: a.ID, Title: a.Title, Image: a.Image.String, URL: a.URL, IsActive: a.IsActive, Text: a.Text, PreviewText: a.PreviewText.String, MetaKeywords: a.MetaKeywords.String, MetaDescription: a.MetaDescription.String, PublishTime: a.PublishTime, ActiveTags: tagMap, }, nil } func ConvertArticleFormToModel(f models.ArticleForm) models.Article { var previewText, image, metaKeywords, metaDesc sql.NullString if f.PreviewText != "" { previewText = sql.NullString{ String: f.PreviewText, Valid: true, } } if f.Image != "" { image = sql.NullString{ String: f.Image, Valid: true, } } if f.MetaKeywords != "" { metaKeywords = sql.NullString{ String: f.MetaKeywords, Valid: true, } } if f.MetaDescription != "" { metaDesc = sql.NullString{ String: f.MetaDescription, Valid: true, } } return models.Article{ ID: f.ID, URL: f.URL, Title: f.Title, PublishTime: f.PublishTime, Text: f.Text, PreviewText: previewText, IsActive: f.IsActive, Image: image, MetaKeywords: metaKeywords, MetaDescription: metaDesc, } }