|
@@ -13,9 +13,9 @@ import (
|
|
|
|
|
|
const errArticleExists = "Статья с данным URL уже существует"
|
|
|
|
|
|
-func ArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
+func ArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
|
|
|
return func(ctx *fiber.Ctx) error {
|
|
|
- articles, err := app.GetArticleRepository().GetAll(ctx.Context())
|
|
|
+ articles, err := sp.GetArticleRepository().GetAll(ctx.Context())
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -32,7 +32,7 @@ func ArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
+func AddArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
|
|
|
return func(ctx *fiber.Ctx) error {
|
|
|
var validate = validator.New()
|
|
|
validateErrors := make(map[string]string)
|
|
@@ -46,7 +46,7 @@ func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
ActiveTags: make(map[int]bool),
|
|
|
}
|
|
|
|
|
|
- tags, err := app.GetTagRepository().GetAll(ctx.Context())
|
|
|
+ tags, err := sp.GetTagRepository().GetAll(ctx.Context())
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -65,7 +65,7 @@ func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
validateErrors = helpers.FormatValidateErrors(err, trans)
|
|
|
}
|
|
|
|
|
|
- if res, _ := app.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
|
|
|
+ if res, _ := sp.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
|
|
|
validateErrors["ArticleForm.URL"] = errArticleExists
|
|
|
}
|
|
|
|
|
@@ -84,18 +84,18 @@ func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
if len(validateErrors) == 0 {
|
|
|
- articleID, articleErr := app.GetArticleRepository().Add(ctx.Context(), mapper.ConvertArticleFormToModel(form))
|
|
|
+ articleID, articleErr := sp.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 {
|
|
|
+ if err = sp.GetArticleTagRepository().Add(ctx.Context(), articleID, tagIDs); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- app.GetCacheService().FlushAll()
|
|
|
+ sp.GetCacheService().FlushAll()
|
|
|
|
|
|
if err = ctx.Redirect("/admin"); err != nil {
|
|
|
return err
|
|
@@ -114,7 +114,7 @@ func AddArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
+func EditArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
|
|
|
return func(ctx *fiber.Ctx) error {
|
|
|
var validate = validator.New()
|
|
|
validateErrors := make(map[string]string)
|
|
@@ -129,7 +129,7 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- article, err := app.GetArticleRepository().GetByID(ctx.Context(), ID)
|
|
|
+ article, err := sp.GetArticleRepository().GetByID(ctx.Context(), ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -137,12 +137,12 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
return fiber.ErrNotFound
|
|
|
}
|
|
|
|
|
|
- articleTags, err := app.GetTagRepository().GetByArticleID(ctx.Context(), ID)
|
|
|
+ articleTags, err := sp.GetTagRepository().GetByArticleID(ctx.Context(), ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- tags, err := app.GetTagRepository().GetAll(ctx.Context())
|
|
|
+ tags, err := sp.GetTagRepository().GetAll(ctx.Context())
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -172,7 +172,7 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
validateErrors = helpers.FormatValidateErrors(err, trans)
|
|
|
}
|
|
|
|
|
|
- if res, _ := app.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
|
|
|
+ if res, _ := sp.GetArticleRepository().GetByURL(ctx.Context(), form.URL); res != nil {
|
|
|
if res.ID != ID {
|
|
|
validateErrors["ArticleForm.URL"] = errArticleExists
|
|
|
}
|
|
@@ -193,7 +193,7 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
if len(validateErrors) == 0 {
|
|
|
- err = app.GetArticleRepository().Update(ctx.Context(), mapper.ConvertArticleFormToModel(*form))
|
|
|
+ err = sp.GetArticleRepository().Update(ctx.Context(), mapper.ConvertArticleFormToModel(*form))
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -215,18 +215,18 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
}
|
|
|
|
|
|
if len(tagsToAdd) > 0 {
|
|
|
- if err = app.GetArticleTagRepository().Add(ctx.Context(), ID, tagsToAdd); err != nil {
|
|
|
+ if err = sp.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 {
|
|
|
+ if err = sp.GetArticleTagRepository().Delete(ctx.Context(), ID, tagsToDelete); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- app.GetCacheService().FlushAll()
|
|
|
+ sp.GetCacheService().FlushAll()
|
|
|
|
|
|
if ctx.FormValue("action", "save") == "save" {
|
|
|
if err = ctx.Redirect("/admin"); err != nil {
|
|
@@ -247,30 +247,30 @@ func EditArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func DeleteArticleHandler(app interfaces.IApp) fiber.Handler {
|
|
|
+func DeleteArticleHandler(sp interfaces.IServiceProvider) fiber.Handler {
|
|
|
return func(ctx *fiber.Ctx) error {
|
|
|
ID, err := strconv.Atoi(ctx.Params("id"))
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- article, err := app.GetArticleRepository().GetByID(ctx.Context(), ID)
|
|
|
+ article, err := sp.GetArticleRepository().GetByID(ctx.Context(), ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
if ctx.Method() == fiber.MethodPost {
|
|
|
- err = app.GetArticleTagRepository().DeleteByArticleID(ctx.Context(), ID)
|
|
|
+ err = sp.GetArticleTagRepository().DeleteByArticleID(ctx.Context(), ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- err = app.GetArticleRepository().Delete(ctx.Context(), ID)
|
|
|
+ err = sp.GetArticleRepository().Delete(ctx.Context(), ID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- app.GetCacheService().FlushAll()
|
|
|
+ sp.GetCacheService().FlushAll()
|
|
|
|
|
|
if err = ctx.Redirect("/admin"); err != nil {
|
|
|
return err
|