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 ( import (
"fmt" "fmt"
"html/template"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@@ -31,6 +33,10 @@ func main() {
router := gin.Default() router := gin.Default()
router.MaxMultipartMemory = 10 << 30 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/*") router.LoadHTMLGlob("templates/*")
var staticPath = gin.Dir("./static", false) var staticPath = gin.Dir("./static", false)
fmt.Printf("Static path: %v\n", staticPath) fmt.Printf("Static path: %v\n", staticPath)
@@ -49,8 +55,29 @@ func main() {
admin.GET("/", func(c *gin.Context) { admin.GET("/", func(c *gin.Context) {
var files []FileRecord 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) { admin.POST("/delete/:id", func(c *gin.Context) {

View File

@@ -17,9 +17,10 @@
.status-deleted { background: #ffcccc; text-decoration: line-through; } .status-deleted { background: #ffcccc; text-decoration: line-through; }
.status-no{ background: #ffcccc; } .status-no{ background: #ffcccc; }
.status-active { background: #ccffcc; } .status-active { background: #ccffcc; }
button { border: 1px solid #000; background: #eee; padding: 2px 8px; cursor: pointer; font-size: 11px; font-weight: bold; } button, .button { border: 1px solid #000; background: #eee; padding: 2px 8px; cursor: pointer; font-size: 11px; font-weight: bold; text-decoration: none; }
button:hover { background: #000; color: #fff; } button:hover, .button:hover { background: #000; color: #fff; }
.nav-link { font-weight: bold; text-decoration: underline; margin-bottom: 20px; display: inline-block; } .nav-link { font-weight: bold; text-decoration: underline; margin-bottom: 20px; display: inline-block; }
.pagination a { margin: 0 2px; }
</style> </style>
</head> </head>
<body> <body>
@@ -27,8 +28,10 @@
<div class="max-w-6xl mx-auto"> <div class="max-w-6xl mx-auto">
<header class="mb-8 border-b-4 border-black pb-2 flex justify-between items-end"> <header class="mb-8 border-b-4 border-black pb-2 flex justify-between items-end">
<h1 class="text-3xl font-black uppercase tracking-tighter">System Console</h1> <h1 class="text-3xl font-black uppercase tracking-tighter">System Console</h1>
<div>
<a href="/" class="nav-link text-xs">← BACK TO UPLOADER</a> <a href="/" class="nav-link text-xs">← BACK TO UPLOADER</a>
<a href="/logout" class="nav-link text-xs">LOGOUT</a> <a href="/logout" class="nav-link text-xs">LOGOUT</a>
</div>
</header> </header>
<div class="box overflow-x-auto"> <div class="box overflow-x-auto">
@@ -52,10 +55,8 @@
</td> </td>
<td>{{.CreatedAt.Format "Jan 02, 2006 15:04"}}</td> <td>{{.CreatedAt.Format "Jan 02, 2006 15:04"}}</td>
<td>{{.ExpiresAt.Format "Jan 02, 2006 15:04"}}</td> <td>{{.ExpiresAt.Format "Jan 02, 2006 15:04"}}</td>
<td>{{.DownloadCount}}</td>
<td> <td>
{{.DownloadCount}}
</td>
<td class="">
{{if .DeleteAfterDownload}} {{if .DeleteAfterDownload}}
<span class="status-tag status-active">YES</span> <span class="status-tag status-active">YES</span>
{{else}} {{else}}
@@ -82,8 +83,19 @@
</table> </table>
</div> </div>
<div class="mt-4 flex justify-between items-center">
<div class="space-x-2">
{{if gt .Page 1}}
<a href="?page={{sub .Page 1}}" class="button">← Prev</a>
{{end}}
{{if lt .Page .TotalPages}}
<a href="?page={{add .Page 1}}" class="button">Next →</a>
{{end}}
</div>
</div>
<footer class="mt-4 text-[10px] text-gray-500 uppercase font-bold"> <footer class="mt-4 text-[10px] text-gray-500 uppercase font-bold">
Total Records: {{len .Files}} Showing {{len .Files}} records — Page {{.Page}} of {{.TotalPages}} — Total Pages: {{.TotalPages}}
</footer> </footer>
</div> </div>

View File

@@ -128,7 +128,7 @@
<option value="1">1 Hour</option> <option value="1">1 Hour</option>
<option value="24">24 Hours</option> <option value="24">24 Hours</option>
<option value="168">7 Days</option> <option value="168">7 Days</option>
<option value="730001" selected>1 Month</option> <option value="730" selected>1 Month</option>
</select> </select>
</div> </div>