article_list.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package admin
  2. import (
  3. "git.dmitriygnatenko.ru/dima/go-common/logger"
  4. "github.com/gofiber/fiber/v2"
  5. customErrors "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helper/errors"
  6. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/mapper"
  7. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/service/i18n"
  8. )
  9. type articleListPage struct {
  10. articleRepository ArticleRepository
  11. }
  12. func NewArticleListPageHandler(
  13. articleRepository ArticleRepository,
  14. ) fiber.Handler {
  15. h := articleListPage{
  16. articleRepository: articleRepository,
  17. }
  18. return h.handler()
  19. }
  20. func (h articleListPage) handler() fiber.Handler {
  21. return func(ctx *fiber.Ctx) error {
  22. lang := i18n.LanguageFromContext(ctx)
  23. articles, err := h.articleRepository.GetAll(ctx.Context())
  24. if err != nil {
  25. logger.Error(ctx.Context(), customErrors.Wrap(err, "articles list page: can't get articles").Error())
  26. return err
  27. }
  28. return ctx.Render("admin/article", fiber.Map{
  29. "section": "article",
  30. "lang": lang,
  31. "articles": mapper.ToArticlesList(lang, articles),
  32. }, "admin/_layout")
  33. }
  34. }