[add] pagination
This commit is contained in:
parent
af41946a65
commit
1403bae036
@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -130,10 +131,30 @@ func (api *API) createAppResourcesRoute(routeName string, args ...map[string]any
|
||||
return
|
||||
}
|
||||
|
||||
length, err := api.DB.Queries.GetDocumentsSize(api.DB.Ctx, query)
|
||||
if err != nil {
|
||||
log.Error("[createAppResourcesRoute] GetDocumentsSize DB Error:", err)
|
||||
errorPage(c, http.StatusInternalServerError, fmt.Sprintf("GetDocumentsSize DB Error: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
if err = api.getDocumentsWordCount(documents); err != nil {
|
||||
log.Error("[createAppResourcesRoute] Unable to Get Word Counts: ", err)
|
||||
}
|
||||
|
||||
totalPages := int64(math.Ceil(float64(length) / float64(*qParams.Limit)))
|
||||
nextPage := *qParams.Page + 1
|
||||
previousPage := *qParams.Page - 1
|
||||
|
||||
if nextPage <= totalPages {
|
||||
templateVars["NextPage"] = nextPage
|
||||
}
|
||||
|
||||
if previousPage >= 0 {
|
||||
templateVars["PreviousPage"] = previousPage
|
||||
}
|
||||
|
||||
templateVars["PageLimit"] = *qParams.Limit
|
||||
templateVars["Data"] = documents
|
||||
} else if routeName == "document" {
|
||||
var rDocID requestDocumentID
|
||||
@ -983,7 +1004,7 @@ func bindQueryParams(c *gin.Context) queryParams {
|
||||
c.BindQuery(&qParams)
|
||||
|
||||
if qParams.Limit == nil {
|
||||
var defaultValue int64 = 50
|
||||
var defaultValue int64 = 9
|
||||
qParams.Limit = &defaultValue
|
||||
} else if *qParams.Limit < 0 {
|
||||
var zeroValue int64 = 0
|
||||
@ -991,7 +1012,7 @@ func bindQueryParams(c *gin.Context) queryParams {
|
||||
}
|
||||
|
||||
if qParams.Page == nil || *qParams.Page < 1 {
|
||||
var oneValue int64 = 0
|
||||
var oneValue int64 = 1
|
||||
qParams.Page = &oneValue
|
||||
}
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@ -182,6 +182,16 @@ ORDER BY created_at DESC
|
||||
LIMIT $limit
|
||||
OFFSET $offset;
|
||||
|
||||
-- name: GetDocumentsSize :one
|
||||
SELECT
|
||||
COUNT(rowid) AS length
|
||||
FROM documents AS docs
|
||||
WHERE $query IS NULL OR (
|
||||
docs.title LIKE $query OR
|
||||
docs.author LIKE $query
|
||||
)
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetDocumentsWithStats :many
|
||||
SELECT
|
||||
docs.id,
|
||||
|
@ -596,6 +596,24 @@ func (q *Queries) GetDocuments(ctx context.Context, arg GetDocumentsParams) ([]D
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getDocumentsSize = `-- name: GetDocumentsSize :one
|
||||
SELECT
|
||||
COUNT(rowid) AS length
|
||||
FROM documents AS docs
|
||||
WHERE ?1 IS NULL OR (
|
||||
docs.title LIKE ?1 OR
|
||||
docs.author LIKE ?1
|
||||
)
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetDocumentsSize(ctx context.Context, query interface{}) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, getDocumentsSize, query)
|
||||
var length int64
|
||||
err := row.Scan(&length)
|
||||
return length, err
|
||||
}
|
||||
|
||||
const getDocumentsWithStats = `-- name: GetDocumentsWithStats :many
|
||||
SELECT
|
||||
docs.id,
|
||||
|
@ -147,6 +147,16 @@
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
<div class="w-full flex gap-4 justify-center mt-4 text-black dark:text-white">
|
||||
{{ if .PreviousPage }}
|
||||
<a href="./documents?page={{ .PreviousPage }}&limit={{ .PageLimit }}" class="bg-white shadow-lg dark:bg-gray-600 hover:bg-gray-400 font-medium rounded text-sm text-center p-2 w-24 dark:hover:bg-gray-700 focus:outline-none">◄</a>
|
||||
{{ end }}
|
||||
|
||||
{{ if .NextPage }}
|
||||
<a href="./documents?page={{ .NextPage }}&limit={{ .PageLimit }}" class="bg-white shadow-lg dark:bg-gray-600 hover:bg-gray-400 font-medium rounded text-sm text-center p-2 w-24 dark:hover:bg-gray-700 focus:outline-none">►</a>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
||||
<div class="fixed bottom-6 right-6 rounded-full flex items-center justify-center">
|
||||
<input type="checkbox" id="upload-file-button" class="hidden css-button"/>
|
||||
<div class="rounded p-4 bg-gray-800 dark:bg-gray-200 text-white dark:text-black w-72 text-sm flex flex-col gap-2">
|
||||
|
Loading…
Reference in New Issue
Block a user