- Implemented service classes for PedimentoConfigParameters, PedimentoConfigSurcharges, PedimentoConfigUpdateRectification, PedimentoConfigUpdates, PedimentoCustomsOffices, PedimentoDates, PedimentoDecrementables, PedimentoIncrementables, PedimentoIndexes, PedimentoPayments, PedimentoRectificationDestination, PedimentoRectificationOrigin, PedimentoTransportMeans, and PedimentoValidation. - Each service class includes methods for CRUD operations: create, read, update, and delete. - Added a main router for the API v1, integrating various modules including authentication, tenants, licenses, and pedimentos. - Created models for PedimentoValidation with appropriate constraints and relationships.
84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
from core.database import get_core_db
|
|
from core.security import get_current_user, has_role
|
|
from .models import Country
|
|
from .dto import CountryDTO
|
|
from typing import Any, Dict
|
|
|
|
|
|
router = APIRouter(prefix="/countries")
|
|
|
|
|
|
|
|
@router.get("/", response_model=Dict[str, Any])
|
|
async def list_countries(
|
|
page: int = Query(1, ge=1, description="Número de página"),
|
|
page_size: int = Query(50, ge=1, le=100, description="Tamaño de página"),
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(get_current_user)
|
|
):
|
|
skip = (page - 1) * page_size
|
|
query = db.query(Country)
|
|
items = query.offset(skip).limit(page_size).all()
|
|
total = query.count()
|
|
return {
|
|
"items": [CountryDTO.model_validate(obj) for obj in items],
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": page_size
|
|
}
|
|
|
|
|
|
@router.get("/{m3_key}", response_model=CountryDTO)
|
|
async def get_country(m3_key: str, db: Session = Depends(get_core_db), current_user: dict = Depends(get_current_user)):
|
|
obj = db.query(Country).filter(Country.m3_key == m3_key).first()
|
|
if not obj:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
return obj
|
|
|
|
|
|
@router.post("/", response_model=CountryDTO, status_code=201)
|
|
async def create_country(
|
|
data: CountryDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(has_role("admin"))
|
|
):
|
|
obj = Country(**data.dict())
|
|
db.add(obj)
|
|
db.commit()
|
|
db.refresh(obj)
|
|
return obj
|
|
|
|
|
|
@router.put("/{m3_key}", response_model=CountryDTO)
|
|
async def update_country(
|
|
m3_key: str,
|
|
data: CountryDTO,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(has_role("admin"))
|
|
):
|
|
obj = db.query(Country).filter(Country.m3_key == m3_key).first()
|
|
if not obj:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
for field, value in data.dict().items():
|
|
setattr(obj, field, value)
|
|
db.commit()
|
|
db.refresh(obj)
|
|
return obj
|
|
|
|
|
|
@router.delete("/{m3_key}", status_code=204)
|
|
async def delete_country(
|
|
m3_key: str,
|
|
db: Session = Depends(get_core_db),
|
|
current_user: dict = Depends(has_role("admin"))
|
|
):
|
|
obj = db.query(Country).filter(Country.m3_key == m3_key).first()
|
|
if not obj:
|
|
raise HTTPException(status_code=404, detail="Not found")
|
|
db.delete(obj)
|
|
db.commit()
|
|
return None
|