Build the URL once and not overcomplicate stuff

This commit is contained in:
VnPower
2024-06-06 10:04:27 +07:00
parent 1c21b6fa5b
commit 60437bbb01
5 changed files with 34 additions and 36 deletions
+3 -10
View File
@@ -8,24 +8,20 @@ import (
type URLConstructor struct {
path string
hash map[string]string
fragment string
}
func lowercaseFirstChar(s string) string {
return strings.ToLower(s[0:1]) + s[1:]
}
func (obj *URLConstructor) Replace(name, value string) string {
func (obj *URLConstructor) Replace(name string) string {
url := fmt.Sprintf("/%s", obj.path)
first := true
var matchedV string
for k, v := range obj.hash {
k = lowercaseFirstChar(k)
if k == name {
matchedV = value
continue
}
if first {
@@ -44,17 +40,14 @@ func (obj *URLConstructor) Replace(name, value string) string {
} else {
t = "&"
}
url += fmt.Sprintf("%s%s=%s", t, name, matchedV)
url += obj.fragment
url += fmt.Sprintf("%s%s=", t, name)
return url
}
func NewURLConstruct(path string, obj map[string]string, fragment string) URLConstructor {
func NewURLConstruct(path string, obj map[string]string) URLConstructor {
return URLConstructor{
path: path,
hash: obj,
fragment: fragment,
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ func DiscoveryPage(c *fiber.Ctx) error {
return err
}
urlc := site.NewURLConstruct("discovery", map[string]string{"mode": mode}, "#checkpoint")
urlc := site.NewURLConstruct("discovery", map[string]string{"mode": mode})
return c.Render("discovery", fiber.Map{
"Artworks": works,
+9 -9
View File
@@ -4,8 +4,8 @@ import (
"net/url"
"strconv"
core "codeberg.org/vnpower/pixivfe/v2/core/webapi"
site "codeberg.org/vnpower/pixivfe/v2/core/http"
core "codeberg.org/vnpower/pixivfe/v2/core/webapi"
"github.com/gofiber/fiber/v2"
)
@@ -24,13 +24,13 @@ func TagPage(c *fiber.Ctx) error {
// Because of the large amount of queries available for this route,
// I made a struct type just to manage the queries
queries := core.SearchPageSettings {
Name: name,
queries := core.SearchPageSettings{
Name: name,
Category: c.Query("category", "artworks"),
Order: c.Query("order", "date_d"),
Mode: c.Query("mode", "safe"),
Ratio: c.Query("ratio", ""),
Page: page,
Order: c.Query("order", "date_d"),
Mode: c.Query("mode", "safe"),
Ratio: c.Query("ratio", ""),
Page: page,
}
tag, err := core.GetTagData(c, name)
@@ -42,8 +42,8 @@ func TagPage(c *fiber.Ctx) error {
return err
}
urlc := site.NewURLConstruct("tags", queries.ReturnMap(), "")
urlc := site.NewURLConstruct("tags", queries.ReturnMap())
return c.Render("tag", fiber.Map{"Title": "Results for " + name, "Tag": tag, "Data": result, "Queries": queries.ReturnMap(), "TrueTag": param, "Page": pageInt, "URLC": urlc.Replace})
}
+4 -3
View File
@@ -7,9 +7,10 @@
</div>
<div class="switcher">
<div class="switch-title">Filter</div>
<a href='{{URLC("mode", "all")}}' class="switch-button">All</a>
<a href='{{URLC("mode", "safe")}}' class="switch-button">Safe</a>
<a href='{{URLC("mode", "r18")}}' class="switch-button">R-18</a>
{{ URL := URLC("mode") }}
<a href='{{URL}}all' class="switch-button">All</a>
<a href='{{URL}}safe' class="switch-button">Safe</a>
<a href='{{URL}}r18' class="switch-button">R-18</a>
</div>
<div class="artwork-container">
{{ include "components/small-tn" Artworks }}
+17 -13
View File
@@ -29,27 +29,31 @@
<div class="switcher">
<div class="switch-title">Type</div>
<a href='{{URLC("category", "artworks")}}' class="switch-button">All</a>
<a href='{{URLC("category", "illustrations")}}' class="switch-button">Illustrations</a>
<a href='{{URLC("category", "manga")}}' class="switch-button">Manga</a>
{{ URL := URLC("category") }}
<a href='{{URL}}artworks#checkpoint' class="switch-button">All</a>
<a href='{{URL}}illustrations#checkpoint' class="switch-button">Illustrations</a>
<a href='{{URL}}manga#checkpoint' class="switch-button">Manga</a>
</div>
<div class="switcher">
<div class="switch-title">Order</div>
<a href='{{URLC("order", "date_d")}}' class="switch-button">Newest</a>
<a href='{{URLC("order", "date")}}' class="switch-button">Oldest</a>
{{ URL := URLC("order") }}
<a href='{{URL}}date_d#checkpoint' class="switch-button">Newest</a>
<a href='{{URL}}date#checkpoint' class="switch-button">Oldest</a>
</div>
<div class="switcher">
<div class="switch-title">Filter</div>
<a href='{{URLC("mode", "all")}}' class="switch-button">All</a>
<a href='{{URLC("mode", "safe")}}' class="switch-button">Safe</a>
<a href='{{URLC("mode", "r18")}}' class="switch-button">R-18</a>
{{ URL := URLC("mode") }}
<a href='{{URL}}all#checkpoint' class="switch-button">All</a>
<a href='{{URL}}safe#checkpoint' class="switch-button">Safe</a>
<a href='{{URL}}r18#checkpoint' class="switch-button">R-18</a>
</div>
<div class="switcher">
<div class="switch-title">Ratio</div>
<a href='{{URLC("ratio", "")}}' class="switch-button">All</a>
<a href='{{URLC("ratio", "-0.5")}}' class="switch-button">Portrait</a>
<a href='{{URLC("ratio", "0")}}' class="switch-button">Square</a>
<a href='{{URLC("ratio", "0.5")}}' class="switch-button">Landscape</a>
{{ URL := URLC("ratio") }}
<a href='{{URL}}#checkpoint' class="switch-button">All</a>
<a href='{{URL}}-0.5#checkpoint' class="switch-button">Portrait</a>
<a href='{{URL}}0#checkpoint' class="switch-button">Square</a>
<a href='{{URL}}0.5#checkpoint' class="switch-button">Landscape</a>
</div>
{{ if Data.Popular.Recent }}
<h2>Popular artworks</h2>
@@ -70,7 +74,7 @@
</div>
<div class="pagination">
{{ url := URLC("page", "") }}
{{ url := URLC("page") }}
<small>Page {{ Page }}</small>
<br />
{{ raw: createPaginator(url, "#checkpoint", Page, -1) }}