API_logistics/app/infrastructure/fastapi/storage_accessory_routes.py
2024-10-04 10:33:37 +05:00

54 lines
2.3 KiB
Python

from typing import List
from fastapi import APIRouter, HTTPException, Depends
from sqlalchemy.orm import Session
from app.infrastructure.database.dependencies import get_db
from app.core.entities.storage_accessory import StorageAccessoryEntity
from app.core.usecases.storage_accessory_service import StorageAccessoryService
router = APIRouter()
@router.get("/storage-accessories", response_model=List[StorageAccessoryEntity])
def read_storage_accessories(db: Session = Depends(get_db)):
service = StorageAccessoryService(db)
return service.get_all_storage_accessories()
@router.get("/storage-accessories/{storage_accessories_id}", response_model=StorageAccessoryEntity)
def read_storage_accessory(storage_accessories_id: int, db: Session = Depends(get_db)):
service = StorageAccessoryService(db)
storage_accessory = service.get_storage_accessory_by_id(storage_accessories_id)
if storage_accessory is None:
raise HTTPException(status_code=404, detail="Storage Accessory not found")
return storage_accessory
@router.post("/storage-accessories", response_model=StorageAccessoryEntity)
def create_storage_accessory(storage_accessory: StorageAccessoryEntity, db: Session = Depends(get_db)):
service = StorageAccessoryService(db)
return service.create_storage_accessory(storage_accessory)
@router.put("/storage-accessories/{storage_accessories_id}", response_model=StorageAccessoryEntity)
def update_storage_accessory(storage_accessories_id: int, storage_accessory: StorageAccessoryEntity,
db: Session = Depends(get_db)):
service = StorageAccessoryService(db)
updated_storage_accessory = service.update_storage_accessory(storage_accessories_id, storage_accessory)
if updated_storage_accessory is None:
raise HTTPException(status_code=404, detail="Storage Accessory not found")
return updated_storage_accessory
@router.delete("/storage-accessories/{storage_accessories_id}", response_model=bool)
def delete_storage_accessory(storage_accessories_id: int, db: Session = Depends(get_db)):
service = StorageAccessoryService(db)
success = service.delete_storage_accessory(storage_accessories_id)
if not success:
raise HTTPException(status_code=404, detail="Storage Accessory not found")
return success