package admin import ( "context" "strconv" "github.com/go-playground/validator/v10" "github.com/gofiber/fiber/v2" "github.com/golang-jwt/jwt/v4" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helpers" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/mapper" "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models" ) type ( CacheService interface { FlushAll() } ArticleRepository interface { GetAll(ctx context.Context) ([]models.Article, error) GetByURL(ctx context.Context, url string) (*models.Article, error) GetByID(ctx context.Context, ID int) (*models.Article, error) Add(ctx context.Context, m models.Article) (int, error) Update(ctx context.Context, m models.Article) error Delete(ctx context.Context, ID int) error } TagRepository interface { GetAll(ctx context.Context) ([]models.Tag, error) IsUsed(ctx context.Context, ID int) (bool, error) GetByArticleID(ctx context.Context, ID int) ([]models.Tag, error) GetByURL(ctx context.Context, tag string) (*models.Tag, error) GetByID(ctx context.Context, ID int) (*models.Tag, error) Add(ctx context.Context, m models.Tag) error Update(ctx context.Context, m models.Tag) error Delete(ctx context.Context, ID int) error } ArticleTagRepository interface { Add(ctx context.Context, articleID int, tagIDs []int) error Delete(ctx context.Context, articleID int, tagIDs []int) error DeleteByArticleID(ctx context.Context, articleID int) error } AuthService interface { GeneratePasswordHash(password string) (string, error) IsCorrectPassword(password string, hash string) bool GenerateToken(user models.User) (string, error) GetClaims(fctx *fiber.Ctx) jwt.MapClaims } EnvService interface { JWTCookie() string JWTLifetime() int } UserRepository interface { Get(ctx context.Context, username string) (*models.User, error) Add(ctx context.Context, username string, password string) (int, error) UpdatePassword(ctx context.Context, id int, newPassword string) error } ) const errArticleExists = "Статья с данным URL уже существует" func ArticleHandler(articleRepository ArticleRepository) fiber.Handler { return func(ctx *fiber.Ctx) error { articles, err := articleRepository.GetAll(ctx.Context()) if err != nil { return err } return ctx.Render("admin/article", fiber.Map{ "articles": mapper.ToArticleDTOList(articles), "section": "article", }, "admin/_layout") } } func AddArticleHandler( articleRepository ArticleRepository, tagRepository TagRepository, articleTagRepository ArticleTagRepository, cacheService CacheService, ) fiber.Handler { return func(fctx *fiber.Ctx) error { ctx := fctx.Context() 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 := tagRepository.GetAll(ctx) if err != nil { return err } tagsDTO := mapper.ToTagDTOList(tags) if fctx.Method() == fiber.MethodPost { if err = fctx.BodyParser(&form); err != nil { return err } if err = validate.Struct(form); err != nil { validateErrors = helpers.FormatValidateErrors(err, trans) } if res, _ := articleRepository.GetByURL(ctx, 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 { articleModel, err := mapper.ToArticle(form) if err != nil { return err } articleID, articleErr := articleRepository.Add(ctx, *articleModel) if articleErr != nil { return articleErr } if len(form.Tags) > 0 { if err = articleTagRepository.Add(ctx, articleID, tagIDs); err != nil { return err } } cacheService.FlushAll() return fctx.Redirect("/admin") } } return fctx.Render("admin/article_edit", fiber.Map{ "form": form, "errors": validateErrors, "tags": tagsDTO, "section": "article", "title": "Добавление статьи", "show_apply": false, }, "admin/_layout") } } func EditArticleHandler( articleRepository ArticleRepository, tagRepository TagRepository, articleTagRepository ArticleTagRepository, cacheService CacheService, ) fiber.Handler { return func(fctx *fiber.Ctx) error { ctx := fctx.Context() var validate = validator.New() validateErrors := make(map[string]string) trans, err := helpers.GetDefaultTranslator(validate) if err != nil { return err } ID, err := strconv.Atoi(fctx.Params("id")) if err != nil { return err } article, err := articleRepository.GetByID(ctx, ID) if err != nil { return err } if article == nil { return fiber.ErrNotFound } articleTags, err := tagRepository.GetByArticleID(ctx, ID) if err != nil { return err } tags, err := tagRepository.GetAll(ctx) if err != nil { return err } tagsDTO := mapper.ToTagDTOList(tags) var form *models.ArticleForm if fctx.Method() == fiber.MethodGet { form = mapper.ToArticleForm(*article, articleTags) } else if fctx.Method() == fiber.MethodPost { form = &models.ArticleForm{ ID: ID, ActiveTags: make(map[int]bool), } if err = fctx.BodyParser(form); err != nil { return err } if err = validate.Struct(*form); err != nil { validateErrors = helpers.FormatValidateErrors(err, trans) } if res, _ := articleRepository.GetByURL(ctx, 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 { articleModel, err := mapper.ToArticle(*form) if err != nil { return err } err = articleRepository.Update(ctx, *articleModel) 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 = articleTagRepository.Add(ctx, ID, tagsToAdd); err != nil { return err } } if len(tagsToDelete) > 0 { if err = articleTagRepository.Delete(ctx, ID, tagsToDelete); err != nil { return err } } cacheService.FlushAll() if fctx.FormValue("action", "save") == "save" { return fctx.Redirect("/admin") } } } return fctx.Render("admin/article_edit", fiber.Map{ "form": form, "errors": validateErrors, "tags": tagsDTO, "show_apply": true, "section": "article", "title": "Редактирование статьи", }, "admin/_layout") } } func DeleteArticleHandler( articleRepository ArticleRepository, articleTagRepository ArticleTagRepository, cacheService CacheService, ) fiber.Handler { return func(fctx *fiber.Ctx) error { ctx := fctx.Context() ID, err := strconv.Atoi(fctx.Params("id")) if err != nil { return err } article, err := articleRepository.GetByID(ctx, ID) if err != nil { return err } if fctx.Method() == fiber.MethodPost { err = articleTagRepository.DeleteByArticleID(ctx, ID) if err != nil { return err } err = articleRepository.Delete(ctx, ID) if err != nil { return err } cacheService.FlushAll() return fctx.Redirect("/admin") } return fctx.Render("admin/article_delete", fiber.Map{ "article": article.Title, "section": "article", }, "admin/_layout") } }