Coverage for src/data_api/api/routers/announcements.py: 100%
19 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"""
2Announcements router.
4Handles HTTP endpoints for announcements.
5"""
7from fastapi import APIRouter, Query, Response
9from data_api.api.schemas import announcements as schemas
10from data_api.api.schemas.responses import SNAPSHOT_RESPONSES
11from data_api.runtime import get_runtime
13router = APIRouter(responses=SNAPSHOT_RESPONSES)
16@router.get(
17 "",
18 response_model=list[schemas.AnnouncementDetail],
19 operation_id="getAnnouncements",
20)
21async def get_announcements(
22 response: Response,
23 department: str = Query(
24 None,
25 description="部門名稱。請透過 `/announcements/sources` 的 department 欄位取得部門名稱。",
26 ),
27 title: str = Query(None, description="公告標題關鍵字"),
28 language: schemas.AnnouncementLanguageOption = Query(None, description="語言篩選"),
29 fuzzy: bool = Query(True, description="是否進行模糊搜尋,若不啟用則必須完全符合(不建議)"),
30 url: str = Query(
31 None,
32 description="公告來源網址部分比對,忽略 http:// 與 https://,可輸入網域或含路徑的網址;不受 fuzzy 影響。",
33 ),
34):
35 """
36 取得校內每個處室的所有公告資訊。
37 資料來源:各處室網站
38 """
39 if fuzzy:
40 commit_hash, data = await get_runtime().announcements.fuzzy_search_announcements(
41 department=department, title=title, language=language, url=url
42 )
43 else:
44 commit_hash, data = await get_runtime().announcements.get_announcements(
45 department=department, title=title, language=language, url=url
46 )
47 if commit_hash is not None:
48 response.headers["X-Data-Commit-Hash"] = commit_hash
49 return data
52@router.get(
53 "/sources",
54 response_model=list[schemas.AnnouncementSource],
55 operation_id="getAnnouncementsList",
56)
57async def get_announcements_list(
58 response: Response,
59 department: str = Query(None, description="部門名稱"),
60):
61 """
62 取得公告列表(不包含文章內容)。
63 資料來源:各處室網站
64 """
65 commit_hash, data = await get_runtime().announcements.get_announcements_list(
66 department=department
67 )
68 if commit_hash is not None:
69 response.headers["X-Data-Commit-Hash"] = commit_hash
70 return data