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 ContainerDTO(BaseModel):
|
||||
key: str = Field(..., min_length=1, max_length=3)
|
||||
description: 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 Container
|
||||
from .dto import ContainerDTO
|
||||
|
||||
router = APIRouter(prefix="/containers", tags=["Containers"])
|
||||
|
||||
@router.get("/", response_model=list[ContainerDTO])
|
||||
def list_containers(db: Session = Depends(get_core_db)):
|
||||
return db.query(Container).all()
|
||||
|
||||
@router.get("/{key}", response_model=ContainerDTO)
|
||||
def get_container(key: str, db: Session = Depends(get_core_db)):
|
||||
obj = db.query(Container).filter(Container.key == key).first()
|
||||
if not obj:
|
||||
raise HTTPException(status_code=404, detail="Not found")
|
||||
return obj
|
||||
|
||||
@router.post("/", response_model=ContainerDTO, status_code=201)
|
||||
def create_container(data: ContainerDTO, db: Session = Depends(get_core_db)):
|
||||
obj = Container(**data.dict())
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
@router.put("/{key}", response_model=ContainerDTO)
|
||||
def update_container(key: str, data: ContainerDTO, db: Session = Depends(get_core_db)):
|
||||
obj = db.query(Container).filter(Container.key == 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("/{key}", status_code=204)
|
||||
def delete_container(key: str, db: Session = Depends(get_core_db)):
|
||||
obj = db.query(Container).filter(Container.key == 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