article.go 3.2 KB

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