123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package handler
- //go:generate mkdir -p mocks
- //go:generate rm -rf ./mocks/*_minimock.go
- //go:generate minimock -i CacheService,ArticleRepository,TagRepository -o ./mocks/ -s "_minimock.go"
- import (
- "context"
- "time"
- "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/models"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
- )
- type (
- CacheService interface {
- Get(key string) (interface{}, bool)
- Set(key string, value interface{}, expiration *time.Duration)
- }
- ArticleRepository interface {
- GetByURL(ctx context.Context, url string) (*models.Article, error)
- GetAllPreview(ctx context.Context) ([]models.ArticlePreview, error)
- GetPreviewByTagID(ctx context.Context, tagID uint64) ([]models.ArticlePreview, error)
- }
- TagRepository interface {
- GetAllUsed(ctx context.Context) ([]models.Tag, error)
- GetByURL(ctx context.Context, tag string) (*models.Tag, error)
- }
- )
- const allPreviewArticlesCacheKey = "all-preview-articles"
- func MainPageHandler(
- cacheService CacheService,
- articleRepository ArticleRepository,
- ) fiber.Handler {
- return func(fctx *fiber.Ctx) error {
- ctx := fctx.Context()
- lang := i18n.Language(fctx)
- renderData, found := cacheService.Get(allPreviewArticlesCacheKey + string(lang))
- if !found {
- articles, err := articleRepository.GetAllPreview(fctx.Context())
- if err != nil {
- logger.Error(ctx, err.Error())
- return err
- }
- renderData = fiber.Map{
- "headTitle": i18n.T(lang, "head_title"),
- "pageTitle": i18n.T(lang, "main_page_title"),
- "headDescription": i18n.T(lang, "main_page_desc"),
- "headKeywords": i18n.T(lang, "main_page_keywords"),
- "articles": mapper.ToArticlePreviewDTOList(fctx, articles),
- }
- cacheService.Set(allPreviewArticlesCacheKey+string(lang), renderData, nil)
- }
- return fctx.Render("index", renderData, "_layout")
- }
- }
|