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 := mapper.LanguageFromContext(fctx) cacheKey := TagCacheKey + url + string(lang) renderData, found := cacheService.Get(cacheKey) 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, mapper.ToLanguageModel(lang)) if err != nil { logger.Error(ctx, err.Error()) return err } renderData = fiber.Map{ "lang": lang, "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.ToArticlesPreview(lang, articles), } cacheService.Set(cacheKey, renderData, nil) } return fctx.Render("tag", renderData, "_layout") } }