123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package admin
- import (
- "context"
- "strconv"
- "git.dmitriygnatenko.ru/dima/go-common/logger"
- "github.com/go-playground/validator/v10"
- "github.com/gofiber/fiber/v2"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/dto"
- helper "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helpers/i18n"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helpers/request"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/mapper"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/handler"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
- )
- type articleEditPage struct {
- tm TransactionManager
- articleRepository ArticleRepository
- tagRepository TagRepository
- articleTagRepository ArticleTagRepository
- cacheService CacheService
- }
- func NewArticleEditPageHandler(
- tm TransactionManager,
- articleRepository ArticleRepository,
- tagRepository TagRepository,
- articleTagRepository ArticleTagRepository,
- cacheService CacheService,
- ) fiber.Handler {
- h := articleEditPage{
- tm: tm,
- articleRepository: articleRepository,
- tagRepository: tagRepository,
- articleTagRepository: articleTagRepository,
- cacheService: cacheService,
- }
- return h.handler()
- }
- func (h articleEditPage) handler() fiber.Handler {
- return func(fctx *fiber.Ctx) error {
- ctx := fctx.Context()
- lang := i18n.LanguageFromContext(fctx)
- var validate = validator.New()
- validateErrors := make(map[string]string)
- trans, err := helper.GetDefaultTranslator(validate)
- if err != nil {
- logger.Error(ctx, err.Error())
- return err
- }
- 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 article == nil {
- return fiber.ErrNotFound
- }
- articleTags, err := h.tagRepository.GetByArticleID(ctx, id)
- if err != nil {
- logger.Error(ctx, err.Error())
- return err
- }
- tags, err := h.tagRepository.GetAll(ctx)
- if err != nil {
- logger.Error(ctx, err.Error())
- return err
- }
- tagsDTO := mapper.ToTagsList(tags)
- var form *dto.ArticleForm
- if fctx.Method() == fiber.MethodGet {
- form = mapper.ToArticleForm(*article, articleTags)
- } else if fctx.Method() == fiber.MethodPost {
- form = &dto.ArticleForm{
- ID: id,
- ActiveTags: make(map[uint64]bool),
- }
- if err = fctx.BodyParser(form); err != nil {
- logger.Info(ctx, err.Error())
- return err
- }
- if err = validate.Struct(*form); err != nil {
- validateErrors = helper.FormatValidateErrors(err, trans)
- }
- if res, _ := h.articleRepository.GetByURL(ctx, form.URL); res != nil {
- if res.ID != id {
- validateErrors["ArticleForm.URL"] = i18n.T(lang, "admin_err_article_exists")
- }
- }
- tagIDs := make([]uint64, 0, len(form.Tags))
- for i := range form.Tags {
- tagID, tagErr := strconv.ParseUint(form.Tags[i], 10, 64)
- if tagErr != nil {
- return tagErr
- }
- tagIDs = append(tagIDs, tagID)
- }
- for i := range tagIDs {
- form.ActiveTags[tagIDs[i]] = true
- }
- if len(validateErrors) == 0 {
- articleModel, err := mapper.ToArticleModel(*form)
- if err != nil {
- logger.Error(ctx, err.Error())
- return err
- }
- var tagsToAdd, tagsToDelete []uint64
- oldTagsMap := make(map[int]struct{}, len(articleTags))
- for i := range articleTags {
- oldTagsMap[int(articleTags[i].ID)] = struct{}{}
- if _, ok := form.ActiveTags[articleTags[i].ID]; !ok {
- tagsToDelete = append(tagsToDelete, articleTags[i].ID)
- }
- }
- for i := range tagIDs {
- if _, ok := oldTagsMap[int(tagIDs[i])]; !ok {
- tagsToAdd = append(tagsToAdd, tagIDs[i])
- }
- }
- err = h.tm.ReadCommitted(ctx, func(ctx context.Context) error {
- txErr := h.articleRepository.Update(ctx, *articleModel)
- if txErr != nil {
- return txErr
- }
- if len(tagsToAdd) > 0 {
- if txErr = h.articleTagRepository.Add(ctx, id, tagsToAdd); txErr != nil {
- return txErr
- }
- }
- if len(tagsToDelete) > 0 {
- if txErr = h.articleTagRepository.Delete(ctx, id, tagsToDelete); 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)
- if fctx.FormValue("action", "save") == "save" {
- return fctx.Redirect("/admin")
- }
- }
- }
- return fctx.Render("admin/article_edit", fiber.Map{
- "section": "article",
- "lang": lang,
- "form": form,
- "errors": validateErrors,
- "tags": tagsDTO,
- "show_apply": true,
- "title": i18n.T(lang, "admin_edit_article_title"),
- }, "admin/_layout")
- }
- }
|