Creacion de CRUDs
se crearon los cruds de los catalogos que se encuentran en public/reference_data No se agrego autenticacion de los endpoints
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
class CountryDTO(BaseModel):
|
||||
m3_key: str = Field(..., min_length=1, max_length=3)
|
||||
mex_key: str = Field(..., min_length=1, max_length=2)
|
||||
ame_key: str = Field(..., min_length=1, max_length=2)
|
||||
description_es: str
|
||||
description_en: str
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
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
|
||||
Reference in New Issue
Block a user