article.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. res = append(res, dto.ArticleDTO{
  12. ID: m[i].ID,
  13. URL: m[i].URL,
  14. Title: m[i].Title,
  15. Text: m[i].Text,
  16. Image: m[i].Image.String,
  17. MetaKeywords: m[i].MetaKeywords.String,
  18. MetaDescription: m[i].MetaDescription.String,
  19. PublishTime: helpers.FormatDateStr(m[i].PublishTime),
  20. })
  21. }
  22. return res, nil
  23. }
  24. func ConvertArticleModelToDTO(m models.Article) (*dto.ArticleDTO, error) {
  25. return &dto.ArticleDTO{
  26. ID: m.ID,
  27. URL: m.URL,
  28. Title: m.Title,
  29. Text: m.Text,
  30. Image: m.Image.String,
  31. MetaKeywords: m.MetaKeywords.String,
  32. MetaDescription: m.MetaDescription.String,
  33. PublishTime: helpers.FormatDateStr(m.PublishTime),
  34. }, nil
  35. }
  36. func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) ([]dto.ArticlePreviewDTO, error) {
  37. res := make([]dto.ArticlePreviewDTO, 0, len(m))
  38. for i := range m {
  39. res = append(res, dto.ArticlePreviewDTO{
  40. ID: m[i].ID,
  41. URL: m[i].URL,
  42. Title: m[i].Title,
  43. PreviewText: m[i].PreviewText.String,
  44. Image: m[i].Image.String,
  45. PublishTime: helpers.FormatDateStr(m[i].PublishTime),
  46. })
  47. }
  48. return res, nil
  49. }
  50. func ConvertArticleModelToForm(a models.Article, tags []models.Tag) (*models.ArticleForm, error) {
  51. tagMap := make(map[int]bool, len(tags))
  52. for i := range tags {
  53. tagMap[tags[i].ID] = true
  54. }
  55. return &models.ArticleForm{
  56. ID: a.ID,
  57. Title: a.Title,
  58. Image: a.Image.String,
  59. URL: a.URL,
  60. IsActive: a.IsActive,
  61. Text: a.Text,
  62. PreviewText: a.PreviewText.String,
  63. MetaKeywords: a.MetaKeywords.String,
  64. MetaDescription: a.MetaDescription.String,
  65. PublishTime: a.PublishTime,
  66. ActiveTags: tagMap,
  67. }, nil
  68. }
  69. func ConvertArticleFormToModel(f models.ArticleForm) models.Article {
  70. var previewText, image, metaKeywords, metaDesc sql.NullString
  71. if f.PreviewText != "" {
  72. previewText = sql.NullString{
  73. String: f.PreviewText,
  74. Valid: true,
  75. }
  76. }
  77. if f.Image != "" {
  78. image = sql.NullString{
  79. String: f.Image,
  80. Valid: true,
  81. }
  82. }
  83. if f.MetaKeywords != "" {
  84. metaKeywords = sql.NullString{
  85. String: f.MetaKeywords,
  86. Valid: true,
  87. }
  88. }
  89. if f.MetaDescription != "" {
  90. metaDesc = sql.NullString{
  91. String: f.MetaDescription,
  92. Valid: true,
  93. }
  94. }
  95. return models.Article{
  96. ID: f.ID,
  97. URL: f.URL,
  98. Title: f.Title,
  99. PublishTime: f.PublishTime,
  100. Text: f.Text,
  101. PreviewText: previewText,
  102. IsActive: f.IsActive,
  103. Image: image,
  104. MetaKeywords: metaKeywords,
  105. MetaDescription: metaDesc,
  106. }
  107. }