package admin import ( "strconv" "github.com/dmitriygnatenko/internal/helpers" "github.com/dmitriygnatenko/internal/interfaces" "github.com/dmitriygnatenko/internal/mapper" "github.com/dmitriygnatenko/internal/models" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" ) const errArticleExists = "Статья с данным URL уже существует" func ArticleHandler(sp interfaces.ServiceProvider) fiber.Handler { return func(ctx *fiber.Ctx) error { articles, err := sp.GetArticleRepository().GetAll(ctx.Context()) if err != nil { return err } articlesDTO, err := mapper.ConvertArticleModelsToDTO(articles) if err != nil { return err } return ctx.Render("admin/article", fiber.Map{ "articles": articlesDTO, "section": "article", }, "admin/_layout") } } func AddArticleHandler(sp interfaces.ServiceProvider) fiber.Handler { return func(ctx *fiber.Ctx) error { var validate = validator.New() validateErrors := make(map[string]string) trans, err := helpers.GetDefaultTranslator(validate) if err != nil { return err } form := models.ArticleForm{ ActiveTags: make(map[int]bool), } tags, err := sp.GetTagRepository().GetAll(ctx.Context()) if err != nil { return err } tagsDTO := mapper.ConvertTagModelsToDTO(tags) if ctx.Method() == fiber.MethodPost { if err = ctx.BodyParser(&form); err != nil { return err } if err = validate.Struct(form); err != nil { validateErrors = helpers.FormatValidateErrors(err, trans) } if res, _ := sp.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil { validateErrors["ArticleForm.URL"] = errArticleExists } tagIDs := make([]int, 0, len(form.Tags)) for i := range form.Tags { tagID, tagErr := strconv.Atoi(form.Tags[i]) if tagErr != nil { return tagErr } tagIDs = append(tagIDs, tagID) } for i := range tagIDs { form.ActiveTags[tagIDs[i]] = true } if len(validateErrors) == 0 { articleID, articleErr := sp.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form)) if articleErr != nil { return articleErr } if len(form.Tags) > 0 { if err = sp.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil { return err } } sp.GetCacheService().FlushAll() if err = ctx.Redirect("/admin"); err != nil { return err } } } return ctx.Render("admin/article_edit", fiber.Map{ "form": form, "errors": validateErrors, "tags": tagsDTO, "section": "article", "title": "Добавление статьи", "show_apply": false, }, "admin/_layout") } } func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler { return func(ctx *fiber.Ctx) error { var validate = validator.New() validateErrors := make(map[string]string) trans, err := helpers.GetDefaultTranslator(validate) if err != nil { return err } ID, err := strconv.Atoi(ctx.Params("id")) if err != nil { return err } article, err := sp.GetArticleRepository().GetByID(ctx.Context(), ID) if err != nil { return err } if article == nil { return fiber.ErrNotFound } articleTags, err := sp.GetTagRepository().GetByArticleID(ctx.Context(), ID) if err != nil { return err } tags, err := sp.GetTagRepository().GetAll(ctx.Context()) if err != nil { return err } tagsDTO := mapper.ConvertTagModelsToDTO(tags) var form *models.ArticleForm if ctx.Method() == fiber.MethodGet { form, err = mapper.ConvertArticleModelToForm(*article, articleTags) if err != nil { return err } } else if ctx.Method() == fiber.MethodPost { form = &models.ArticleForm{ ID: ID, ActiveTags: make(map[int]bool), } if err = ctx.BodyParser(form); err != nil { return err } if err = validate.Struct(*form); err != nil { validateErrors = helpers.FormatValidateErrors(err, trans) } if res, _ := sp.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil { if res.ID != ID { validateErrors["ArticleForm.URL"] = errArticleExists } } tagIDs := make([]int, 0, len(form.Tags)) for i := range form.Tags { tagID, tagErr := strconv.Atoi(form.Tags[i]) if tagErr != nil { return tagErr } tagIDs = append(tagIDs, tagID) } for i := range tagIDs { form.ActiveTags[tagIDs[i]] = true } if len(validateErrors) == 0 { err = sp.GetArticleRepository().Update(ctx.Context(), mapper.ConvertArticleFormToModel(*form)) if err != nil { return err } var tagsToAdd, tagsToDelete []int oldTagsMap := make(map[int]struct{}, len(articleTags)) for i := range articleTags { oldTagsMap[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[tagIDs[i]]; !ok { tagsToAdd = append(tagsToAdd, tagIDs[i]) } } if len(tagsToAdd) > 0 { if err = sp.GetArticleTagRepository().Add(ctx.Context(), ID, tagsToAdd); err != nil { return err } } if len(tagsToDelete) > 0 { if err = sp.GetArticleTagRepository().Delete(ctx.Context(), ID, tagsToDelete); err != nil { return err } } sp.GetCacheService().FlushAll() if ctx.FormValue("action", "save") == "save" { if err = ctx.Redirect("/admin"); err != nil { return err } } } } return ctx.Render("admin/article_edit", fiber.Map{ "form": form, "errors": validateErrors, "tags": tagsDTO, "show_apply": true, "section": "article", "title": "Редактирование статьи", }, "admin/_layout") } } func DeleteArticleHandler(sp interfaces.ServiceProvider) fiber.Handler { return func(ctx *fiber.Ctx) error { ID, err := strconv.Atoi(ctx.Params("id")) if err != nil { return err } article, err := sp.GetArticleRepository().GetByID(ctx.Context(), ID) if err != nil { return err } if ctx.Method() == fiber.MethodPost { err = sp.GetArticleTagRepository().DeleteByArticleID(ctx.Context(), ID) if err != nil { return err } err = sp.GetArticleRepository().Delete(ctx.Context(), ID) if err != nil { return err } sp.GetCacheService().FlushAll() if err = ctx.Redirect("/admin"); err != nil { return err } } return ctx.Render("admin/article_delete", fiber.Map{ "article": article.Title, "section": "article", }, "admin/_layout") } }