add pagination

This commit is contained in:
2026-02-27 16:01:07 +01:00
parent cf264c04fc
commit 1b2f2bb942
3 changed files with 52 additions and 13 deletions

View File

@@ -2,9 +2,11 @@ package main
import (
"fmt"
"html/template"
"log"
"os"
"path/filepath"
"strconv"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
@@ -31,6 +33,10 @@ func main() {
router := gin.Default()
router.MaxMultipartMemory = 10 << 30
router.SetFuncMap(template.FuncMap{
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
})
router.LoadHTMLGlob("templates/*")
var staticPath = gin.Dir("./static", false)
fmt.Printf("Static path: %v\n", staticPath)
@@ -49,8 +55,29 @@ func main() {
admin.GET("/", func(c *gin.Context) {
var files []FileRecord
db.Order("created_at desc").Find(&files)
c.HTML(200, "admin.html", gin.H{"Files": files})
// Pagination parameters
perPage := 20
page := 1
if p := c.Query("page"); p != "" {
if v, err := strconv.Atoi(p); err == nil && v > 0 {
page = v
}
}
var total int64
db.Model(&FileRecord{}).Count(&total)
totalPages := int((total + int64(perPage) - 1) / int64(perPage)) // ceiling division
offset := (page - 1) * perPage
db.Order("created_at desc").Limit(perPage).Offset(offset).Find(&files)
c.HTML(200, "admin.html", gin.H{
"Files": files,
"Page": page,
"TotalPages": totalPages,
})
})
admin.POST("/delete/:id", func(c *gin.Context) {