|
@@ -79,6 +79,10 @@ func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
tagIDs = append(tagIDs, tagID)
|
|
tagIDs = append(tagIDs, tagID)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ for i := range tagIDs {
|
|
|
|
+ form.ActiveTags[tagIDs[i]] = true
|
|
|
|
+ }
|
|
|
|
+
|
|
if len(validateErrors) == 0 {
|
|
if len(validateErrors) == 0 {
|
|
articleID, articleErr := app.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form))
|
|
articleID, articleErr := app.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form))
|
|
if articleErr != nil {
|
|
if articleErr != nil {
|
|
@@ -86,7 +90,6 @@ func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
}
|
|
}
|
|
|
|
|
|
if len(form.Tags) > 0 {
|
|
if len(form.Tags) > 0 {
|
|
-
|
|
|
|
if err = app.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil {
|
|
if err = app.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -98,10 +101,6 @@ func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- for i := range tagIDs {
|
|
|
|
- form.ActiveTags[tagIDs[i]] = true
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
return ctx.Render("admin/article_edit", fiber.Map{
|
|
return ctx.Render("admin/article_edit", fiber.Map{
|
|
@@ -143,11 +142,6 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- form, err := mapper.ConvertArticleModelToForm(*article, articleTags)
|
|
|
|
- if err != nil {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
tags, err := app.GetTagRepository().GetAll(ctx.Context())
|
|
tags, err := app.GetTagRepository().GetAll(ctx.Context())
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
@@ -158,24 +152,97 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- if ctx.Method() == fiber.MethodPost {
|
|
|
|
- if err = ctx.BodyParser(&form); err != nil {
|
|
|
|
|
|
+ var form *models.ArticleForm
|
|
|
|
+ if ctx.Method() == fiber.MethodGet {
|
|
|
|
+ form, err = mapper.ConvertArticleModelToForm(*article, articleTags)
|
|
|
|
+ if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
+ } else if ctx.Method() == fiber.MethodPost {
|
|
|
|
+ form = &models.ArticleForm{
|
|
|
|
+ ID: ID,
|
|
|
|
+ ActiveTags: make(map[int]bool),
|
|
|
|
+ }
|
|
|
|
|
|
- if err = validate.Struct(form); err != nil {
|
|
|
|
|
|
+ if err = ctx.BodyParser(form); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if err = validate.Struct(*form); err != nil {
|
|
validateErrors = helpers.FormatValidateErrors(err, trans)
|
|
validateErrors = helpers.FormatValidateErrors(err, trans)
|
|
}
|
|
}
|
|
|
|
|
|
- // TODO
|
|
|
|
|
|
+ if res, _ := app.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 = app.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 = app.GetArticleTagRepository().Add(ctx.Context(), ID, tagsToAdd); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(tagsToDelete) > 0 {
|
|
|
|
+ if err = app.GetArticleTagRepository().Delete(ctx.Context(), ID, tagsToDelete); err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ app.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{
|
|
return ctx.Render("admin/article_edit", fiber.Map{
|
|
- "form": form,
|
|
|
|
- "errors": validateErrors,
|
|
|
|
- "tags": tagsDTO,
|
|
|
|
- "section": "article",
|
|
|
|
- "title": "Редактирование статьи",
|
|
|
|
|
|
+ "form": form,
|
|
|
|
+ "errors": validateErrors,
|
|
|
|
+ "tags": tagsDTO,
|
|
|
|
+ "show_apply": true,
|
|
|
|
+ "section": "article",
|
|
|
|
+ "title": "Редактирование статьи",
|
|
}, "admin/_layout")
|
|
}, "admin/_layout")
|
|
}
|
|
}
|
|
}
|
|
}
|