|
| 1 | +from fastapi import APIRouter, Depends, Query |
| 2 | +from src.core.authentication import authenticate_admin |
| 3 | +from src.modules.account.account_model import Account, AccountData, AccountRole |
| 4 | +from src.modules.account.account_service import AccountService |
| 5 | +from src.modules.police.police_model import PoliceAccount, PoliceAccountUpdate |
| 6 | +from src.modules.police.police_service import PoliceService |
| 7 | + |
| 8 | +account_router = APIRouter(prefix="/api/accounts", tags=["accounts"]) |
| 9 | + |
| 10 | + |
| 11 | +@account_router.get("/police") |
| 12 | +async def get_police_credentials( |
| 13 | + police_service: PoliceService = Depends(), |
| 14 | + _=Depends(authenticate_admin), |
| 15 | +) -> PoliceAccount: |
| 16 | + police_entity = await police_service.get_police() |
| 17 | + return PoliceAccount(email=police_entity.email) |
| 18 | + |
| 19 | + |
| 20 | +@account_router.put("/police") |
| 21 | +async def update_police_credentials( |
| 22 | + data: PoliceAccountUpdate, |
| 23 | + police_service: PoliceService = Depends(), |
| 24 | + _=Depends(authenticate_admin), |
| 25 | +) -> PoliceAccount: |
| 26 | + police_entity = await police_service.update_police(data.email, data.password) |
| 27 | + return PoliceAccount(email=police_entity.email) |
| 28 | + |
| 29 | + |
| 30 | +@account_router.get("") |
| 31 | +async def list_accounts( |
| 32 | + role: list[AccountRole] | None = Query( |
| 33 | + None, description="Filter by role(s): admin, staff, student" |
| 34 | + ), |
| 35 | + account_service: AccountService = Depends(), |
| 36 | + _=Depends(authenticate_admin), |
| 37 | +) -> list[Account]: |
| 38 | + return await account_service.get_accounts_by_roles(role) |
| 39 | + |
| 40 | + |
| 41 | +@account_router.post("") |
| 42 | +async def create_account( |
| 43 | + data: AccountData, |
| 44 | + account_service: AccountService = Depends(), |
| 45 | + _=Depends(authenticate_admin), |
| 46 | +) -> Account: |
| 47 | + return await account_service.create_account(data) |
| 48 | + |
| 49 | + |
| 50 | +@account_router.put("/{account_id}") |
| 51 | +async def update_account( |
| 52 | + account_id: int, |
| 53 | + data: AccountData, |
| 54 | + account_service: AccountService = Depends(), |
| 55 | + _=Depends(authenticate_admin), |
| 56 | +) -> Account: |
| 57 | + return await account_service.update_account(account_id, data) |
| 58 | + |
| 59 | + |
| 60 | +@account_router.delete("/{account_id}") |
| 61 | +async def delete_account( |
| 62 | + account_id: int, |
| 63 | + account_service: AccountService = Depends(), |
| 64 | + _=Depends(authenticate_admin), |
| 65 | +) -> Account: |
| 66 | + return await account_service.delete_account(account_id) |
0 commit comments