|
@@ -6,10 +6,13 @@ import (
|
|
"github.com/dmitriygnatenko/internal/helpers"
|
|
"github.com/dmitriygnatenko/internal/helpers"
|
|
"github.com/dmitriygnatenko/internal/interfaces"
|
|
"github.com/dmitriygnatenko/internal/interfaces"
|
|
"github.com/dmitriygnatenko/internal/mapper"
|
|
"github.com/dmitriygnatenko/internal/mapper"
|
|
|
|
+ "github.com/dmitriygnatenko/internal/models"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+const errArticleExists = "Статья с данным URL уже существует"
|
|
|
|
+
|
|
func ArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
func ArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
return func(ctx *fiber.Ctx) error {
|
|
return func(ctx *fiber.Ctx) error {
|
|
articles, err := app.GetArticleRepository().GetAll(ctx.Context())
|
|
articles, err := app.GetArticleRepository().GetAll(ctx.Context())
|
|
@@ -31,7 +34,84 @@ func ArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
|
|
|
func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
return func(ctx *fiber.Ctx) error {
|
|
return func(ctx *fiber.Ctx) error {
|
|
- return nil
|
|
|
|
|
|
+ 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 := app.GetTagRepository().GetAll(ctx.Context())
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ tagsDTO, err := mapper.ConvertTagModelsToDTO(tags)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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, _ := app.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)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(validateErrors) == 0 {
|
|
|
|
+ articleID, articleErr := app.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form))
|
|
|
|
+ if articleErr != nil {
|
|
|
|
+ return articleErr
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(form.Tags) > 0 {
|
|
|
|
+
|
|
|
|
+ if err = app.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ app.GetCacheService().FlushAll()
|
|
|
|
+
|
|
|
|
+ if err = ctx.Redirect("/admin"); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for i := range tagIDs {
|
|
|
|
+ form.ActiveTags[tagIDs[i]] = true
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return ctx.Render("admin/article_edit", fiber.Map{
|
|
|
|
+ "form": form,
|
|
|
|
+ "errors": validateErrors,
|
|
|
|
+ "tags": tagsDTO,
|
|
|
|
+ "section": "article",
|
|
|
|
+ "title": "Добавление статьи",
|
|
|
|
+ "show_apply": false,
|
|
|
|
+ }, "admin/_layout")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|