-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
142 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package style | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
|
||
"userstyles.world/handlers/jwt" | ||
"userstyles.world/models" | ||
"userstyles.world/modules/config" | ||
"userstyles.world/modules/storage" | ||
) | ||
|
||
func GetCategory(c *fiber.Ctx) error { | ||
u, _ := jwt.User(c) | ||
|
||
c.Locals("Title", "Categories") | ||
c.Locals("User", u) | ||
|
||
page, err := models.IsValidPage(c.Query("page")) | ||
if err != nil { | ||
c.Locals("Title", "Invalid page size") | ||
return c.Render("err", fiber.Map{}) | ||
} | ||
|
||
count, err := storage.CountStyleCategories() | ||
if err != nil { | ||
c.Locals("Title", "Failed to count categories") | ||
return c.Render("err", fiber.Map{}) | ||
} | ||
|
||
p := models.NewPagination(page, count, "", c.Path()) | ||
if p.OutOfBounds() { | ||
return c.Redirect(p.URL(p.Now), 302) | ||
} | ||
c.Locals("Pagination", p) | ||
|
||
cat, err := storage.GetStyleCategories(page, config.AppPageMaxItems) | ||
if err != nil { | ||
c.Locals("Title", "Failed to find categories") | ||
return c.Render("err", fiber.Map{}) | ||
} | ||
c.Locals("Categories", cat) | ||
|
||
return c.Render("style/category", fiber.Map{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package storage | ||
|
||
import ( | ||
"userstyles.world/modules/database" | ||
) | ||
|
||
// Category represents a single category. | ||
type Category struct { | ||
Category string | ||
Count int | ||
} | ||
|
||
// URL returns a link to a category page. | ||
func (c *Category) URL() string { | ||
return "/search?category=" + c.Category | ||
} | ||
|
||
// Categories is an alias for a slice of Category structs. | ||
type Categories []*Category | ||
|
||
// GetStyleCategories returns a slice of categories for category page. | ||
func GetStyleCategories(page, size int) (c Categories, err error) { | ||
offset := (page - 1) * size | ||
|
||
err = database.Conn. | ||
Select("LOWER(category) AS category, COUNT(LOWER(category)) AS count"). | ||
Table("styles"). | ||
Group("LOWER(category)"). | ||
Order("count DESC"). | ||
Offset(offset). | ||
Limit(size). | ||
Find(&c, notDeleted). | ||
Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return c, err | ||
} | ||
|
||
// CountStyleCategories returns a count of unique categories for pagination. | ||
func CountStyleCategories() (i int, err error) { | ||
err = database.Conn. | ||
Select("COUNT(DISTINCT LOWER(category))"). | ||
Table("styles"). | ||
Where(notDeleted). | ||
Scan(&i). | ||
Error | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return i, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<section class="ta:c"> | ||
<h1>{{ .Title }}</h1> | ||
<p class="fg:3">Browse all available categories.</p> | ||
</section> | ||
|
||
<section> | ||
{{ if .Categories }} | ||
<div class="grid flex rwrap mx:r mt:m"> | ||
{{ range .Categories }} | ||
<div class="card col gap"> | ||
<div class="card-header p:m"> | ||
<a href="{{ .URL }}">{{ .Category }}</a> | ||
</div> | ||
<div class="card-footer p:m"> | ||
{{ .Count }} userstyle{{ if gt .Count 1 }}s{{ end }} | ||
</div> | ||
</div> | ||
{{ end }} | ||
</div> | ||
{{ else }} | ||
<p class="ta:c">No categories found.</p> | ||
{{ end }} | ||
</section> | ||
|
||
{{ if .Pagination.Show }} | ||
{{ template "partials/pagination" .Pagination }} | ||
{{ end }} |