|
@@ -28,7 +28,8 @@ func ArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
func AddArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
- return func(ctx *fiber.Ctx) error {
|
|
|
+ return func(fctx *fiber.Ctx) error {
|
|
|
+ ctx := fctx.Context()
|
|
|
var validate = validator.New()
|
|
|
validateErrors := make(map[string]string)
|
|
|
|
|
@@ -41,15 +42,15 @@ func AddArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
ActiveTags: make(map[int]bool),
|
|
|
}
|
|
|
|
|
|
- tags, err := sp.GetTagRepository().GetAll(ctx.Context())
|
|
|
+ tags, err := sp.GetTagRepository().GetAll(ctx)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
tagsDTO := mapper.ConvertTagModelsToDTO(tags)
|
|
|
|
|
|
- if ctx.Method() == fiber.MethodPost {
|
|
|
- if err = ctx.BodyParser(&form); err != nil {
|
|
|
+ if fctx.Method() == fiber.MethodPost {
|
|
|
+ if err = fctx.BodyParser(&form); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
@@ -57,7 +58,7 @@ func AddArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
validateErrors = helpers.FormatValidateErrors(err, trans)
|
|
|
}
|
|
|
|
|
|
- if res, _ := sp.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
|
|
|
+ if res, _ := sp.GetArticleRepository().GetByURL(ctx, form.URL); res != nil {
|
|
|
validateErrors["ArticleForm.URL"] = errArticleExists
|
|
|
}
|
|
|
|
|
@@ -76,26 +77,29 @@ func AddArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
if len(validateErrors) == 0 {
|
|
|
- articleID, articleErr := sp.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form))
|
|
|
+ articleModel, err := mapper.ConvertArticleFormToModel(form)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ articleID, articleErr := sp.GetArticleRepository().Add(ctx, *articleModel)
|
|
|
if articleErr != nil {
|
|
|
return articleErr
|
|
|
}
|
|
|
|
|
|
if len(form.Tags) > 0 {
|
|
|
- if err = sp.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil {
|
|
|
+ if err = sp.GetArticleTagRepository().Add(ctx, articleID, tagIDs); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sp.GetCacheService().FlushAll()
|
|
|
|
|
|
- if err = ctx.Redirect("/admin"); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+ return fctx.Redirect("/admin")
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return ctx.Render("admin/article_edit", fiber.Map{
|
|
|
+ return fctx.Render("admin/article_edit", fiber.Map{
|
|
|
"form": form,
|
|
|
"errors": validateErrors,
|
|
|
"tags": tagsDTO,
|
|
@@ -107,7 +111,8 @@ func AddArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
- return func(ctx *fiber.Ctx) error {
|
|
|
+ return func(fctx *fiber.Ctx) error {
|
|
|
+ ctx := fctx.Context()
|
|
|
var validate = validator.New()
|
|
|
validateErrors := make(map[string]string)
|
|
|
|
|
@@ -116,12 +121,12 @@ func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- ID, err := strconv.Atoi(ctx.Params("id"))
|
|
|
+ ID, err := strconv.Atoi(fctx.Params("id"))
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- article, err := sp.GetArticleRepository().GetByID(ctx.Context(), ID)
|
|
|
+ article, err := sp.GetArticleRepository().GetByID(ctx, ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -129,12 +134,12 @@ func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
return fiber.ErrNotFound
|
|
|
}
|
|
|
|
|
|
- articleTags, err := sp.GetTagRepository().GetByArticleID(ctx.Context(), ID)
|
|
|
+ articleTags, err := sp.GetTagRepository().GetByArticleID(ctx, ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- tags, err := sp.GetTagRepository().GetAll(ctx.Context())
|
|
|
+ tags, err := sp.GetTagRepository().GetAll(ctx)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -142,15 +147,15 @@ func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
tagsDTO := mapper.ConvertTagModelsToDTO(tags)
|
|
|
|
|
|
var form *models.ArticleForm
|
|
|
- if ctx.Method() == fiber.MethodGet {
|
|
|
+ if fctx.Method() == fiber.MethodGet {
|
|
|
form = mapper.ConvertArticleModelToForm(*article, articleTags)
|
|
|
- } else if ctx.Method() == fiber.MethodPost {
|
|
|
+ } else if fctx.Method() == fiber.MethodPost {
|
|
|
form = &models.ArticleForm{
|
|
|
ID: ID,
|
|
|
ActiveTags: make(map[int]bool),
|
|
|
}
|
|
|
|
|
|
- if err = ctx.BodyParser(form); err != nil {
|
|
|
+ if err = fctx.BodyParser(form); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
@@ -158,7 +163,7 @@ func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
validateErrors = helpers.FormatValidateErrors(err, trans)
|
|
|
}
|
|
|
|
|
|
- if res, _ := sp.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
|
|
|
+ if res, _ := sp.GetArticleRepository().GetByURL(ctx, form.URL); res != nil {
|
|
|
if res.ID != ID {
|
|
|
validateErrors["ArticleForm.URL"] = errArticleExists
|
|
|
}
|
|
@@ -179,7 +184,12 @@ func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
if len(validateErrors) == 0 {
|
|
|
- err = sp.GetArticleRepository().Update(ctx.Context(), mapper.ConvertArticleFormToModel(*form))
|
|
|
+ articleModel, err := mapper.ConvertArticleFormToModel(*form)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = sp.GetArticleRepository().Update(ctx, *articleModel)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -201,28 +211,26 @@ func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
if len(tagsToAdd) > 0 {
|
|
|
- if err = sp.GetArticleTagRepository().Add(ctx.Context(), ID, tagsToAdd); err != nil {
|
|
|
+ if err = sp.GetArticleTagRepository().Add(ctx, ID, tagsToAdd); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if len(tagsToDelete) > 0 {
|
|
|
- if err = sp.GetArticleTagRepository().Delete(ctx.Context(), ID, tagsToDelete); err != nil {
|
|
|
+ if err = sp.GetArticleTagRepository().Delete(ctx, ID, tagsToDelete); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
sp.GetCacheService().FlushAll()
|
|
|
|
|
|
- if ctx.FormValue("action", "save") == "save" {
|
|
|
- if err = ctx.Redirect("/admin"); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+ if fctx.FormValue("action", "save") == "save" {
|
|
|
+ return fctx.Redirect("/admin")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return ctx.Render("admin/article_edit", fiber.Map{
|
|
|
+ return fctx.Render("admin/article_edit", fiber.Map{
|
|
|
"form": form,
|
|
|
"errors": validateErrors,
|
|
|
"tags": tagsDTO,
|
|
@@ -234,36 +242,35 @@ func EditArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
func DeleteArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
|
|
|
- return func(ctx *fiber.Ctx) error {
|
|
|
- ID, err := strconv.Atoi(ctx.Params("id"))
|
|
|
+ return func(fctx *fiber.Ctx) error {
|
|
|
+ ctx := fctx.Context()
|
|
|
+ ID, err := strconv.Atoi(fctx.Params("id"))
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- article, err := sp.GetArticleRepository().GetByID(ctx.Context(), ID)
|
|
|
+ article, err := sp.GetArticleRepository().GetByID(ctx, ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- if ctx.Method() == fiber.MethodPost {
|
|
|
- err = sp.GetArticleTagRepository().DeleteByArticleID(ctx.Context(), ID)
|
|
|
+ if fctx.Method() == fiber.MethodPost {
|
|
|
+ err = sp.GetArticleTagRepository().DeleteByArticleID(ctx, ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- err = sp.GetArticleRepository().Delete(ctx.Context(), ID)
|
|
|
+ err = sp.GetArticleRepository().Delete(ctx, ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
sp.GetCacheService().FlushAll()
|
|
|
|
|
|
- if err = ctx.Redirect("/admin"); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+ return fctx.Redirect("/admin")
|
|
|
}
|
|
|
|
|
|
- return ctx.Render("admin/article_delete", fiber.Map{
|
|
|
+ return fctx.Render("admin/article_delete", fiber.Map{
|
|
|
"article": article.Title,
|
|
|
"section": "article",
|
|
|
}, "admin/_layout")
|