aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvulonkaaz <7442677+vulonkaaz@users.noreply.github.com>2025-05-30 02:29:35 +0200
committervulonkaaz <7442677+vulonkaaz@users.noreply.github.com>2025-05-30 02:29:35 +0200
commit148baefc06c1558776928dd635394dbfdad63da7 (patch)
tree60a4d17c4b0e1308448f616c09b3396429232994
parent9a94241730fcfdc9ee315730c213e0d8add114c7 (diff)
multiple pages
-rw-r--r--controllers/pages.go23
-rw-r--r--router/router.go3
-rw-r--r--views/index.html5
3 files changed, 28 insertions, 3 deletions
diff --git a/controllers/pages.go b/controllers/pages.go
index c5c5306..fd87570 100644
--- a/controllers/pages.go
+++ b/controllers/pages.go
@@ -7,15 +7,34 @@ import (
"log"
)
+const maxThreadsPerPage = 24
+
func ThreadList(c *fiber.Ctx) error {
- var threads []models.Thread
db := database.DB
- if err := db.Select(&threads, "SELECT a.*, (SELECT COUNT(*) FROM \"post\" AS b WHERE b.thread = a.id) AS replies FROM \"post\" AS \"a\" WHERE \"thread\" IS NULL ORDER BY (SELECT c.created_at FROM \"post\" AS c WHERE c.thread = a.id OR c.id = a.id ORDER BY c.created_at DESC LIMIT 1) DESC"); err != nil {
+ var nthread []int
+ if err := db.Select(&nthread, "SELECT COUNT(*) FROM \"post\" WHERE \"thread\" IS NULL"); err != nil {
+ log.Println(err)
+ return c.Status(500).SendString("ERROR")
+ }
+ npage := (nthread[0] + maxThreadsPerPage - 1) / maxThreadsPerPage
+ var pages []int
+ for i := 1; i <= npage; i++ {
+ pages = append(pages, i)
+ }
+
+ page,_ := c.ParamsInt("page")
+ if page < 1 {
+ page = 1
+ }
+ offset := maxThreadsPerPage * (page - 1)
+ var threads []models.Thread
+ if err := db.Select(&threads, "SELECT a.*, (SELECT COUNT(*) FROM \"post\" AS b WHERE b.thread = a.id) AS replies FROM \"post\" AS \"a\" WHERE \"thread\" IS NULL ORDER BY (SELECT c.created_at FROM \"post\" AS c WHERE c.thread = a.id OR c.id = a.id ORDER BY c.created_at DESC LIMIT 1) DESC LIMIT $1 OFFSET $2", maxThreadsPerPage, offset); err != nil {
log.Println(err)
return c.Status(500).SendString("ERROR")
}
return c.Render("index", fiber.Map{
"posts": threads,
+ "pages": pages,
})
}
diff --git a/router/router.go b/router/router.go
index 7845b41..1d84815 100644
--- a/router/router.go
+++ b/router/router.go
@@ -10,7 +10,8 @@ import (
func SetRoutes(app *fiber.App) {
app.Get("/", controllers.ThreadList)
- app.Get("/thread/:id", controllers.Thread)
+ app.Get("/:page<min(0)>", controllers.ThreadList)
+ app.Get("/thread/:id<min(0)>", controllers.Thread)
app.Post("/api/post", rateLimiter, controllers.Publish)
app.Delete("/api/post", controllers.Delete)
}
diff --git a/views/index.html b/views/index.html
index 67045c2..34a6f55 100644
--- a/views/index.html
+++ b/views/index.html
@@ -19,4 +19,9 @@
</article>
{{ end }}
</section>
+ <p>Page:
+ {{ range .pages }}
+ <a href="/{{.}}">[{{.}}]</a>
+ {{ end }}
+ </p>
{{template "partials/canvas" .}}