FastAPI + PostgreSQL 16. KYC, issue sistemi, permission/group yönetimi, session yönetimi, API client auth (kışla kapısı), officials/persons CRUD. Migration 0001–0013 dahil.
68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
||
from psycopg import AsyncConnection
|
||
from mm_api.db import get_conn
|
||
from mm_api.models.admin_unit import AdminUnitCreate, AdminUnitAssign, AdminUnitClose
|
||
import mm_api.services.admin_unit as svc
|
||
|
||
router = APIRouter(tags=["admin-units"])
|
||
|
||
|
||
@router.get("/admin-unit-types")
|
||
async def list_types(conn: AsyncConnection = Depends(get_conn)):
|
||
return await svc.list_types(conn)
|
||
|
||
|
||
@router.get("/admin-units")
|
||
async def list_units(
|
||
type_id: int | None = None,
|
||
active_only: bool = True,
|
||
conn: AsyncConnection = Depends(get_conn),
|
||
):
|
||
return await svc.list_units(conn, type_id, active_only)
|
||
|
||
|
||
@router.get("/admin-units/{unit_id}")
|
||
async def get_unit(unit_id: int, conn: AsyncConnection = Depends(get_conn)):
|
||
unit = await svc.get_unit(conn, unit_id)
|
||
if not unit:
|
||
raise HTTPException(404, "Birim bulunamadı")
|
||
return unit
|
||
|
||
|
||
@router.post("/admin-units", status_code=201)
|
||
async def create_unit(data: AdminUnitCreate, conn: AsyncConnection = Depends(get_conn)):
|
||
return await svc.create_unit(conn, data)
|
||
|
||
|
||
@router.patch("/admin-units/{unit_id}/close")
|
||
async def close_unit(unit_id: int, data: AdminUnitClose, conn: AsyncConnection = Depends(get_conn)):
|
||
unit = await svc.get_unit(conn, unit_id)
|
||
if not unit:
|
||
raise HTTPException(404, "Birim bulunamadı")
|
||
await svc.close_unit(conn, unit_id, data)
|
||
return await svc.get_unit(conn, unit_id)
|
||
|
||
|
||
@router.post("/locations/{location_id}/admin-units")
|
||
async def assign_unit(location_id: int, data: AdminUnitAssign, conn: AsyncConnection = Depends(get_conn)):
|
||
await svc.assign_to_location(conn, location_id, data)
|
||
return {"ok": True}
|
||
|
||
|
||
@router.delete("/locations/{location_id}/admin-units/{unit_id}")
|
||
async def unassign_unit(
|
||
location_id: int, unit_id: int,
|
||
valid_until: str,
|
||
conn: AsyncConnection = Depends(get_conn),
|
||
):
|
||
await svc.unassign_from_location(conn, location_id, unit_id, valid_until)
|
||
return {"ok": True}
|
||
|
||
|
||
@router.get("/locations/{location_id}/admin-units")
|
||
async def location_units(
|
||
location_id: int,
|
||
active_only: bool = True,
|
||
conn: AsyncConnection = Depends(get_conn),
|
||
):
|
||
return await svc.location_units(conn, location_id, active_only)
|