1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package handler
- import (
- "database/sql"
- "errors"
- "git.dmitriygnatenko.ru/dima/go-common/logger"
- "github.com/gofiber/fiber/v2"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/mapper"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
- )
- const (
- TagCacheKey = "tag"
- tagParam = "tag"
- )
- func TagHandler(
- cacheService CacheService,
- articleRepository ArticleRepository,
- tagRepository TagRepository,
- ) fiber.Handler {
- return func(fctx *fiber.Ctx) error {
- ctx := fctx.Context()
- url := fctx.Params(tagParam)
- lang := i18n.Language(fctx)
- renderData, found := cacheService.Get(TagCacheKey + string(lang) + url)
- if !found {
- tag, err := tagRepository.GetByURL(ctx, url)
- if err != nil {
- if errors.Is(err, sql.ErrNoRows) {
- return fiber.ErrNotFound
- }
- logger.Error(ctx, err.Error())
- return err
- }
- articles, err := articleRepository.GetPreviewByTagID(ctx, tag.ID)
- if err != nil {
- logger.Error(ctx, err.Error())
- return err
- }
- renderData = fiber.Map{
- "headTitle": i18n.T(lang, "head_title"),
- "pageTitle": i18n.T(lang, "tag_page_title", tag.Tag),
- "headDescription": i18n.T(lang, "tag_page_desc", tag.Tag),
- "headKeywords": i18n.T(lang, "tag_page_keywords", tag.Tag),
- "articles": mapper.ToArticlePreviewDTOList(fctx, articles),
- }
- cacheService.Set(TagCacheKey+string(lang)+url, renderData, nil)
- }
- return fctx.Render("tag", renderData, "_layout")
- }
- }
|