article.go 3.2 KB

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