12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package handler
- import (
- "database/sql"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/interfaces"
- "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/mapper"
- "github.com/gofiber/fiber/v2"
- )
- const (
- maxArticlesCount = 3
- articleParam = "article"
- articleCacheKey = "article"
- )
- func ArticleHandler(sp interfaces.ServiceProvider) fiber.Handler {
- return func(ctx *fiber.Ctx) error {
- articleReq := ctx.Params(articleParam)
- renderData, found := sp.GetCacheService().Get(articleCacheKey + articleReq)
- if !found {
- article, err := sp.GetArticleRepository().GetByURL(ctx.Context(), articleReq)
- if err != nil {
- if err == sql.ErrNoRows {
- return fiber.ErrNotFound
- }
- return err
- }
- if !article.IsActive {
- return fiber.ErrNotFound
- }
- articleDTO := mapper.ConvertArticleModelToDTO(*article)
- // All used tags
- tags, err := sp.GetTagRepository().GetAllUsed(ctx.Context())
- if err != nil {
- return err
- }
- tagsDTO := mapper.ConvertTagModelsToDTO(tags)
- // Last articles
- articles, err := sp.GetArticleRepository().GetAllPreview(ctx.Context())
- if err != nil {
- return err
- }
- if len(articles) > maxArticlesCount {
- articles = articles[:maxArticlesCount]
- }
- renderData = fiber.Map{
- "headTitle": "От слона к суслику - статьи про PHP, Go, алгоритмы",
- "headDescription": articleDTO.MetaDescription,
- "headKeywords": articleDTO.MetaKeywords,
- "pageTitle": "Статья<br>" + articleDTO.Title,
- "article": articleDTO,
- "sidebarArticles": mapper.ConvertArticlePreviewModelsToDTO(articles),
- "sidebarTags": tagsDTO,
- }
- sp.GetCacheService().Set(articleCacheKey+articleReq, renderData)
- }
- return ctx.Render("article", renderData, "_layout")
- }
- }
|