article.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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(sp interfaces.IServiceProvider) fiber.Handler {
  13. return func(ctx *fiber.Ctx) error {
  14. articles, err := sp.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(sp interfaces.IServiceProvider) 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 := sp.GetTagRepository().GetAll(ctx.Context())
  40. if err != nil {
  41. return err
  42. }
  43. tagsDTO := mapper.ConvertTagModelsToDTO(tags)
  44. if ctx.Method() == fiber.MethodPost {
  45. if err = ctx.BodyParser(&form); err != nil {
  46. return err
  47. }
  48. if err = validate.Struct(form); err != nil {
  49. validateErrors = helpers.FormatValidateErrors(err, trans)
  50. }
  51. if res, _ := sp.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
  52. validateErrors["ArticleForm.URL"] = errArticleExists
  53. }
  54. tagIDs := make([]int, 0, len(form.Tags))
  55. for i := range form.Tags {
  56. tagID, tagErr := strconv.Atoi(form.Tags[i])
  57. if tagErr != nil {
  58. return tagErr
  59. }
  60. tagIDs = append(tagIDs, tagID)
  61. }
  62. for i := range tagIDs {
  63. form.ActiveTags[tagIDs[i]] = true
  64. }
  65. if len(validateErrors) == 0 {
  66. articleID, articleErr := sp.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form))
  67. if articleErr != nil {
  68. return articleErr
  69. }
  70. if len(form.Tags) > 0 {
  71. if err = sp.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil {
  72. return err
  73. }
  74. }
  75. sp.GetCacheService().FlushAll()
  76. if err = ctx.Redirect("/admin"); err != nil {
  77. return err
  78. }
  79. }
  80. }
  81. return ctx.Render("admin/article_edit", fiber.Map{
  82. "form": form,
  83. "errors": validateErrors,
  84. "tags": tagsDTO,
  85. "section": "article",
  86. "title": "Добавление статьи",
  87. "show_apply": false,
  88. }, "admin/_layout")
  89. }
  90. }
  91. func EditArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
  92. return func(ctx *fiber.Ctx) error {
  93. var validate = validator.New()
  94. validateErrors := make(map[string]string)
  95. trans, err := helpers.GetDefaultTranslator(validate)
  96. if err != nil {
  97. return err
  98. }
  99. ID, err := strconv.Atoi(ctx.Params("id"))
  100. if err != nil {
  101. return err
  102. }
  103. article, err := sp.GetArticleRepository().GetByID(ctx.Context(), ID)
  104. if err != nil {
  105. return err
  106. }
  107. if article == nil {
  108. return fiber.ErrNotFound
  109. }
  110. articleTags, err := sp.GetTagRepository().GetByArticleID(ctx.Context(), ID)
  111. if err != nil {
  112. return err
  113. }
  114. tags, err := sp.GetTagRepository().GetAll(ctx.Context())
  115. if err != nil {
  116. return err
  117. }
  118. tagsDTO := mapper.ConvertTagModelsToDTO(tags)
  119. var form *models.ArticleForm
  120. if ctx.Method() == fiber.MethodGet {
  121. form, err = mapper.ConvertArticleModelToForm(*article, articleTags)
  122. if err != nil {
  123. return err
  124. }
  125. } else if ctx.Method() == fiber.MethodPost {
  126. form = &models.ArticleForm{
  127. ID: ID,
  128. ActiveTags: make(map[int]bool),
  129. }
  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. if res, _ := sp.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
  137. if res.ID != ID {
  138. validateErrors["ArticleForm.URL"] = errArticleExists
  139. }
  140. }
  141. tagIDs := make([]int, 0, len(form.Tags))
  142. for i := range form.Tags {
  143. tagID, tagErr := strconv.Atoi(form.Tags[i])
  144. if tagErr != nil {
  145. return tagErr
  146. }
  147. tagIDs = append(tagIDs, tagID)
  148. }
  149. for i := range tagIDs {
  150. form.ActiveTags[tagIDs[i]] = true
  151. }
  152. if len(validateErrors) == 0 {
  153. err = sp.GetArticleRepository().Update(ctx.Context(), mapper.ConvertArticleFormToModel(*form))
  154. if err != nil {
  155. return err
  156. }
  157. var tagsToAdd, tagsToDelete []int
  158. oldTagsMap := make(map[int]struct{}, len(articleTags))
  159. for i := range articleTags {
  160. oldTagsMap[articleTags[i].ID] = struct{}{}
  161. if _, ok := form.ActiveTags[articleTags[i].ID]; !ok {
  162. tagsToDelete = append(tagsToDelete, articleTags[i].ID)
  163. }
  164. }
  165. for i := range tagIDs {
  166. if _, ok := oldTagsMap[tagIDs[i]]; !ok {
  167. tagsToAdd = append(tagsToAdd, tagIDs[i])
  168. }
  169. }
  170. if len(tagsToAdd) > 0 {
  171. if err = sp.GetArticleTagRepository().Add(ctx.Context(), ID, tagsToAdd); err != nil {
  172. return err
  173. }
  174. }
  175. if len(tagsToDelete) > 0 {
  176. if err = sp.GetArticleTagRepository().Delete(ctx.Context(), ID, tagsToDelete); err != nil {
  177. return err
  178. }
  179. }
  180. sp.GetCacheService().FlushAll()
  181. if ctx.FormValue("action", "save") == "save" {
  182. if err = ctx.Redirect("/admin"); err != nil {
  183. return err
  184. }
  185. }
  186. }
  187. }
  188. return ctx.Render("admin/article_edit", fiber.Map{
  189. "form": form,
  190. "errors": validateErrors,
  191. "tags": tagsDTO,
  192. "show_apply": true,
  193. "section": "article",
  194. "title": "Редактирование статьи",
  195. }, "admin/_layout")
  196. }
  197. }
  198. func DeleteArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
  199. return func(ctx *fiber.Ctx) error {
  200. ID, err := strconv.Atoi(ctx.Params("id"))
  201. if err != nil {
  202. return err
  203. }
  204. article, err := sp.GetArticleRepository().GetByID(ctx.Context(), ID)
  205. if err != nil {
  206. return err
  207. }
  208. if ctx.Method() == fiber.MethodPost {
  209. err = sp.GetArticleTagRepository().DeleteByArticleID(ctx.Context(), ID)
  210. if err != nil {
  211. return err
  212. }
  213. err = sp.GetArticleRepository().Delete(ctx.Context(), ID)
  214. if err != nil {
  215. return err
  216. }
  217. sp.GetCacheService().FlushAll()
  218. if err = ctx.Redirect("/admin"); err != nil {
  219. return err
  220. }
  221. }
  222. return ctx.Render("admin/article_delete", fiber.Map{
  223. "article": article.Title,
  224. "section": "article",
  225. }, "admin/_layout")
  226. }
  227. }