Skip to content

Commit

Permalink
Implement caching for artist handler data
Browse files Browse the repository at this point in the history
  • Loading branch information
blackfyre committed Oct 12, 2023
1 parent 8ff0952 commit a6a3c92
Showing 1 changed file with 94 additions and 77 deletions.
171 changes: 94 additions & 77 deletions handlers/artists.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func registerArtists(app *pocketbase.PocketBase) {
page := 1
searchExpression := ""
searchExpressionPresent := false
confirmedHtmxRequest := isHtmxRequest(c)
currentUrl := c.Request().URL.String()
c.Response().Header().Set("HX-Push-Url", currentUrl)

if c.QueryParam("page") != "" {
err := error(nil)
Expand All @@ -44,112 +47,126 @@ func registerArtists(app *pocketbase.PocketBase) {

offset := (page - 1) * limit

data := map[string]any{
"Content": "",
}
cacheKey := "artists:" + strconv.Itoa(offset) + ":" + strconv.Itoa(limit) + ":" + strconv.Itoa(page) + ":" + searchExpression

filter := "published = true"

if searchExpression != "" {
filter = filter + " && name ~ {:searchExpression}"
if confirmedHtmxRequest {
cacheKey = cacheKey + ":htmx"
}

records, err := app.Dao().FindRecordsByFilter(
"artists",
filter,
"+name",
limit,
offset,
dbx.Params{
"searchExpression": searchExpression,
},
)

if err != nil {
return apis.NewBadRequestError("Invalid page", err)
if searchExpressionPresent {
cacheKey = cacheKey + ":search"
}

totalRecords, err := app.Dao().FindRecordsByFilter(
"artists",
filter,
"+name",
0,
0,
dbx.Params{
"searchExpression": searchExpression,
},
)

if err != nil {
return apis.NewBadRequestError("Invalid page", err)
}
if app.Cache().Has(cacheKey) {
html := app.Cache().Get(cacheKey).(string)
return c.HTML(http.StatusOK, html)
} else {

recordsCount := len(totalRecords)
data := map[string]any{
"Content": "",
}

preRendered := []map[string]string{}
filter := "published = true"

for _, m := range records {
if searchExpression != "" {
filter = filter + " && name ~ {:searchExpression}"
}

// TODO: handle aka
records, err := app.Dao().FindRecordsByFilter(
"artists",
filter,
"+name",
limit,
offset,
dbx.Params{
"searchExpression": searchExpression,
},
)

school := m.GetStringSlice("school")
if err != nil {
return apis.NewBadRequestError("Invalid page", err)
}

schoolCollector := []string{}
totalRecords, err := app.Dao().FindRecordsByFilter(
"artists",
filter,
"+name",
0,
0,
dbx.Params{
"searchExpression": searchExpression,
},
)

for _, s := range school {
r, err := app.Dao().FindRecordById("schools", s)
if err != nil {
return apis.NewBadRequestError("Invalid page", err)
}

if err != nil {
log.Print("school not found")
continue
}
recordsCount := len(totalRecords)

schoolCollector = append(schoolCollector, r.GetString("name"))
preRendered := []map[string]string{}

}
for _, m := range records {

row := map[string]string{
"Name": m.GetString("name"),
"Url": artistUrl(m.GetString("slug")),
"Profession": m.GetString("profession"),
"BornDied": normalizedBirthDeathActivity(m),
"Schools": strings.Join(schoolCollector, ", "),
}
// TODO: handle aka

preRendered = append(preRendered, row)
}
school := m.GetStringSlice("school")

data["Content"] = preRendered
data["Count"] = recordsCount
schoolCollector := []string{}

pagination := utils.NewPagination(recordsCount, limit, page, "/artists?q="+searchExpression)
for _, s := range school {
r, err := app.Dao().FindRecordById("schools", s)

data["Pagination"] = pagination.Render()
if err != nil {
log.Print("school not found")
continue
}

html := ""
schoolCollector = append(schoolCollector, r.GetString("name"))

if isHtmxRequest(c) {
blockToRender := "artists:content"
}

row := map[string]string{
"Name": m.GetString("name"),
"Url": artistUrl(m.GetString("slug")),
"Profession": m.GetString("profession"),
"BornDied": normalizedBirthDeathActivity(m),
"Schools": strings.Join(schoolCollector, ", "),
}

if searchExpression != "" || searchExpressionPresent {
blockToRender = "artists:search-results"
preRendered = append(preRendered, row)
}

html, err = renderBlock(blockToRender, data)
} else {
html, err = renderPage("artists", data)
}
data["Content"] = preRendered
data["Count"] = recordsCount

if err != nil {
// or redirect to a dedicated 404 HTML page
return apis.NewNotFoundError("", err)
}
pagination := utils.NewPagination(recordsCount, limit, page, "/artists?q="+searchExpression)

currentUrl := c.Request().URL.String()
data["Pagination"] = pagination.Render()

c.Response().Header().Set("HX-Push-Url", currentUrl)
html := ""

if confirmedHtmxRequest {
blockToRender := "artists:content"

return c.HTML(http.StatusOK, html)
if searchExpression != "" || searchExpressionPresent {
blockToRender = "artists:search-results"
}

html, err = renderBlock(blockToRender, data)
} else {
html, err = renderPage("artists", data)
}

if err != nil {
// or redirect to a dedicated 404 HTML page
return apis.NewNotFoundError("", err)
}

app.Cache().Set(cacheKey, html)

return c.HTML(http.StatusOK, html)
}
})

return nil
Expand Down

0 comments on commit a6a3c92

Please sign in to comment.