feat: search rig catalog by name
This commit is contained in:
@@ -83,11 +83,19 @@ def tackle_item(item_id: UUID, db: Db) -> TackleItemOut:
|
||||
@router.get("/api/v1/tackle/rigs", response_model=PaginatedRigOut)
|
||||
def tackle_rigs(
|
||||
db: Db,
|
||||
q: str | None = None,
|
||||
limit: int = Query(48, ge=1, le=100),
|
||||
offset: int = Query(0, ge=0),
|
||||
) -> PaginatedRigOut:
|
||||
if q is not None and len(q) > 100:
|
||||
raise HTTPException(status_code=422, detail="search query is too long")
|
||||
query = select(Rig).options(selectinload(Rig.components))
|
||||
total = db.scalar(select(func.count(Rig.id))) or 0
|
||||
if q and q.strip():
|
||||
query = query.where(Rig.name.ilike(f"%{q.strip()}%"))
|
||||
total_query = select(func.count(Rig.id))
|
||||
if q and q.strip():
|
||||
total_query = total_query.where(Rig.name.ilike(f"%{q.strip()}%"))
|
||||
total = db.scalar(total_query) or 0
|
||||
rigs = list(db.scalars(query.order_by(Rig.name, Rig.id).offset(offset).limit(limit)))
|
||||
return PaginatedRigOut(
|
||||
items=[RigSummaryOut(
|
||||
|
||||
@@ -23,7 +23,7 @@ if (family) query.set("family", family);
|
||||
let result: PaginatedTackleItems = { items: [], total: 0, limit, offset }, rigs: PaginatedRigs = { items: [], total: 0, limit, offset }, unavailable = false, rigsUnavailable = false;
|
||||
const [itemsResult, rigsResult] = await Promise.allSettled([
|
||||
api<PaginatedTackleItems>(`/api/v1/tackle/items?${query}`),
|
||||
api<PaginatedRigs>(`/api/v1/tackle/rigs?limit=${limit}&offset=${offset}`),
|
||||
api<PaginatedRigs>(`/api/v1/tackle/rigs?limit=${limit}&offset=${offset}${search ? `&q=${encodeURIComponent(search)}` : ""}`),
|
||||
]);
|
||||
if (itemsResult.status === "fulfilled") result = itemsResult.value; else unavailable = true;
|
||||
if (rigsResult.status === "fulfilled") rigs = rigsResult.value; else rigsUnavailable = true;
|
||||
|
||||
Reference in New Issue
Block a user