Skip to content

Commit

Permalink
feat(styles): implement pagination on Explore page
Browse files Browse the repository at this point in the history
The HTML/CSS will be added in the next commit.
  • Loading branch information
vednoc committed Jul 18, 2021
1 parent 9561f2c commit 17f78a9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
40 changes: 35 additions & 5 deletions handlers/style/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package style

import (
"sort"
"strconv"

"github.com/gofiber/fiber/v2"

Expand All @@ -11,8 +12,33 @@ import (

func GetExplore(c *fiber.Ctx) error {
u, _ := jwt.User(c)
page := c.Query("page")

s, err := models.GetAllAvailableStyles()
var pageNow int
if page != "" {
i, err := strconv.Atoi(page)
if err != nil {
return c.Render("err", fiber.Map{
"Title": "Invalid page size",
"User": u,
})
}
pageNow = i
} else {
pageNow = 1
}

styleCount, err := models.GetStyleCount()
if err != nil {
return c.Render("err", fiber.Map{
"Title": "Failed to add pagination",
"User": u,
})
}

maxPages := styleCount / 50

s, err := models.GetAllAvailableStylesPaginated(pageNow)
if err != nil {
return c.Render("err", fiber.Map{
"Title": "Styles not found",
Expand Down Expand Up @@ -45,9 +71,13 @@ func GetExplore(c *fiber.Ctx) error {
}

return c.Render("core/explore", fiber.Map{
"Title": "Explore",
"User": u,
"Styles": s,
"Sort": fv,
"Title": "Explore",
"User": u,
"Styles": s,
"Sort": fv,
"PageMax": maxPages,
"PageNow": pageNow,
"PageBack": pageNow - 1,
"PageNext": pageNow + 1,
})
}
33 changes: 33 additions & 0 deletions models/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,39 @@ func GetAllStylesForIndexAPI() (*[]APIStyle, error) {
return q, nil
}

func GetStyleCount() (i int64, err error) {
if err := database.Conn.Select("count(id)").Model(Style{}).Count(&i).Error; err != nil {
return 0, err
}

return i, nil
}

func GetAllAvailableStylesPaginated(page int) ([]StyleCard, error) {
q := new([]StyleCard)
size := 50
offset := (page - 1) * size

s1 := "styles.id, styles.name, styles.created_at, styles.updated_at, styles.preview, u.username, u.display_name, "
s2 := "(select count(id) from stats s where s.style_id = styles.id and s.install > 0) installs, "
s3 := "(select count(id) from stats s where s.style_id = styles.id and s.view > 0) views"
stmt := s1 + s2 + s3

err := getDBSession().
Select(stmt).
Model(Style{}).
Joins("join users u on u.id = styles.user_id").
Offset(offset).
Limit(size).
Find(q).Error

if err != nil {
return nil, err
}

return *q, nil
}

func GetAllAvailableStyles() ([]StyleCard, error) {
q := new([]StyleCard)
stmt := `
Expand Down
6 changes: 6 additions & 0 deletions views/core/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ <h1>Explore</h1>
<p class="ta:c">No userstyles found.</p>
{{ end }}
</section>

<section class="Pagination mt:m mb:m flex jc:c ai:c">
<div>
back: {{ .PageBack }} / now: {{ .PageNow }} next: {{ .PageNext }} / max: {{ .PageMax }}
</div>
</section>
</div>

0 comments on commit 17f78a9

Please sign in to comment.