article.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package mapper
  2. import (
  3. "database/sql"
  4. "github.com/dmitriygnatenko/internal/dto"
  5. "github.com/dmitriygnatenko/internal/helpers"
  6. "github.com/dmitriygnatenko/internal/models"
  7. )
  8. func ConvertArticleModelsToDTO(m []models.Article) ([]dto.ArticleDTO, error) {
  9. res := make([]dto.ArticleDTO, 0, len(m))
  10. for i := range m {
  11. parsedDateTime, err := helpers.ParseDateTime(m[i].PublishTime)
  12. if err != nil {
  13. return nil, err
  14. }
  15. res = append(res, dto.ArticleDTO{
  16. ID: m[i].ID,
  17. URL: m[i].URL,
  18. Title: m[i].Title,
  19. Text: m[i].Text,
  20. PublishTime: helpers.FormatDateStr(parsedDateTime),
  21. Image: m[i].Image.String,
  22. MetaKeywords: m[i].MetaKeywords.String,
  23. MetaDescription: m[i].MetaDescription.String,
  24. })
  25. }
  26. return res, nil
  27. }
  28. func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) {
  29. parsedDateTime, err := helpers.ParseDateTime(m.PublishTime)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &dto.ArticleDTO{
  34. ID: m.ID,
  35. URL: m.URL,
  36. Title: m.Title,
  37. PublishTime: helpers.FormatDateStr(parsedDateTime),
  38. Text: m.Text,
  39. Image: m.Image.String,
  40. MetaKeywords: m.MetaKeywords.String,
  41. MetaDescription: m.MetaDescription.String,
  42. }, nil
  43. }
  44. func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) ([]dto.ArticlePreviewDTO, error) {
  45. res := make([]dto.ArticlePreviewDTO, 0, len(m))
  46. for i := range m {
  47. parsedDateTime, err := helpers.ParseDateTime(m[i].PublishTime)
  48. if err != nil {
  49. return nil, err
  50. }
  51. res = append(res, dto.ArticlePreviewDTO{
  52. ID: m[i].ID,
  53. URL: m[i].URL,
  54. Title: m[i].Title,
  55. PublishTime: helpers.FormatDateStr(parsedDateTime),
  56. PreviewText: m[i].PreviewText.String,
  57. Image: m[i].Image.String,
  58. })
  59. }
  60. return res, nil
  61. }
  62. func ConvertArticleModelToForm(a models.Article, tags []models.Tag) (*models.ArticleForm, error) {
  63. parsedDateTime, err := helpers.ParseDateTime(a.PublishTime)
  64. if err != nil {
  65. return nil, err
  66. }
  67. tagMap := make(map[int]bool, len(tags))
  68. for i := range tags {
  69. tagMap[tags[i].ID] = true
  70. }
  71. return &models.ArticleForm{
  72. ID: a.ID,
  73. Title: a.Title,
  74. Image: a.Image.String,
  75. URL: a.URL,
  76. IsActive: a.IsActive,
  77. Text: a.Text,
  78. PreviewText: a.PreviewText.String,
  79. MetaKeywords: a.MetaKeywords.String,
  80. MetaDescription: a.MetaDescription.String,
  81. PublishTime: helpers.FormatDateForm(parsedDateTime),
  82. ActiveTags: tagMap,
  83. }, nil
  84. }
  85. func ConvertArticleFormToModel(f models.ArticleForm) models.Article {
  86. var previewText, image, metaKeywords, metaDesc sql.NullString
  87. if f.PreviewText != "" {
  88. previewText = sql.NullString{
  89. String: f.PreviewText,
  90. Valid: true,
  91. }
  92. }
  93. if f.Image != "" {
  94. image = sql.NullString{
  95. String: f.Image,
  96. Valid: true,
  97. }
  98. }
  99. if f.MetaKeywords != "" {
  100. metaKeywords = sql.NullString{
  101. String: f.MetaKeywords,
  102. Valid: true,
  103. }
  104. }
  105. if f.MetaDescription != "" {
  106. metaDesc = sql.NullString{
  107. String: f.MetaDescription,
  108. Valid: true,
  109. }
  110. }
  111. return models.Article{
  112. ID: f.ID,
  113. URL: f.URL,
  114. Title: f.Title,
  115. PublishTime: f.PublishTime,
  116. Text: f.Text,
  117. PreviewText: previewText,
  118. IsActive: f.IsActive,
  119. Image: image,
  120. MetaKeywords: metaKeywords,
  121. MetaDescription: metaDesc,
  122. }
  123. }