se crearon los cruds de los catalogos que se encuentran en public/reference_data No se agrego autenticacion de los endpoints
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from core.database import get_core_db
|
|
from .models import Country
|
|
from .dto import CountryDTO
|
|
|
|
router = APIRouter(prefix="/countries", tags=["Countries"])
|
|
|
|
@router.get("/", response_model=list[CountryDTO])
|
|
def list_countries(db: Session = Depends(get_core_db)):
|
|
return db.query(Country).all()
|
|
|
|
@router.get("/{m3_key}", response_model=CountryDTO)
|
|
def get_country(m3_key: str, db: Session = Depends(get_core_db)):
|
|
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)
|
|
def create_country(data: CountryDTO, db: Session = Depends(get_core_db)):
|
|
obj = Country(**data.dict())
|
|
db.add(obj)
|
|
db.commit()
|
|
db.refresh(obj)
|
|
return obj
|
|
|
|
@router.put("/{m3_key}", response_model=CountryDTO)
|
|
def update_country(m3_key: str, data: CountryDTO, db: Session = Depends(get_core_db)):
|
|
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)
|
|
def delete_country(m3_key: str, db: Session = Depends(get_core_db)):
|
|
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
|