feat: Added sort=random|recent query param to list_techniques endpoint…
- "backend/routers/techniques.py" - "frontend/src/api/public-client.ts" GSD-Task: S03/T01
This commit is contained in:
parent
5a959cc9b2
commit
810f96a640
2 changed files with 9 additions and 1 deletions
|
|
@ -33,6 +33,7 @@ router = APIRouter(prefix="/techniques", tags=["techniques"])
|
|||
async def list_techniques(
|
||||
category: Annotated[str | None, Query()] = None,
|
||||
creator_slug: Annotated[str | None, Query()] = None,
|
||||
sort: Annotated[str, Query()] = "recent",
|
||||
offset: Annotated[int, Query(ge=0)] = 0,
|
||||
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
||||
db: AsyncSession = Depends(get_session),
|
||||
|
|
@ -72,7 +73,12 @@ async def list_techniques(
|
|||
Creator.slug == creator_slug
|
||||
)
|
||||
|
||||
stmt = stmt.options(selectinload(TechniquePage.creator)).order_by(TechniquePage.created_at.desc()).offset(offset).limit(limit)
|
||||
stmt = stmt.options(selectinload(TechniquePage.creator))
|
||||
if sort == "random":
|
||||
stmt = stmt.order_by(func.random())
|
||||
else:
|
||||
stmt = stmt.order_by(TechniquePage.created_at.desc())
|
||||
stmt = stmt.offset(offset).limit(limit)
|
||||
result = await db.execute(stmt)
|
||||
rows = result.all()
|
||||
|
||||
|
|
|
|||
|
|
@ -218,6 +218,7 @@ export interface TechniqueListParams {
|
|||
offset?: number;
|
||||
category?: string;
|
||||
creator_slug?: string;
|
||||
sort?: string;
|
||||
}
|
||||
|
||||
export async function fetchTechniques(
|
||||
|
|
@ -228,6 +229,7 @@ export async function fetchTechniques(
|
|||
if (params.offset !== undefined) qs.set("offset", String(params.offset));
|
||||
if (params.category) qs.set("category", params.category);
|
||||
if (params.creator_slug) qs.set("creator_slug", params.creator_slug);
|
||||
if (params.sort) qs.set("sort", params.sort);
|
||||
const query = qs.toString();
|
||||
return request<TechniqueListResponse>(
|
||||
`${BASE}/techniques${query ? `?${query}` : ""}`,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue