Files
Verificacion_EFOS_Backend/output/generated/routers/efos_router.py
2026-04-06 16:06:31 -07:00

116 lines
4.1 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Query, Header, status
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from sqlalchemy.orm import Session
from typing import Optional, List
#
from database import get_db
from app.core.auth import get_current_user
from app.models.user import User # Modelo base de usuario
from app.modules.efos.models import EFOS
from app.modules.efos.schema import , Response
from app.modules.efos.services import EFOSService
router = APIRouter(prefix="/efos", tags=["efos"])
security = HTTPBearer()
#================ Create ====================
@router.post("/create", response_model=Response, status_code=status.HTTP_201_CREATED)
async def create_efos(
data: ,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Create a efos - Requires authentication"""
if not current_user:
raise HTTPException(status_code=401, detail="User not found - invalid token")
if current_user.tipo_usuario not in ["root", "admin"]:
raise HTTPException(status_code=403, detail="You don't have permission to create efos")
try:
result = EFOSService.create_efos(db=db, data=data)
return result
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
#================ Get ====================
@router.get("/", response_model=List[Response])
def get_efoss(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
skip: int = Query(0, ge=0, description="Number of records to skip"),
limit: int = Query(100, ge=1, le=1000, description="Max number of records to return")
):
"""Get list with pagination - requires authentication"""
if not current_user:
raise HTTPException(status_code=401, detail="Authentication required")
return EFOSService.get_efoss(db=db, skip=skip, limit=limit)
#================ Get by ID ====================
@router.get("/{{{entity_name}}_id}", response_model=Response)
def get_efos_by_id(
efos_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Get efos by ID - requires authentication"""
if not current_user:
raise HTTPException(status_code=401, detail="Authentication required")
try:
result = EFOSService.get_efos(db=db, efos_id=efos_id, current_user=current_user)
return result
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
#================ Update ====================
@router.patch("/update/{{{entity_name}}_id}", response_model=Response)
async def update_efos(
efos_id: int,
data: ,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Update existing efos - requires authentication"""
if not current_user:
raise HTTPException(status_code=401, detail="Authentication required")
if current_user.tipo_usuario not in ["root", "admin"]:
raise HTTPException(status_code=403, detail="You don't have permission to update efos")
try:
result = EFOSService.update_efos(
db=db,
efos_id=efos_id,
data=data,
current_user=current_user
)
return result
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
#================ Delete ====================
@router.delete("/delete/{{{entity_name}}_id}", status_code=status.HTTP_200_OK)
async def delete_efos(
efos_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Delete efos by ID - requires authentication"""
if not current_user:
raise HTTPException(status_code=401, detail="Authentication required")
if current_user.tipo_usuario not in ["root", "admin"]:
raise HTTPException(status_code=403, detail="You don't have permission to delete efos")
try:
result = EFOSService.delete_efos(db=db, efos_id=efos_id, current_user=current_user)
return {"message": "efos deleted successfully"}
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))