article.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 ConvertArticleModelsToDTO(m []models.Article) ([]dto.ArticleDTO, error) {
  8. res := make([]dto.ArticleDTO, 0, len(m))
  9. for i := range m {
  10. parsedDateTime, err := helpers.ParseDateTime(m[i].PublishTime)
  11. if err != nil {
  12. return nil, err
  13. }
  14. res = append(res, dto.ArticleDTO{
  15. ID: m[i].ID,
  16. URL: m[i].URL,
  17. Title: m[i].Title,
  18. Text: m[i].Text,
  19. PublishTime: helpers.FormatDateStr(parsedDateTime),
  20. Image: m[i].Image.String,
  21. MetaKeywords: m[i].MetaKeywords.String,
  22. MetaDescription: m[i].MetaDescription.String,
  23. })
  24. }
  25. return res, nil
  26. }
  27. func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) {
  28. parsedDateTime, err := helpers.ParseDateTime(m.PublishTime)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &dto.ArticleDTO{
  33. ID: m.ID,
  34. URL: m.URL,
  35. Title: m.Title,
  36. PublishTime: helpers.FormatDateStr(parsedDateTime),
  37. Text: m.Text,
  38. Image: m.Image.String,
  39. MetaKeywords: m.MetaKeywords.String,
  40. MetaDescription: m.MetaDescription.String,
  41. }, nil
  42. }
  43. func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) ([]dto.ArticlePreviewDTO, error) {
  44. res := make([]dto.ArticlePreviewDTO, 0, len(m))
  45. for i := range m {
  46. parsedDateTime, err := helpers.ParseDateTime(m[i].PublishTime)
  47. if err != nil {
  48. return nil, err
  49. }
  50. res = append(res, dto.ArticlePreviewDTO{
  51. ID: m[i].ID,
  52. URL: m[i].URL,
  53. Title: m[i].Title,
  54. PublishTime: helpers.FormatDateStr(parsedDateTime),
  55. PreviewText: m[i].PreviewText.String,
  56. Image: m[i].Image.String,
  57. })
  58. }
  59. return res, nil
  60. }
  61. func ConvertArticleModelToForm(a models.Article, tags []models.Tag) (*models.ArticleForm, error) {
  62. parsedDateTime, err := helpers.ParseDateTime(a.PublishTime)
  63. if err != nil {
  64. return nil, err
  65. }
  66. tagMap := make(map[int]bool, len(tags))
  67. for i := range tags {
  68. tagMap[tags[i].ID] = true
  69. }
  70. return &models.ArticleForm{
  71. ID: a.ID,
  72. Title: a.Title,
  73. Image: a.Image.String,
  74. URL: a.URL,
  75. IsActive: a.IsActive,
  76. Text: a.Text,
  77. PreviewText: a.PreviewText.String,
  78. MetaKeywords: a.MetaKeywords.String,
  79. MetaDescription: a.MetaDescription.String,
  80. PublishTime: helpers.FormatDateForm(parsedDateTime),
  81. Tags: tagMap,
  82. }, nil
  83. }