add pagination
This commit is contained in:
31
src/main.go
31
src/main.go
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user