article_delete.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package admin
  2. import (
  3. "context"
  4. "git.dmitriygnatenko.ru/dima/go-common/db"
  5. "git.dmitriygnatenko.ru/dima/go-common/logger"
  6. "github.com/gofiber/fiber/v2"
  7. customErrors "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helper/errors"
  8. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helper/request"
  9. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/model"
  10. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/service/handler"
  11. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/service/i18n"
  12. )
  13. type deleteArticlePage struct {
  14. tm TransactionManager
  15. articleRepository ArticleRepository
  16. articleTagRepository ArticleTagRepository
  17. cacheService CacheService
  18. }
  19. func NewDeleteArticlePageHandler(
  20. tm TransactionManager,
  21. articleRepository ArticleRepository,
  22. articleTagRepository ArticleTagRepository,
  23. cacheService CacheService,
  24. ) fiber.Handler {
  25. h := deleteArticlePage{
  26. tm: tm,
  27. articleRepository: articleRepository,
  28. articleTagRepository: articleTagRepository,
  29. cacheService: cacheService,
  30. }
  31. return h.handler()
  32. }
  33. func (h deleteArticlePage) handler() fiber.Handler {
  34. return func(fctx *fiber.Ctx) error {
  35. ctx := fctx.Context()
  36. lang := i18n.LanguageFromContext(fctx)
  37. articleID, err := request.ConvertToUint64(fctx, "id")
  38. if err != nil {
  39. logger.Error(ctx, customErrors.Wrap(err, "delete article page: can't get id from request").Error())
  40. return err
  41. }
  42. article, err := h.getArticle(ctx, articleID)
  43. if err != nil {
  44. return err // logging in the getArticle method
  45. }
  46. if fctx.Method() == fiber.MethodPost {
  47. err = h.tm.ReadCommitted(ctx, func(ctx context.Context) error {
  48. txErr := h.articleTagRepository.DeleteByArticleID(ctx, articleID)
  49. if txErr != nil {
  50. return customErrors.Wrap(txErr, "can't delete tags")
  51. }
  52. txErr = h.articleRepository.Delete(ctx, articleID)
  53. if txErr != nil {
  54. return customErrors.Wrap(txErr, "can't delete article")
  55. }
  56. return nil
  57. })
  58. if err != nil {
  59. logger.Error(ctx, customErrors.Wrap(err, "delete article page").Error())
  60. return err
  61. }
  62. articleLang := string(i18n.LanguageFromID(article.Language))
  63. h.cacheService.Delete(handler.ArticleCacheKey + article.URL + articleLang)
  64. h.cacheService.Delete(handler.RecentArticlesCacheKey + articleLang)
  65. h.cacheService.Delete(handler.UsedTagsCacheKey + articleLang)
  66. h.cacheService.Delete(handler.AllPreviewArticlesCacheKey + articleLang)
  67. return fctx.Redirect("/admin")
  68. }
  69. return fctx.Render("admin/article_delete", fiber.Map{
  70. "lang": lang,
  71. "section": "article",
  72. "article": article.Title,
  73. }, "admin/_layout")
  74. }
  75. }
  76. func (h deleteArticlePage) getArticle(ctx context.Context, id uint64) (*model.Article, error) {
  77. article, err := h.articleRepository.GetByID(ctx, id)
  78. if err != nil {
  79. if db.IsNotFoundError(err) {
  80. return nil, fiber.ErrNotFound
  81. }
  82. logger.Error(ctx, customErrors.Wrap(err, "delete article page: can't get article").Error())
  83. return nil, err
  84. }
  85. return article, nil
  86. }