67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.deps import require_staff
|
|
from app.db import get_db
|
|
from app.models.report_filing import create_filing, delete_filing, list_filings, update_filing
|
|
|
|
router = APIRouter(dependencies=[Depends(require_staff)])
|
|
|
|
|
|
class FilingWrite(BaseModel):
|
|
institutionName: str
|
|
reportNumber: str
|
|
issuedOn: str
|
|
published: bool = False
|
|
|
|
|
|
def _raise(exc: ValueError) -> None:
|
|
code = status.HTTP_404_NOT_FOUND if str(exc) == "备案不存在" else status.HTTP_400_BAD_REQUEST
|
|
raise HTTPException(status_code=code, detail=str(exc)) from exc
|
|
|
|
|
|
@router.get("/reports")
|
|
def read_reports(db: Session = Depends(get_db)) -> dict:
|
|
return list_filings(db)
|
|
|
|
|
|
@router.post("/reports")
|
|
def write_report(body: FilingWrite, db: Session = Depends(get_db)) -> dict:
|
|
try:
|
|
filing_id = create_filing(
|
|
db,
|
|
institution_name=body.institutionName,
|
|
report_number=body.reportNumber,
|
|
issued_on=body.issuedOn,
|
|
published=body.published,
|
|
)
|
|
except ValueError as exc:
|
|
_raise(exc)
|
|
return {"id": filing_id}
|
|
|
|
|
|
@router.put("/reports/{filing_id}")
|
|
def replace_report(filing_id: int, body: FilingWrite, db: Session = Depends(get_db)) -> dict:
|
|
try:
|
|
update_filing(
|
|
db,
|
|
filing_id,
|
|
institution_name=body.institutionName,
|
|
report_number=body.reportNumber,
|
|
issued_on=body.issuedOn,
|
|
published=body.published,
|
|
)
|
|
except ValueError as exc:
|
|
_raise(exc)
|
|
return {"ok": True}
|
|
|
|
|
|
@router.delete("/reports/{filing_id}")
|
|
def remove_report(filing_id: int, db: Session = Depends(get_db)) -> dict:
|
|
try:
|
|
delete_filing(db, filing_id)
|
|
except ValueError as exc:
|
|
_raise(exc)
|
|
return {"ok": True}
|