from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app.database import get_db from app.models import ResourcePool, PoolResource, PoolReceipt, PoolAllocation from app.schemas import ResourcePoolCreate, ResourcePoolUpdate, PoolResourceCreate, PoolResourceUpdate, PoolReceiptCreate, PoolReceiptUpdate, PoolAllocationCreate, PoolAllocationUpdate router = APIRouter() # ResourcePool routes @router.get("/resource_pools") async def get_resource_pools(db: Session = Depends(get_db)): pools = db.query(ResourcePool).all() return pools @router.get("/resource_pools/{pool_id}") async def get_resource_pool(pool_id: int, db: Session = Depends(get_db)): pool = db.query(ResourcePool).filter(ResourcePool.id == pool_id).first() if not pool: raise HTTPException(status_code=404, detail="Resource pool not found") return pool @router.post("/resource_pools") async def create_resource_pool(pool: ResourcePoolCreate, db: Session = Depends(get_db)): db_pool = ResourcePool(**pool.dict()) db.add(db_pool) db.commit() db.refresh(db_pool) return db_pool @router.put("/resource_pools/{pool_id}") async def update_resource_pool(pool_id: int, pool: ResourcePoolUpdate, db: Session = Depends(get_db)): db_pool = db.query(ResourcePool).filter(ResourcePool.id == pool_id).first() if not db_pool: raise HTTPException(status_code=404, detail="Resource pool not found") for key, value in pool.dict(exclude_unset=True).items(): setattr(db_pool, key, value) db.commit() db.refresh(db_pool) return db_pool @router.delete("/resource_pools/{pool_id}") async def delete_resource_pool(pool_id: int, db: Session = Depends(get_db)): db_pool = db.query(ResourcePool).filter(ResourcePool.id == pool_id).first() if not db_pool: raise HTTPException(status_code=404, detail="Resource pool not found") db.delete(db_pool) db.commit() return {"message": "Resource pool deleted successfully"} # PoolResource routes @router.get("/pool_resources") async def get_pool_resources(db: Session = Depends(get_db)): resources = db.query(PoolResource).all() return resources @router.get("/pool_resources/{resource_id}") async def get_pool_resource(resource_id: int, db: Session = Depends(get_db)): resource = db.query(PoolResource).filter(PoolResource.id == resource_id).first() if not resource: raise HTTPException(status_code=404, detail="Pool resource not found") return resource @router.post("/pool_resources") async def create_pool_resource(resource: PoolResourceCreate, db: Session = Depends(get_db)): db_resource = PoolResource(**resource.dict()) db.add(db_resource) db.commit() db.refresh(db_resource) return db_resource @router.put("/pool_resources/{resource_id}") async def update_pool_resource(resource_id: int, resource: PoolResourceUpdate, db: Session = Depends(get_db)): db_resource = db.query(PoolResource).filter(PoolResource.id == resource_id).first() if not db_resource: raise HTTPException(status_code=404, detail="Pool resource not found") for key, value in resource.dict(exclude_unset=True).items(): setattr(db_resource, key, value) db.commit() db.refresh(db_resource) return db_resource @router.delete("/pool_resources/{resource_id}") async def delete_pool_resource(resource_id: int, db: Session = Depends(get_db)): db_resource = db.query(PoolResource).filter(PoolResource.id == resource_id).first() if not db_resource: raise HTTPException(status_code=404, detail="Pool resource not found") db.delete(db_resource) db.commit() return {"message": "Pool resource deleted successfully"} # PoolReceipt routes @router.get("/pool_receipts") async def get_pool_receipts(db: Session = Depends(get_db)): receipts = db.query(PoolReceipt).all() return receipts @router.get("/pool_receipts/{receipt_id}") async def get_pool_receipt(receipt_id: int, db: Session = Depends(get_db)): receipt = db.query(PoolReceipt).filter(PoolReceipt.id == receipt_id).first() if not receipt: raise HTTPException(status_code=404, detail="Pool receipt not found") return receipt @router.post("/pool_receipts") async def create_pool_receipt(receipt: PoolReceiptCreate, db: Session = Depends(get_db)): db_receipt = PoolReceipt(**receipt.dict()) db.add(db_receipt) db.commit() db.refresh(db_receipt) return db_receipt @router.put("/pool_receipts/{receipt_id}") async def update_pool_receipt(receipt_id: int, receipt: PoolReceiptUpdate, db: Session = Depends(get_db)): db_receipt = db.query(PoolReceipt).filter(PoolReceipt.id == receipt_id).first() if not db_receipt: raise HTTPException(status_code=404, detail="Pool receipt not found") for key, value in receipt.dict(exclude_unset=True).items(): setattr(db_receipt, key, value) db.commit() db.refresh(db_receipt) return db_receipt @router.delete("/pool_receipts/{receipt_id}") async def delete_pool_receipt(receipt_id: int, db: Session = Depends(get_db)): db_receipt = db.query(PoolReceipt).filter(PoolReceipt.id == receipt_id).first() if not db_receipt: raise HTTPException(status_code=404, detail="Pool receipt not found") db.delete(db_receipt) db.commit() return {"message": "Pool receipt deleted successfully"} # PoolAllocation routes @router.get("/pool_allocations") async def get_pool_allocations(db: Session = Depends(get_db)): allocations = db.query(PoolAllocation).all() return allocations @router.get("/pool_allocations/{allocation_id}") async def get_pool_allocation(allocation_id: int, db: Session = Depends(get_db)): allocation = db.query(PoolAllocation).filter(PoolAllocation.id == allocation_id).first() if not allocation: raise HTTPException(status_code=404, detail="Pool allocation not found") return allocation @router.post("/pool_allocations") async def create_pool_allocation(allocation: PoolAllocationCreate, db: Session = Depends(get_db)): db_allocation = PoolAllocation(**allocation.dict()) db.add(db_allocation) db.commit() db.refresh(db_allocation) return db_allocation @router.put("/pool_allocations/{allocation_id}") async def update_pool_allocation(allocation_id: int, allocation: PoolAllocationUpdate, db: Session = Depends(get_db)): db_allocation = db.query(PoolAllocation).filter(PoolAllocation.id == allocation_id).first() if not db_allocation: raise HTTPException(status_code=404, detail="Pool allocation not found") for key, value in allocation.dict(exclude_unset=True).items(): setattr(db_allocation, key, value) db.commit() db.refresh(db_allocation) return db_allocation @router.delete("/pool_allocations/{allocation_id}") async def delete_pool_allocation(allocation_id: int, db: Session = Depends(get_db)): db_allocation = db.query(PoolAllocation).filter(PoolAllocation.id == allocation_id).first() if not db_allocation: raise HTTPException(status_code=404, detail="Pool allocation not found") db.delete(db_allocation) db.commit() return {"message": "Pool allocation deleted successfully"}