|
1 | | -from fastapi import APIRouter, Depends |
| 1 | +from fastapi import APIRouter, Depends, Query |
2 | 2 | from fastapi.security import OAuth2PasswordBearer |
3 | 3 | from sqlalchemy import select |
4 | 4 | from sqlalchemy.ext.asyncio import AsyncSession |
5 | 5 | from sqlalchemy.orm import selectinload |
6 | 6 |
|
7 | 7 | from ..db import get_db_session |
8 | 8 | from ..redis import r |
| 9 | +from ..utils.pagination import paginate |
9 | 10 | from .models import City, Song, Tag |
10 | | -from .schemas import CityCreate, CityRead, SongCreate, SongRead, TagCreate, TagRead |
| 11 | +from .schemas import ( |
| 12 | + CityCreate, |
| 13 | + CityRead, |
| 14 | + SongCreate, |
| 15 | + TagCreate, |
| 16 | + TagRead, |
| 17 | +) |
11 | 18 |
|
12 | 19 | router = APIRouter() |
13 | 20 |
|
14 | 21 | oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") |
15 | 22 |
|
16 | 23 |
|
17 | | -@router.get("/songs", response_model=list[SongRead]) |
18 | | -async def get_songs(session: AsyncSession = Depends(get_db_session)): |
19 | | - result = await session.scalars( |
20 | | - select(Song).options(selectinload(Song.tags), selectinload(Song.city)) |
21 | | - ) |
22 | | - songs = result.all() |
23 | | - return songs |
| 24 | +@router.get("/songs") |
| 25 | +async def get_songs( |
| 26 | + page: int = Query(1, ge=1), |
| 27 | + per_page: int = Query(5, le=100), |
| 28 | + session: AsyncSession = Depends(get_db_session), |
| 29 | +): |
| 30 | + query = select(Song).options(selectinload(Song.tags), selectinload(Song.city)) |
| 31 | + items, pagination = await paginate(session, query, page, per_page) |
| 32 | + |
| 33 | + return {"meta": pagination, "data": items} |
24 | 34 |
|
25 | 35 |
|
26 | 36 | @router.post("/songs") |
|
0 commit comments