article_edit.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package admin
  2. import (
  3. "context"
  4. "errors"
  5. "strconv"
  6. "git.dmitriygnatenko.ru/dima/go-common/db"
  7. "git.dmitriygnatenko.ru/dima/go-common/logger"
  8. validation "github.com/go-ozzo/ozzo-validation/v4"
  9. "github.com/gofiber/fiber/v2"
  10. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/dto"
  11. customErrors "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helper/errors"
  12. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helper/request"
  13. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/mapper"
  14. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/model"
  15. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/service/handler"
  16. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/service/i18n"
  17. )
  18. type editArticlePage struct {
  19. tm TransactionManager
  20. articleRepository ArticleRepository
  21. tagRepository TagRepository
  22. articleTagRepository ArticleTagRepository
  23. languageRepository LanguageRepository
  24. cacheService CacheService
  25. }
  26. func NewEditArticlePageHandler(
  27. tm TransactionManager,
  28. articleRepository ArticleRepository,
  29. tagRepository TagRepository,
  30. articleTagRepository ArticleTagRepository,
  31. languageRepository LanguageRepository,
  32. cacheService CacheService,
  33. ) fiber.Handler {
  34. h := editArticlePage{
  35. tm: tm,
  36. articleRepository: articleRepository,
  37. tagRepository: tagRepository,
  38. articleTagRepository: articleTagRepository,
  39. languageRepository: languageRepository,
  40. cacheService: cacheService,
  41. }
  42. return h.handler()
  43. }
  44. func (h editArticlePage) handler() fiber.Handler {
  45. return func(fctx *fiber.Ctx) error {
  46. ctx := fctx.Context()
  47. lang := i18n.LanguageFromContext(fctx)
  48. validationErrors := make(map[string]string)
  49. articleID, err := request.ConvertToUint64(fctx, "id")
  50. if err != nil {
  51. logger.Error(ctx, customErrors.Wrap(err, "edit article page: can't get id from request").Error())
  52. return err
  53. }
  54. tags, err := h.getTags(ctx)
  55. if err != nil {
  56. logger.Error(ctx, customErrors.Wrap(err, "edit article page: can't get tags").Error())
  57. return err
  58. }
  59. languages, err := h.getLanguages(ctx)
  60. if err != nil {
  61. logger.Error(ctx, customErrors.Wrap(err, "edit article page: can't get languages").Error())
  62. return err
  63. }
  64. article, err := h.getArticle(ctx, articleID)
  65. if err != nil {
  66. return err // logging in the getArticle method
  67. }
  68. articleTags, err := h.tagRepository.GetByArticleID(ctx, articleID)
  69. if err != nil {
  70. logger.Error(ctx, customErrors.Wrap(err, "edit article page: can't get article tags").Error())
  71. return err
  72. }
  73. var form dto.ArticleForm
  74. if fctx.Method() == fiber.MethodGet {
  75. form = mapper.ToArticleForm(*article, articleTags)
  76. } else if fctx.Method() == fiber.MethodPost {
  77. form = dto.ArticleForm{
  78. ID: articleID,
  79. ActiveTags: make(map[uint64]bool),
  80. }
  81. if err = fctx.BodyParser(&form); err != nil {
  82. logger.Info(ctx, customErrors.Wrap(err, "edit article page: can't parse body").Error())
  83. return err
  84. }
  85. if err = h.validateArticleForm(ctx, form, *article); err != nil {
  86. logger.Info(ctx, customErrors.Wrap(err, "edit article page: validation").Error())
  87. validationErrors = mapper.ToErrorsMap(err)
  88. }
  89. tagIDs, err := h.formatTags(form.Tags)
  90. if err != nil {
  91. logger.Error(ctx, customErrors.Wrap(err, "edit article page: can't format tags").Error())
  92. return err
  93. }
  94. for i := range tagIDs {
  95. form.ActiveTags[tagIDs[i]] = true
  96. }
  97. if len(validationErrors) == 0 {
  98. articleModel, err := mapper.ToArticleModel(form)
  99. if err != nil {
  100. logger.Error(
  101. ctx,
  102. customErrors.Wrap(err, "edit article page: can't create article model").Error(),
  103. )
  104. return err
  105. }
  106. var tagsToAdd, tagsToDelete []uint64
  107. oldTagsMap := make(map[int]struct{}, len(articleTags))
  108. for i := range articleTags {
  109. oldTagsMap[int(articleTags[i].ID)] = struct{}{}
  110. if _, ok := form.ActiveTags[articleTags[i].ID]; !ok {
  111. tagsToDelete = append(tagsToDelete, articleTags[i].ID)
  112. }
  113. }
  114. for i := range tagIDs {
  115. if _, ok := oldTagsMap[int(tagIDs[i])]; !ok {
  116. tagsToAdd = append(tagsToAdd, tagIDs[i])
  117. }
  118. }
  119. err = h.tm.ReadCommitted(ctx, func(ctx context.Context) error {
  120. txErr := h.articleRepository.Update(ctx, *articleModel)
  121. if txErr != nil {
  122. return customErrors.Wrap(txErr, "can't update article")
  123. }
  124. if len(tagsToAdd) > 0 {
  125. if txErr = h.articleTagRepository.Add(ctx, articleID, tagsToAdd); txErr != nil {
  126. return customErrors.Wrap(txErr, "can't add tags")
  127. }
  128. }
  129. if len(tagsToDelete) > 0 {
  130. if txErr = h.articleTagRepository.Delete(ctx, articleID, tagsToDelete); txErr != nil {
  131. return customErrors.Wrap(txErr, "can't delete tags")
  132. }
  133. }
  134. return nil
  135. })
  136. if err != nil {
  137. logger.Error(ctx, customErrors.Wrap(err, "edit article page").Error())
  138. return err
  139. }
  140. h.cacheService.Delete(handler.ArticleCacheKey + form.URL + form.Language)
  141. h.cacheService.Delete(handler.RecentArticlesCacheKey + form.Language)
  142. h.cacheService.Delete(handler.UsedTagsCacheKey + form.Language)
  143. h.cacheService.Delete(handler.AllPreviewArticlesCacheKey + form.Language)
  144. if fctx.FormValue("action", "save") == "save" {
  145. return fctx.Redirect("/admin")
  146. }
  147. }
  148. }
  149. return fctx.Render("admin/article_edit", fiber.Map{
  150. "lang": lang,
  151. "title": i18n.T(lang, "admin_article_edit_title"),
  152. "section": "article",
  153. "show_apply": true,
  154. "form": form,
  155. "errors": validationErrors,
  156. "tags": tags,
  157. "languages": languages,
  158. }, "admin/_layout")
  159. }
  160. }
  161. func (h editArticlePage) getArticle(ctx context.Context, id uint64) (*model.Article, error) {
  162. article, err := h.articleRepository.GetByID(ctx, id)
  163. if err != nil {
  164. if db.IsNotFoundError(err) {
  165. return nil, fiber.ErrNotFound
  166. }
  167. logger.Error(ctx, customErrors.Wrap(err, "edit article page: can't get article").Error())
  168. return nil, err
  169. }
  170. return article, nil
  171. }
  172. func (h editArticlePage) getTags(ctx context.Context) ([]dto.Tag, error) {
  173. tagModels, err := h.tagRepository.GetAll(ctx)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return mapper.ToTagsList(tagModels), nil
  178. }
  179. func (h editArticlePage) getLanguages(ctx context.Context) ([]string, error) {
  180. langModels, err := h.languageRepository.GetAll(ctx)
  181. if err != nil {
  182. return nil, err
  183. }
  184. res := make([]string, 0, len(langModels))
  185. for i := range langModels {
  186. res = append(res, langModels[i].URL)
  187. }
  188. return res, nil
  189. }
  190. func (h editArticlePage) formatTags(tags []string) ([]uint64, error) {
  191. ids := make([]uint64, 0, len(tags))
  192. for i := range tags {
  193. id, err := strconv.ParseUint(tags[i], 10, 64)
  194. if err != nil {
  195. return nil, err
  196. }
  197. ids = append(ids, id)
  198. }
  199. return ids, nil
  200. }
  201. func (h editArticlePage) validateArticleForm(
  202. ctx context.Context,
  203. req dto.ArticleForm,
  204. article model.Article,
  205. ) error {
  206. lang := i18n.DefaultLanguage()
  207. requiredMsg := i18n.T(lang, "admin_err_required")
  208. lengthMsg := i18n.T(lang, "admin_err_length_255")
  209. return validation.ValidateStructWithContext(
  210. ctx,
  211. &req,
  212. validation.Field(
  213. &req.Title,
  214. validation.Required.Error(requiredMsg),
  215. validation.Length(1, 255).Error(lengthMsg),
  216. ),
  217. validation.Field(
  218. &req.Image,
  219. validation.Required.Error(requiredMsg),
  220. validation.Length(1, 255).Error(lengthMsg),
  221. ),
  222. validation.Field(
  223. &req.URL,
  224. validation.Required.Error(requiredMsg),
  225. validation.Length(1, 255).Error(lengthMsg),
  226. validation.By(h.validateDuplicateURL(article)),
  227. ),
  228. validation.Field(
  229. &req.Text,
  230. validation.Required.Error(requiredMsg),
  231. ),
  232. validation.Field(
  233. &req.MetaKeywords,
  234. validation.Length(1, 255).Error(lengthMsg),
  235. ),
  236. validation.Field(
  237. &req.MetaDescription,
  238. validation.Length(1, 255).Error(lengthMsg),
  239. ),
  240. validation.Field(
  241. &req.PublishTime,
  242. validation.Required.Error(requiredMsg),
  243. ),
  244. validation.Field(
  245. &req.Language,
  246. validation.Required.Error(requiredMsg),
  247. ),
  248. )
  249. }
  250. func (h editArticlePage) validateDuplicateURL(
  251. article model.Article,
  252. ) func(value interface{}) error {
  253. return func(value interface{}) error {
  254. url, _ := value.(string)
  255. if url == article.URL {
  256. return nil
  257. }
  258. ctx := context.Background()
  259. _, err := h.articleRepository.GetByURL(ctx, url, article.Language)
  260. if err != nil {
  261. if !db.IsNotFoundError(err) {
  262. logger.Error(ctx, err.Error())
  263. }
  264. return nil
  265. }
  266. return errors.New(i18n.T(i18n.DefaultLanguage(), "admin_err_article_duplicate", url))
  267. }
  268. }