article.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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"
  6. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models"
  7. )
  8. func ConvertArticleModelsToDTO(m []models.Article) []dto.ArticleDTO {
  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
  23. }
  24. func ConvertArticleModelToDTO(m models.Article) *dto.ArticleDTO {
  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. }
  35. }
  36. func ConvertArticlePreviewModelsToDTO(m []models.ArticlePreview) []dto.ArticlePreviewDTO {
  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
  49. }
  50. func ConvertArticleModelToForm(a models.Article, tags []models.Tag) *models.ArticleForm {
  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: helpers.FormatDateForm(a.PublishTime),
  66. ActiveTags: tagMap,
  67. }
  68. }
  69. func ConvertArticleFormToModel(f models.ArticleForm) (*models.Article, error) {
  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. parsedDateTime, err := helpers.ParseDateTime(f.PublishTime)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return &models.Article{
  100. ID: f.ID,
  101. URL: f.URL,
  102. Title: f.Title,
  103. PublishTime: parsedDateTime,
  104. Text: f.Text,
  105. PreviewText: previewText,
  106. IsActive: f.IsActive,
  107. Image: image,
  108. MetaKeywords: metaKeywords,
  109. MetaDescription: metaDesc,
  110. }, nil
  111. }