article.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package admin
  2. import (
  3. "strconv"
  4. "github.com/dmitriygnatenko/internal/helpers"
  5. "github.com/dmitriygnatenko/internal/interfaces"
  6. "github.com/dmitriygnatenko/internal/mapper"
  7. "github.com/dmitriygnatenko/internal/models"
  8. "github.com/go-playground/validator/v10"
  9. "github.com/gofiber/fiber/v2"
  10. )
  11. const errArticleExists = "Статья с данным URL уже существует"
  12. func ArticleHandler(app interfaces.IApp) fiber.Handler {
  13. return func(ctx *fiber.Ctx) error {
  14. articles, err := app.GetArticleRepository().GetAll(ctx.Context())
  15. if err != nil {
  16. return err
  17. }
  18. articlesDTO, err := mapper.ConvertArticleModelsToDTO(articles)
  19. if err != nil {
  20. return err
  21. }
  22. return ctx.Render("admin/article", fiber.Map{
  23. "articles": articlesDTO,
  24. "section": "article",
  25. }, "admin/_layout")
  26. }
  27. }
  28. func AddArticleHandler(app interfaces.IApp) fiber.Handler {
  29. return func(ctx *fiber.Ctx) error {
  30. var validate = validator.New()
  31. validateErrors := make(map[string]string)
  32. trans, err := helpers.GetDefaultTranslator(validate)
  33. if err != nil {
  34. return err
  35. }
  36. form := models.ArticleForm{
  37. ActiveTags: make(map[int]bool),
  38. }
  39. tags, err := app.GetTagRepository().GetAll(ctx.Context())
  40. if err != nil {
  41. return err
  42. }
  43. tagsDTO, err := mapper.ConvertTagModelsToDTO(tags)
  44. if err != nil {
  45. return err
  46. }
  47. if ctx.Method() == fiber.MethodPost {
  48. if err = ctx.BodyParser(&form); err != nil {
  49. return err
  50. }
  51. if err = validate.Struct(form); err != nil {
  52. validateErrors = helpers.FormatValidateErrors(err, trans)
  53. }
  54. if res, _ := app.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
  55. validateErrors["ArticleForm.URL"] = errArticleExists
  56. }
  57. tagIDs := make([]int, 0, len(form.Tags))
  58. for i := range form.Tags {
  59. tagID, tagErr := strconv.Atoi(form.Tags[i])
  60. if tagErr != nil {
  61. return tagErr
  62. }
  63. tagIDs = append(tagIDs, tagID)
  64. }
  65. if len(validateErrors) == 0 {
  66. articleID, articleErr := app.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form))
  67. if articleErr != nil {
  68. return articleErr
  69. }
  70. if len(form.Tags) > 0 {
  71. if err = app.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil {
  72. return err
  73. }
  74. }
  75. app.GetCacheService().FlushAll()
  76. if err = ctx.Redirect("/admin"); err != nil {
  77. return err
  78. }
  79. }
  80. for i := range tagIDs {
  81. form.ActiveTags[tagIDs[i]] = true
  82. }
  83. }
  84. return ctx.Render("admin/article_edit", fiber.Map{
  85. "form": form,
  86. "errors": validateErrors,
  87. "tags": tagsDTO,
  88. "section": "article",
  89. "title": "Добавление статьи",
  90. "show_apply": false,
  91. }, "admin/_layout")
  92. }
  93. }
  94. func EditArticleHandler(app interfaces.IApp) fiber.Handler {
  95. return func(ctx *fiber.Ctx) error {
  96. var validate = validator.New()
  97. validateErrors := make(map[string]string)
  98. trans, err := helpers.GetDefaultTranslator(validate)
  99. if err != nil {
  100. return err
  101. }
  102. ID, err := strconv.Atoi(ctx.Params("id"))
  103. if err != nil {
  104. return err
  105. }
  106. article, err := app.GetArticleRepository().GetByID(ctx.Context(), ID)
  107. if err != nil {
  108. return err
  109. }
  110. if article == nil {
  111. return fiber.ErrNotFound
  112. }
  113. articleTags, err := app.GetTagRepository().GetByArticleID(ctx.Context(), ID)
  114. if err != nil {
  115. return err
  116. }
  117. form, err := mapper.ConvertArticleModelToForm(*article, articleTags)
  118. if err != nil {
  119. return err
  120. }
  121. tags, err := app.GetTagRepository().GetAll(ctx.Context())
  122. if err != nil {
  123. return err
  124. }
  125. tagsDTO, err := mapper.ConvertTagModelsToDTO(tags)
  126. if err != nil {
  127. return err
  128. }
  129. if ctx.Method() == fiber.MethodPost {
  130. if err = ctx.BodyParser(&form); err != nil {
  131. return err
  132. }
  133. if err = validate.Struct(form); err != nil {
  134. validateErrors = helpers.FormatValidateErrors(err, trans)
  135. }
  136. // TODO
  137. }
  138. return ctx.Render("admin/article_edit", fiber.Map{
  139. "form": form,
  140. "errors": validateErrors,
  141. "tags": tagsDTO,
  142. "section": "article",
  143. "title": "Редактирование статьи",
  144. }, "admin/_layout")
  145. }
  146. }
  147. func DeleteArticleHandler(app interfaces.IApp) fiber.Handler {
  148. return func(ctx *fiber.Ctx) error {
  149. return nil
  150. }
  151. }