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,9 @@
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
class CustomsSectionDTO(BaseModel):
|
||||
customs_code: str = Field(..., min_length=1, max_length=3)
|
||||
section_name: 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 CustomsSection
|
||||
from .dto import CustomsSectionDTO
|
||||
|
||||
router = APIRouter(prefix="/customs-sections", tags=["Customs Sections"])
|
||||
|
||||
@router.get("/", response_model=list[CustomsSectionDTO])
|
||||
def list_customs_sections(db: Session = Depends(get_core_db)):
|
||||
return db.query(CustomsSection).all()
|
||||
|
||||
@router.get("/{customs_code}", response_model=CustomsSectionDTO)
|
||||
def get_customs_section(customs_code: str, db: Session = Depends(get_core_db)):
|
||||
obj = db.query(CustomsSection).filter(CustomsSection.customs_code == customs_code).first()
|
||||
if not obj:
|
||||
raise HTTPException(status_code=404, detail="Not found")
|
||||
return obj
|
||||
|
||||
@router.post("/", response_model=CustomsSectionDTO, status_code=201)
|
||||
def create_customs_section(data: CustomsSectionDTO, db: Session = Depends(get_core_db)):
|
||||
obj = CustomsSection(**data.dict())
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
@router.put("/{customs_code}", response_model=CustomsSectionDTO)
|
||||
def update_customs_section(customs_code: str, data: CustomsSectionDTO, db: Session = Depends(get_core_db)):
|
||||
obj = db.query(CustomsSection).filter(CustomsSection.customs_code == customs_code).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("/{customs_code}", status_code=204)
|
||||
def delete_customs_section(customs_code: str, db: Session = Depends(get_core_db)):
|
||||
obj = db.query(CustomsSection).filter(CustomsSection.customs_code == customs_code).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