refactor(backend): extract pagination utilities
Issue #4: Pagination logic was repeated across multiple routes. Changes: - Add pagination.py with reusable utilities: - calculate_total_pages: computes page count from total/per_page - calculate_offset: computes offset for given page - create_paginated_response: builds PaginatedResponse with metadata - Update routes/audit.py to use pagination utilities - Update routes/booking.py to use pagination utilities - Update routes/invites.py to use pagination utilities The utilities handle the common pagination math while routes still manage their own query logic (filters, joins, ordering).
This commit is contained in:
parent
09560296aa
commit
0dd84e90a5
4 changed files with 63 additions and 37 deletions
|
|
@ -11,6 +11,11 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||
from auth import require_permission
|
||||
from database import get_db
|
||||
from models import CounterRecord, Permission, RandomNumberOutcome, SumRecord, User
|
||||
from pagination import (
|
||||
calculate_offset,
|
||||
calculate_total_pages,
|
||||
create_paginated_response,
|
||||
)
|
||||
from schemas import (
|
||||
CounterRecordResponse,
|
||||
PaginatedCounterRecords,
|
||||
|
|
@ -39,10 +44,9 @@ async def paginate_with_user_email(
|
|||
# Get total count
|
||||
count_result = await db.execute(select(func.count(model.id)))
|
||||
total = count_result.scalar() or 0
|
||||
total_pages = (total + per_page - 1) // per_page if total > 0 else 1
|
||||
|
||||
# Get paginated records with user email
|
||||
offset = (page - 1) * per_page
|
||||
offset = calculate_offset(page, per_page)
|
||||
query = (
|
||||
select(model, User.email)
|
||||
.join(User, model.user_id == User.id)
|
||||
|
|
@ -54,7 +58,7 @@ async def paginate_with_user_email(
|
|||
rows = result.all()
|
||||
|
||||
records: list[R] = [row_mapper(record, email) for record, email in rows]
|
||||
return records, total, total_pages
|
||||
return records, total, calculate_total_pages(total, per_page)
|
||||
|
||||
|
||||
def _map_counter_record(record: CounterRecord, email: str) -> CounterRecordResponse:
|
||||
|
|
@ -86,16 +90,10 @@ async def get_counter_records(
|
|||
_current_user: User = Depends(require_permission(Permission.VIEW_AUDIT)),
|
||||
) -> PaginatedCounterRecords:
|
||||
"""Get paginated counter action records."""
|
||||
records, total, total_pages = await paginate_with_user_email(
|
||||
records, total, _ = await paginate_with_user_email(
|
||||
db, CounterRecord, page, per_page, _map_counter_record
|
||||
)
|
||||
return PaginatedCounterRecords(
|
||||
records=records,
|
||||
total=total,
|
||||
page=page,
|
||||
per_page=per_page,
|
||||
total_pages=total_pages,
|
||||
)
|
||||
return create_paginated_response(records, total, page, per_page)
|
||||
|
||||
|
||||
@router.get("/sum", response_model=PaginatedSumRecords)
|
||||
|
|
@ -106,16 +104,10 @@ async def get_sum_records(
|
|||
_current_user: User = Depends(require_permission(Permission.VIEW_AUDIT)),
|
||||
) -> PaginatedSumRecords:
|
||||
"""Get paginated sum action records."""
|
||||
records, total, total_pages = await paginate_with_user_email(
|
||||
records, total, _ = await paginate_with_user_email(
|
||||
db, SumRecord, page, per_page, _map_sum_record
|
||||
)
|
||||
return PaginatedSumRecords(
|
||||
records=records,
|
||||
total=total,
|
||||
page=page,
|
||||
per_page=per_page,
|
||||
total_pages=total_pages,
|
||||
)
|
||||
return create_paginated_response(records, total, page, per_page)
|
||||
|
||||
|
||||
@router.get("/random-jobs", response_model=list[RandomNumberOutcomeResponse])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue