Add pagination (Prev/Next with counter) and X-Total-Count header
This commit is contained in:
@@ -23,7 +23,7 @@ import pytesseract
|
||||
import piexif
|
||||
from PIL import Image as PILImage
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Query, Request, UploadFile, File, Form
|
||||
from fastapi import FastAPI, HTTPException, Query, Request, UploadFile, File, Form, Response
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
@@ -970,8 +970,16 @@ def list_assets(
|
||||
params.extend([limit, offset])
|
||||
|
||||
rows = conn.execute(sql, params).fetchall()
|
||||
# Get total count for pagination
|
||||
count_sql = "SELECT COUNT(*) FROM assets"
|
||||
if where:
|
||||
count_sql += f" WHERE {where}"
|
||||
total = conn.execute(count_sql, params[:len(params)-2]).fetchone()[0]
|
||||
conn.close()
|
||||
return [row_to_dict(r) for r in rows]
|
||||
|
||||
response = JSONResponse([row_to_dict(r) for r in rows])
|
||||
response.headers["X-Total-Count"] = str(total)
|
||||
return response
|
||||
|
||||
|
||||
@app.get("/api/assets/search")
|
||||
|
||||
Reference in New Issue
Block a user