CARONTE v1.0 - Plataforma de Gestão Social
This commit is contained in:
32
backend/app/api/v1/beneficios.py
Normal file
32
backend/app/api/v1/beneficios.py
Normal file
@@ -0,0 +1,32 @@
|
||||
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.beneficio import Beneficio
|
||||
from app.models.falecido import Falecido
|
||||
from app.schemas.schemas import BeneficioOut
|
||||
from app.services.beneficio_scanner import escanear_beneficios
|
||||
|
||||
router = APIRouter(prefix="/familias/{familia_id}/beneficios", tags=["beneficios"])
|
||||
|
||||
@router.get("/", response_model=list[BeneficioOut])
|
||||
async def listar(familia_id: int, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(Beneficio).where(Beneficio.familia_id == familia_id))
|
||||
return result.scalars().all()
|
||||
|
||||
@router.post("/scan", response_model=list[BeneficioOut])
|
||||
async def scan(familia_id: int, db: AsyncSession = Depends(get_db)):
|
||||
# Delete existing and rescan
|
||||
result = await db.execute(select(Beneficio).where(Beneficio.familia_id == familia_id))
|
||||
for b in result.scalars().all():
|
||||
await db.delete(b)
|
||||
await db.commit()
|
||||
|
||||
result = await db.execute(select(Falecido).where(Falecido.familia_id == familia_id))
|
||||
falecidos = result.scalars().all()
|
||||
all_bens = []
|
||||
for f in falecidos:
|
||||
bens = await escanear_beneficios(db, familia_id, f)
|
||||
all_bens.extend(bens)
|
||||
return all_bens
|
||||
Reference in New Issue
Block a user