10
"github.com/go-chi/chi/v5"
11
"xelbot.com/reprogl/container"
12
"xelbot.com/reprogl/models"
13
"xelbot.com/reprogl/models/repositories"
14
"xelbot.com/reprogl/session"
15
"xelbot.com/reprogl/views"
18
func IndexAction(app *container.Application) http.HandlerFunc {
19
return func(w http.ResponseWriter, r *http.Request) {
20
pageStr := chi.URLParam(r, "page")
22
page, needsRedirect := pageOrRedirect(pageStr)
24
http.Redirect(w, r, "/", 301)
30
if user, ok := session.GetIdentity(r.Context()); ok {
31
isAdmin = user.IsAdmin()
34
repo := repositories.ArticleRepository{DB: app.DB}
35
articlesPaginator, err := repo.GetCollection(page, isAdmin)
37
if errors.Is(err, models.RecordNotFound) {
40
app.ServerError(w, err)
46
articlesPaginator.URLGenerator = indexPaginationURLs()
48
tagRepo := repositories.TagRepository{DB: app.DB}
49
err = tagRepo.PopulateTagsToArticles(articlesPaginator.Items)
51
app.ServerError(w, err)
56
templateData := views.NewIndexPageData(articlesPaginator)
57
metaDescription := indexPageDescription(app)
59
templateData.AppendTitle(fmt.Sprintf("Страница %d", page))
60
metaDescription += fmt.Sprintf(" Страница %d", page)
61
templateData.SetCanonical(container.GenerateAbsoluteURL("blog-page", "page", strconv.Itoa(page)))
63
templateData.SetCanonical("https://" + container.GetConfig().Host)
65
templateData.AppendName("description", metaDescription)
67
err = views.WriteTemplateWithContext(r.Context(), w, "index.gohtml", templateData)
69
app.ServerError(w, err)
74
func CategoryAction(app *container.Application) http.HandlerFunc {
75
return func(w http.ResponseWriter, r *http.Request) {
76
slug := chi.URLParam(r, "slug")
78
categoryRepo := repositories.CategoryRepository{DB: app.DB}
79
category, err := categoryRepo.GetBySlug(slug)
81
if errors.Is(err, models.RecordNotFound) {
84
app.ServerError(w, err)
90
pageStr := chi.URLParam(r, "page")
91
page, needsRedirect := pageOrRedirect(pageStr)
93
http.Redirect(w, r, container.GenerateURL("category-first", "slug", slug), http.StatusMovedPermanently)
98
repo := repositories.ArticleRepository{DB: app.DB}
99
articlesPaginator, err := repo.GetCollectionByCategory(category, page)
101
if errors.Is(err, models.RecordNotFound) {
104
app.ServerError(w, err)
110
articlesPaginator.URLGenerator = categoryPaginationURLs(slug)
112
tagRepo := repositories.TagRepository{DB: app.DB}
113
err = tagRepo.PopulateTagsToArticles(articlesPaginator.Items)
115
app.ServerError(w, err)
120
templateData := views.NewCategoryPageData(articlesPaginator, category)
122
templateData.SetCanonical(
123
container.GenerateAbsoluteURL("category", "slug", slug, "page", strconv.Itoa(page)),
126
templateData.SetCanonical(container.GenerateAbsoluteURL("category-first", "slug", slug))
129
err = views.WriteTemplateWithContext(r.Context(), w, "index.gohtml", templateData)
131
app.ServerError(w, err)
136
func TagAction(app *container.Application) http.HandlerFunc {
137
return func(w http.ResponseWriter, r *http.Request) {
138
slug := chi.URLParam(r, "slug")
140
tagRepo := repositories.TagRepository{DB: app.DB}
141
tag, err := tagRepo.GetBySlug(slug)
143
if errors.Is(err, models.RecordNotFound) {
146
app.ServerError(w, err)
152
pageStr := chi.URLParam(r, "page")
153
page, needsRedirect := pageOrRedirect(pageStr)
155
http.Redirect(w, r, container.GenerateURL("tag-first", "slug", slug), http.StatusMovedPermanently)
160
repo := repositories.ArticleRepository{DB: app.DB}
161
articlesPaginator, err := repo.GetCollectionByTag(tag, page)
163
if errors.Is(err, models.RecordNotFound) {
166
app.ServerError(w, err)
172
articlesPaginator.URLGenerator = tagPaginationURLs(slug)
174
err = tagRepo.PopulateTagsToArticles(articlesPaginator.Items)
176
app.ServerError(w, err)
181
templateData := views.NewCategoryPageData(articlesPaginator, tag)
183
templateData.SetCanonical(
184
container.GenerateAbsoluteURL("tag", "slug", slug, "page", strconv.Itoa(page)),
187
templateData.SetCanonical(container.GenerateAbsoluteURL("tag-first", "slug", slug))
190
err = views.WriteTemplateWithContext(r.Context(), w, "index.gohtml", templateData)
192
app.ServerError(w, err)
197
func indexPaginationURLs() models.URLGenerator {
198
return func(page int, dir models.PaginationDirection) string {
200
case models.PaginationNext:
202
case models.PaginationPrev:
210
return container.GenerateURL("blog-page", "page", strconv.Itoa(page))
214
func categoryPaginationURLs(slug string) models.URLGenerator {
215
return paginationURLsWithSlug(slug, "category-first", "category")
218
func tagPaginationURLs(slug string) models.URLGenerator {
219
return paginationURLsWithSlug(slug, "tag-first", "tag")
222
func paginationURLsWithSlug(slug, firstRouteName, othersRouteName string) models.URLGenerator {
223
return func(page int, dir models.PaginationDirection) string {
230
case models.PaginationNext:
232
case models.PaginationPrev:
236
pairs = append(pairs, "slug")
237
pairs = append(pairs, slug)
240
routeName = firstRouteName
242
routeName = othersRouteName
243
pairs = append(pairs, "page")
244
pairs = append(pairs, strconv.Itoa(page))
247
return container.GenerateURL(routeName, pairs...)
251
func indexPageDescription(app *container.Application) string {
252
var metaDescription string
254
key := "meta_description"
256
cache := app.GetStringCache()
257
if value, found := cache.Get(key); found {
260
sysParamsRepo := repositories.SystemParametersRepository{DB: app.DB}
261
metaDescription, _ = sysParamsRepo.FindByKey(key)
264
cache.Set(key, metaDescription, 30*24*3600*time.Second)
266
return metaDescription