article.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. for i := range tagIDs {
  66. form.ActiveTags[tagIDs[i]] = true
  67. }
  68. if len(validateErrors) == 0 {
  69. articleID, articleErr := app.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form))
  70. if articleErr != nil {
  71. return articleErr
  72. }
  73. if len(form.Tags) > 0 {
  74. if err = app.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil {
  75. return err
  76. }
  77. }
  78. app.GetCacheService().FlushAll()
  79. if err = ctx.Redirect("/admin"); err != nil {
  80. return err
  81. }
  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. tags, err := app.GetTagRepository().GetAll(ctx.Context())
  118. if err != nil {
  119. return err
  120. }
  121. tagsDTO, err := mapper.ConvertTagModelsToDTO(tags)
  122. if err != nil {
  123. return err
  124. }
  125. var form *models.ArticleForm
  126. if ctx.Method() == fiber.MethodGet {
  127. form, err = mapper.ConvertArticleModelToForm(*article, articleTags)
  128. if err != nil {
  129. return err
  130. }
  131. } else if ctx.Method() == fiber.MethodPost {
  132. form = &models.ArticleForm{
  133. ID: ID,
  134. ActiveTags: make(map[int]bool),
  135. }
  136. if err = ctx.BodyParser(form); err != nil {
  137. return err
  138. }
  139. if err = validate.Struct(*form); err != nil {
  140. validateErrors = helpers.FormatValidateErrors(err, trans)
  141. }
  142. if res, _ := app.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
  143. if res.ID != ID {
  144. validateErrors["ArticleForm.URL"] = errArticleExists
  145. }
  146. }
  147. tagIDs := make([]int, 0, len(form.Tags))
  148. for i := range form.Tags {
  149. tagID, tagErr := strconv.Atoi(form.Tags[i])
  150. if tagErr != nil {
  151. return tagErr
  152. }
  153. tagIDs = append(tagIDs, tagID)
  154. }
  155. for i := range tagIDs {
  156. form.ActiveTags[tagIDs[i]] = true
  157. }
  158. if len(validateErrors) == 0 {
  159. err = app.GetArticleRepository().Update(ctx.Context(), mapper.ConvertArticleFormToModel(*form))
  160. if err != nil {
  161. return err
  162. }
  163. var tagsToAdd, tagsToDelete []int
  164. oldTagsMap := make(map[int]struct{}, len(articleTags))
  165. for i := range articleTags {
  166. oldTagsMap[articleTags[i].ID] = struct{}{}
  167. if _, ok := form.ActiveTags[articleTags[i].ID]; !ok {
  168. tagsToDelete = append(tagsToDelete, articleTags[i].ID)
  169. }
  170. }
  171. for i := range tagIDs {
  172. if _, ok := oldTagsMap[tagIDs[i]]; !ok {
  173. tagsToAdd = append(tagsToAdd, tagIDs[i])
  174. }
  175. }
  176. if len(tagsToAdd) > 0 {
  177. if err = app.GetArticleTagRepository().Add(ctx.Context(), ID, tagsToAdd); err != nil {
  178. return err
  179. }
  180. }
  181. if len(tagsToDelete) > 0 {
  182. if err = app.GetArticleTagRepository().Delete(ctx.Context(), ID, tagsToDelete); err != nil {
  183. return err
  184. }
  185. }
  186. app.GetCacheService().FlushAll()
  187. if ctx.FormValue("action", "save") == "save" {
  188. if err = ctx.Redirect("/admin"); err != nil {
  189. return err
  190. }
  191. }
  192. }
  193. }
  194. return ctx.Render("admin/article_edit", fiber.Map{
  195. "form": form,
  196. "errors": validateErrors,
  197. "tags": tagsDTO,
  198. "show_apply": true,
  199. "section": "article",
  200. "title": "Редактирование статьи",
  201. }, "admin/_layout")
  202. }
  203. }
  204. func DeleteArticleHandler(app interfaces.IApp) fiber.Handler {
  205. return func(ctx *fiber.Ctx) error {
  206. return nil
  207. }
  208. }