package admin import ( "context" "git.dmitriygnatenko.ru/dima/go-common/logger" "github.com/gofiber/fiber/v2" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helpers/request" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n" ) type articleDeletePage struct { tm TransactionManager articleRepository ArticleRepository articleTagRepository ArticleTagRepository cacheService CacheService } func NewArticleDeletePageHandler( tm TransactionManager, articleRepository ArticleRepository, articleTagRepository ArticleTagRepository, cacheService CacheService, ) fiber.Handler { h := articleDeletePage{ tm: tm, articleRepository: articleRepository, articleTagRepository: articleTagRepository, cacheService: cacheService, } return h.handler() } func (h articleDeletePage) handler() fiber.Handler { return func(fctx *fiber.Ctx) error { ctx := fctx.Context() lang := i18n.LanguageFromContext(fctx) id, err := request.ConvertToUint64(fctx, "id") if err != nil { logger.Error(ctx, err.Error()) return err } article, err := h.articleRepository.GetByID(ctx, id) if err != nil { logger.Error(ctx, err.Error()) return err } if fctx.Method() == fiber.MethodPost { err = h.tm.ReadCommitted(ctx, func(ctx context.Context) error { txErr := h.articleTagRepository.DeleteByArticleID(ctx, id) if txErr != nil { return txErr } txErr = h.articleRepository.Delete(ctx, id) if txErr != nil { return txErr } return nil }) if err != nil { logger.Error(ctx, err.Error()) return err } h.cacheService.Delete(handler.ArticleCacheKey + string(lang) + article.URL) return fctx.Redirect("/admin") } return fctx.Render("admin/article_delete", fiber.Map{ "article": article.Title, "section": "article", }, "admin/_layout") } }