article.go 3.3 KB

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