article.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package mapper
  2. import (
  3. "github.com/dmitriygnatenko/internal/dto"
  4. "github.com/dmitriygnatenko/internal/helpers"
  5. "github.com/dmitriygnatenko/internal/models"
  6. )
  7. func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) {
  8. parsedDateTime, err := helpers.ParseDateTime(m.PublishTime)
  9. if err != nil {
  10. return nil, err
  11. }
  12. return &dto.ArticleDTO{
  13. ID: m.ID,
  14. URL: m.URL,
  15. Title: m.Title,
  16. PublishTime: helpers.FormatDateStr(parsedDateTime),
  17. Text: m.Text,
  18. Image: m.Image.String,
  19. MetaKeywords: m.MetaKeywords.String,
  20. MetaDescription: m.MetaDescription.String,
  21. }, nil
  22. }
  23. func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) ([]dto.ArticlePreviewDTO, error) {
  24. res := make([]dto.ArticlePreviewDTO, 0, len(m))
  25. for i := range m {
  26. parsedDateTime, err := helpers.ParseDateTime(m[i].PublishTime)
  27. if err != nil {
  28. return nil, err
  29. }
  30. res = append(res, dto.ArticlePreviewDTO{
  31. ID: m[i].ID,
  32. URL: m[i].URL,
  33. Title: m[i].Title,
  34. PublishTime: helpers.FormatDateStr(parsedDateTime),
  35. PreviewText: m[i].PreviewText.String,
  36. Image: m[i].Image.String,
  37. })
  38. }
  39. return res, nil
  40. }
  41. func ConvertArticleModelToForm(a models.Article, tags []models.Tag) (*models.ArticleForm, error) {
  42. parsedDateTime, err := helpers.ParseDateTime(a.PublishTime)
  43. if err != nil {
  44. return nil, err
  45. }
  46. tagMap := make(map[int]bool, len(tags))
  47. for i := range tags {
  48. tagMap[tags[i].ID] = true
  49. }
  50. return &models.ArticleForm{
  51. ID: a.ID,
  52. Title: a.Title,
  53. Image: a.Image.String,
  54. URL: a.URL,
  55. IsActive: a.IsActive,
  56. Text: a.Text,
  57. PreviewText: a.PreviewText.String,
  58. MetaKeywords: a.MetaKeywords.String,
  59. MetaDescription: a.MetaDescription.String,
  60. PublishTime: helpers.FormatDateForm(parsedDateTime),
  61. Tags: tagMap,
  62. }, nil
  63. }