feat: Refactor DTOs and models to use from_attributes, update database schema references, and implement trailer types
This commit is contained in:
@@ -22,6 +22,7 @@ from .invoice_types.routes import router as invoice_types_router
|
||||
from .code_pedimento_regimens.routes import router as code_pedimento_regimens_router
|
||||
from .pedimento_regimens.routes import router as pedimento_regimens_router
|
||||
from .incoterms.routes import router as incoterms_router
|
||||
from .trailer_types.routes import router as trailer_types_router
|
||||
|
||||
# Router principal
|
||||
router = APIRouter()
|
||||
@@ -60,6 +61,11 @@ router.include_router(
|
||||
router.include_router(
|
||||
states_router, prefix="/refrence_data", tags=["public / refrence_data / states"]
|
||||
)
|
||||
router.include_router(
|
||||
trailer_types_router,
|
||||
prefix="/refrence_data",
|
||||
tags=["public / refrence_data / trailer_types"],
|
||||
)
|
||||
router.include_router(
|
||||
transport_types_router,
|
||||
prefix="/refrence_data",
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
class TrailerTypeBaseDTO(BaseModel):
|
||||
trailer_type_key: str
|
||||
description: Optional[str]
|
||||
company_id: str
|
||||
tenant_id: str
|
||||
|
||||
class TrailerTypeCreateDTO(TrailerTypeBaseDTO):
|
||||
pass
|
||||
|
||||
class TrailerTypeResponseDTO(TrailerTypeBaseDTO):
|
||||
class Config:
|
||||
from_attributes = True
|
||||
@@ -0,0 +1,11 @@
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey
|
||||
from core.database import Base
|
||||
|
||||
class TrailerType(Base):
|
||||
__tablename__ = "trailer_type"
|
||||
__table_args__ = {"schema": "a76"}
|
||||
|
||||
trailer_type_key = Column(String(2), primary_key=True, nullable=False)
|
||||
description = Column(String(100), nullable=True)
|
||||
company_id = Column(Integer, ForeignKey("a76.company.id"), nullable=False)
|
||||
tenant_id = Column(Integer, ForeignKey("a76.tenants.id"), nullable=False)
|
||||
@@ -0,0 +1,24 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from . import services, dto
|
||||
from core.database import get_core_db
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/trailer-types/{trailer_type_key}", response_model=dto.TrailerTypeResponseDTO)
|
||||
def get_trailer_type(trailer_type_key: str, db: Session = Depends(get_core_db)):
|
||||
trailer_type = services.TrailerTypeService.get_trailer_type_by_key(db, trailer_type_key)
|
||||
if not trailer_type:
|
||||
raise HTTPException(status_code=404, detail="Trailer type not found")
|
||||
return trailer_type
|
||||
|
||||
@router.post("/trailer-types", response_model=dto.TrailerTypeResponseDTO)
|
||||
def create_trailer_type(trailer_type_data: dto.TrailerTypeCreateDTO, db: Session = Depends(get_core_db)):
|
||||
return services.TrailerTypeService.create_trailer_type(db, trailer_type_data)
|
||||
|
||||
@router.delete("/trailer-types/{trailer_type_key}", response_model=dto.TrailerTypeResponseDTO)
|
||||
def delete_trailer_type(trailer_type_key: str, db: Session = Depends(get_core_db)):
|
||||
trailer_type = services.TrailerTypeService.delete_trailer_type(db, trailer_type_key)
|
||||
if not trailer_type:
|
||||
raise HTTPException(status_code=404, detail="Trailer type not found")
|
||||
return trailer_type
|
||||
@@ -0,0 +1,23 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from . import models, dto
|
||||
|
||||
class TrailerTypeService:
|
||||
@staticmethod
|
||||
def get_trailer_type_by_key(db: Session, trailer_type_key: str):
|
||||
return db.query(models.TrailerType).filter(models.TrailerType.trailer_type_key == trailer_type_key).first()
|
||||
|
||||
@staticmethod
|
||||
def create_trailer_type(db: Session, trailer_type_data: dto.TrailerTypeCreateDTO):
|
||||
new_trailer_type = models.TrailerType(**trailer_type_data.dict())
|
||||
db.add(new_trailer_type)
|
||||
db.commit()
|
||||
db.refresh(new_trailer_type)
|
||||
return new_trailer_type
|
||||
|
||||
@staticmethod
|
||||
def delete_trailer_type(db: Session, trailer_type_key: str):
|
||||
trailer_type = TrailerTypeService.get_trailer_type_by_key(db, trailer_type_key)
|
||||
if trailer_type:
|
||||
db.delete(trailer_type)
|
||||
db.commit()
|
||||
return trailer_type
|
||||
Reference in New Issue
Block a user