|
| 1 | +"""Laptop endpoints (§6.8). List + detail; laptops are unscored.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Annotated, Any |
| 6 | + |
| 7 | +from fastapi import APIRouter, Query |
| 8 | +from sqlalchemy import func |
| 9 | +from sqlmodel import Session, select |
| 10 | +from sqlmodel.sql.expression import SelectOfScalar |
| 11 | + |
| 12 | +from app.dependencies import PaginationDep, SessionDep |
| 13 | +from app.errors import APIError, not_found |
| 14 | +from app.models.brand import Brand |
| 15 | +from app.models.cpu import CPU |
| 16 | +from app.models.gpu import DiscreteGPU |
| 17 | +from app.models.laptop import Laptop |
| 18 | +from app.routers.utils import build_ref_page |
| 19 | +from app.schemas.common import Page, ResourceRef |
| 20 | +from app.schemas.laptop import LaptopRead |
| 21 | +from app.schemas.serializers import laptop_read, resource_ref |
| 22 | + |
| 23 | +router = APIRouter(prefix="/laptops", tags=["laptops"]) |
| 24 | + |
| 25 | +_SORT_FIELDS: dict[str, Any] = { |
| 26 | + "name": Laptop.name, |
| 27 | + "release_date": Laptop.release_date, |
| 28 | + "msrp_usd": Laptop.msrp_usd, |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +def _resolve_id(session: Session, model: Any, slug: str | None) -> int | None | str: |
| 33 | + if slug is None: |
| 34 | + return None |
| 35 | + row = session.exec(select(model).where(model.slug == slug)).first() |
| 36 | + return row.id if row is not None else "MISSING" |
| 37 | + |
| 38 | + |
| 39 | +def _apply_sort(stmt: SelectOfScalar[Any], sort: str | None) -> SelectOfScalar[Any]: |
| 40 | + if not sort: |
| 41 | + return stmt.order_by(Laptop.name) |
| 42 | + descending = sort.startswith("-") |
| 43 | + field = sort[1:] if descending else sort |
| 44 | + column = _SORT_FIELDS.get(field) |
| 45 | + if column is None: |
| 46 | + raise APIError(400, "INVALID_REQUEST", f"Cannot sort by '{field}'") |
| 47 | + return stmt.order_by(column.desc() if descending else column.asc()) |
| 48 | + |
| 49 | + |
| 50 | +@router.get("", summary="List laptops") |
| 51 | +def list_laptops( |
| 52 | + session: SessionDep, |
| 53 | + pagination: PaginationDep, |
| 54 | + brand: Annotated[str | None, Query()] = None, |
| 55 | + cpu: Annotated[str | None, Query()] = None, |
| 56 | + gpu: Annotated[str | None, Query()] = None, |
| 57 | + base_model: Annotated[str | None, Query(alias="base_model_slug")] = None, |
| 58 | + sort: Annotated[str | None, Query()] = None, |
| 59 | +) -> Page[ResourceRef]: |
| 60 | + brand_id = _resolve_id(session, Brand, brand) |
| 61 | + cpu_id = _resolve_id(session, CPU, cpu) |
| 62 | + gpu_id = _resolve_id(session, DiscreteGPU, gpu) |
| 63 | + |
| 64 | + if "MISSING" in (brand_id, cpu_id, gpu_id): |
| 65 | + return build_ref_page([], count=0, path="/v1/laptops", pagination=pagination) |
| 66 | + |
| 67 | + filters = [] |
| 68 | + if brand_id is not None: |
| 69 | + filters.append(Laptop.brand_id == brand_id) |
| 70 | + if cpu_id is not None: |
| 71 | + filters.append(Laptop.cpu_id == cpu_id) |
| 72 | + if gpu_id is not None: |
| 73 | + filters.append(Laptop.gpu_id == gpu_id) |
| 74 | + if base_model is not None: |
| 75 | + filters.append(Laptop.base_model_slug == base_model) |
| 76 | + |
| 77 | + count_stmt = select(func.count()).select_from(Laptop) |
| 78 | + list_stmt = select(Laptop) |
| 79 | + for clause in filters: |
| 80 | + count_stmt = count_stmt.where(clause) |
| 81 | + list_stmt = list_stmt.where(clause) |
| 82 | + |
| 83 | + count = session.exec(count_stmt).one() |
| 84 | + list_stmt = _apply_sort(list_stmt, sort).offset(pagination.offset).limit(pagination.limit) |
| 85 | + rows = session.exec(list_stmt).all() |
| 86 | + |
| 87 | + refs = [resource_ref("laptops", row.slug, row.name) for row in rows] |
| 88 | + applied = { |
| 89 | + k: v |
| 90 | + for k, v in ( |
| 91 | + ("brand", brand), |
| 92 | + ("cpu", cpu), |
| 93 | + ("gpu", gpu), |
| 94 | + ("base_model_slug", base_model), |
| 95 | + ("sort", sort), |
| 96 | + ) |
| 97 | + if v |
| 98 | + } |
| 99 | + return build_ref_page( |
| 100 | + refs, count=count, path="/v1/laptops", pagination=pagination, filters=applied |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +@router.get("/{slug}", summary="Get a laptop") |
| 105 | +def get_laptop(slug: str, session: SessionDep) -> LaptopRead: |
| 106 | + laptop = session.exec(select(Laptop).where(Laptop.slug == slug)).first() |
| 107 | + if laptop is None: |
| 108 | + raise not_found("Laptop", slug) |
| 109 | + brand = session.get(Brand, laptop.brand_id) |
| 110 | + if brand is None: # pragma: no cover - guarded by FK + validation |
| 111 | + raise not_found("Brand", str(laptop.brand_id)) |
| 112 | + cpu = session.get(CPU, laptop.cpu_id) if laptop.cpu_id is not None else None |
| 113 | + gpu = session.get(DiscreteGPU, laptop.gpu_id) if laptop.gpu_id is not None else None |
| 114 | + return laptop_read(laptop, brand, cpu, gpu) |
0 commit comments