CARONTE v1.0 - Plataforma de Gestão Social
This commit is contained in:
61
backend/app/api/v1/familias.py
Normal file
61
backend/app/api/v1/familias.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from app.core.database import get_db
|
||||
from app.core.security import get_current_user_id
|
||||
from app.models.familia import Familia, MembroFamilia
|
||||
from app.models.falecido import Falecido
|
||||
from app.schemas.schemas import FamiliaCreate, FamiliaOut, MembroCreate, MembroOut, FalecidoCreate, FalecidoOut
|
||||
from app.services.checklist_engine import gerar_checklist
|
||||
from app.services.beneficio_scanner import escanear_beneficios
|
||||
|
||||
router = APIRouter(prefix="/familias", tags=["familias"])
|
||||
|
||||
@router.get("/", response_model=list[FamiliaOut])
|
||||
async def listar(user_id: int = Depends(get_current_user_id), db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Familia).where(Familia.usuario_id == user_id))
|
||||
return result.scalars().all()
|
||||
|
||||
@router.post("/", response_model=FamiliaOut)
|
||||
async def criar(data: FamiliaCreate, user_id: int = Depends(get_current_user_id), db: AsyncSession = Depends(get_db)):
|
||||
f = Familia(nome=data.nome, usuario_id=user_id)
|
||||
db.add(f)
|
||||
await db.commit()
|
||||
await db.refresh(f)
|
||||
return f
|
||||
|
||||
@router.get("/{id}", response_model=FamiliaOut)
|
||||
async def detalhe(id: int, user_id: int = Depends(get_current_user_id), db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Familia).where(Familia.id == id, Familia.usuario_id == user_id))
|
||||
f = result.scalar_one_or_none()
|
||||
if not f:
|
||||
raise HTTPException(404, "Família não encontrada")
|
||||
return f
|
||||
|
||||
@router.post("/{id}/membros", response_model=MembroOut)
|
||||
async def add_membro(id: int, data: MembroCreate, db: AsyncSession = Depends(get_db)):
|
||||
m = MembroFamilia(familia_id=id, **data.model_dump())
|
||||
db.add(m)
|
||||
await db.commit()
|
||||
await db.refresh(m)
|
||||
return m
|
||||
|
||||
@router.get("/{id}/membros", response_model=list[MembroOut])
|
||||
async def listar_membros(id: int, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(MembroFamilia).where(MembroFamilia.familia_id == id))
|
||||
return result.scalars().all()
|
||||
|
||||
@router.post("/{id}/falecido", response_model=FalecidoOut)
|
||||
async def registrar_falecido(id: int, data: FalecidoCreate, db: AsyncSession = Depends(get_db)):
|
||||
f = Falecido(familia_id=id, **data.model_dump())
|
||||
db.add(f)
|
||||
await db.commit()
|
||||
await db.refresh(f)
|
||||
await gerar_checklist(db, id, f)
|
||||
await escanear_beneficios(db, id, f)
|
||||
return f
|
||||
|
||||
@router.get("/{id}/falecidos", response_model=list[FalecidoOut])
|
||||
async def listar_falecidos(id: int, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Falecido).where(Falecido.familia_id == id))
|
||||
return result.scalars().all()
|
||||
Reference in New Issue
Block a user