12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package mapper
- import (
- "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 {
- parsedDateTime, err := helpers.ParseDateTime(m[i].PublishTime)
- if err != nil {
- return nil, err
- }
- res = append(res, dto.ArticleDTO{
- ID: m[i].ID,
- URL: m[i].URL,
- Title: m[i].Title,
- Text: m[i].Text,
- PublishTime: helpers.FormatDateStr(parsedDateTime),
- Image: m[i].Image.String,
- MetaKeywords: m[i].MetaKeywords.String,
- MetaDescription: m[i].MetaDescription.String,
- })
- }
- return res, nil
- }
- func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) {
- parsedDateTime, err := helpers.ParseDateTime(m.PublishTime)
- if err != nil {
- return nil, err
- }
- return &dto.ArticleDTO{
- ID: m.ID,
- URL: m.URL,
- Title: m.Title,
- PublishTime: helpers.FormatDateStr(parsedDateTime),
- Text: m.Text,
- Image: m.Image.String,
- MetaKeywords: m.MetaKeywords.String,
- MetaDescription: m.MetaDescription.String,
- }, nil
- }
- func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) ([]dto.ArticlePreviewDTO, error) {
- res := make([]dto.ArticlePreviewDTO, 0, len(m))
- for i := range m {
- parsedDateTime, err := helpers.ParseDateTime(m[i].PublishTime)
- if err != nil {
- return nil, err
- }
- res = append(res, dto.ArticlePreviewDTO{
- ID: m[i].ID,
- URL: m[i].URL,
- Title: m[i].Title,
- PublishTime: helpers.FormatDateStr(parsedDateTime),
- PreviewText: m[i].PreviewText.String,
- Image: m[i].Image.String,
- })
- }
- return res, nil
- }
- func ConvertArticleModelToForm(a models.Article, tags []models.Tag) (*models.ArticleForm, error) {
- parsedDateTime, err := helpers.ParseDateTime(a.PublishTime)
- if err != nil {
- return nil, err
- }
- 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: helpers.FormatDateForm(parsedDateTime),
- Tags: tagMap,
- }, nil
- }
|