Coverage for src/data_api/api/routers/locations.py: 100%
14 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-13 19:28 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-13 19:28 +0000
1"""Locations router."""
3from typing import Annotated
5from fastapi import APIRouter, Query, Response
7from data_api.api.schemas import locations as schemas
8from data_api.api.schemas.responses import SNAPSHOT_RESPONSES
9from data_api.runtime import get_runtime
11router = APIRouter(responses=SNAPSHOT_RESPONSES)
14@router.get("", response_model=list[schemas.LocationDetail], operation_id="getLocations")
15async def get_locations(
16 response: Response,
17 name: Annotated[str | None, Query(description="地點名稱;未提供或為空時回傳所有地點")] = None,
18 fuzzy: Annotated[
19 bool, Query(description="是否進行模糊搜尋,若不啟用則必須完全符合(不建議)")
20 ] = True,
21):
22 """
23 取得校內地點資訊;預設使用名稱模糊搜尋,fuzzy=false 時須完全符合。
24 未提供名稱或為空時回傳所有地點,無結果時回傳空陣列。
25 資料來源:[國立清華大學校園地圖](https://www.nthu.edu.tw/campusmap)
26 """
27 if name and fuzzy:
28 commit_hash, data = await get_runtime().locations.fuzzy_search_locations(query=name)
29 else:
30 commit_hash, data = await get_runtime().locations.get_locations(name=name)
31 if commit_hash is not None:
32 response.headers["X-Data-Commit-Hash"] = commit_hash
33 return data