Initial commit

This commit is contained in:
Alex
2026-10-07 19:57:07 +00:00
commit 6e6035c789
131 changed files with 23101 additions and 0 deletions
View File
Binary file not shown.
+183
View File
@@ -0,0 +1,183 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import Contract, ContractAddendum, Specification, SpecificationItem
from app.schemas import ContractCreate, ContractUpdate, ContractAddendumCreate, ContractAddendumUpdate, SpecificationCreate, SpecificationUpdate, SpecificationItemCreate, SpecificationItemUpdate
router = APIRouter()
# Contract routes
@router.get("/contracts")
async def get_contracts(db: Session = Depends(get_db)):
contracts = db.query(Contract).all()
return contracts
@router.get("/contracts/{contract_id}")
async def get_contract(contract_id: int, db: Session = Depends(get_db)):
contract = db.query(Contract).filter(Contract.id == contract_id).first()
if not contract:
raise HTTPException(status_code=404, detail="Contract not found")
return contract
@router.post("/contracts")
async def create_contract(contract: ContractCreate, db: Session = Depends(get_db)):
db_contract = Contract(**contract.dict())
db.add(db_contract)
db.commit()
db.refresh(db_contract)
return db_contract
@router.put("/contracts/{contract_id}")
async def update_contract(contract_id: int, contract: ContractUpdate, db: Session = Depends(get_db)):
db_contract = db.query(Contract).filter(Contract.id == contract_id).first()
if not db_contract:
raise HTTPException(status_code=404, detail="Contract not found")
for key, value in contract.dict(exclude_unset=True).items():
setattr(db_contract, key, value)
db.commit()
db.refresh(db_contract)
return db_contract
@router.delete("/contracts/{contract_id}")
async def delete_contract(contract_id: int, db: Session = Depends(get_db)):
db_contract = db.query(Contract).filter(Contract.id == contract_id).first()
if not db_contract:
raise HTTPException(status_code=404, detail="Contract not found")
db.delete(db_contract)
db.commit()
return {"message": "Contract deleted successfully"}
# ContractAddendum routes
@router.get("/contract_addendums")
async def get_contract_addendums(db: Session = Depends(get_db)):
addendums = db.query(ContractAddendum).all()
return addendums
@router.get("/contract_addendums/{addendum_id}")
async def get_contract_addendum(addendum_id: int, db: Session = Depends(get_db)):
addendum = db.query(ContractAddendum).filter(ContractAddendum.id == addendum_id).first()
if not addendum:
raise HTTPException(status_code=404, detail="Contract addendum not found")
return addendum
@router.post("/contract_addendums")
async def create_contract_addendum(addendum: ContractAddendumCreate, db: Session = Depends(get_db)):
db_addendum = ContractAddendum(**addendum.dict())
db.add(db_addendum)
db.commit()
db.refresh(db_addendum)
return db_addendum
@router.put("/contract_addendums/{addendum_id}")
async def update_contract_addendum(addendum_id: int, addendum: ContractAddendumUpdate, db: Session = Depends(get_db)):
db_addendum = db.query(ContractAddendum).filter(ContractAddendum.id == addendum_id).first()
if not db_addendum:
raise HTTPException(status_code=404, detail="Contract addendum not found")
for key, value in addendum.dict(exclude_unset=True).items():
setattr(db_addendum, key, value)
db.commit()
db.refresh(db_addendum)
return db_addendum
@router.delete("/contract_addendums/{addendum_id}")
async def delete_contract_addendum(addendum_id: int, db: Session = Depends(get_db)):
db_addendum = db.query(ContractAddendum).filter(ContractAddendum.id == addendum_id).first()
if not db_addendum:
raise HTTPException(status_code=404, detail="Contract addendum not found")
db.delete(db_addendum)
db.commit()
return {"message": "Contract addendum deleted successfully"}
# Specification routes
@router.get("/specifications")
async def get_specifications(db: Session = Depends(get_db)):
specifications = db.query(Specification).all()
return specifications
@router.get("/specifications/{specification_id}")
async def get_specification(specification_id: int, db: Session = Depends(get_db)):
specification = db.query(Specification).filter(Specification.id == specification_id).first()
if not specification:
raise HTTPException(status_code=404, detail="Specification not found")
return specification
@router.post("/specifications")
async def create_specification(specification: SpecificationCreate, db: Session = Depends(get_db)):
db_specification = Specification(**specification.dict())
db.add(db_specification)
db.commit()
db.refresh(db_specification)
return db_specification
@router.put("/specifications/{specification_id}")
async def update_specification(specification_id: int, specification: SpecificationUpdate, db: Session = Depends(get_db)):
db_specification = db.query(Specification).filter(Specification.id == specification_id).first()
if not db_specification:
raise HTTPException(status_code=404, detail="Specification not found")
for key, value in specification.dict(exclude_unset=True).items():
setattr(db_specification, key, value)
db.commit()
db.refresh(db_specification)
return db_specification
@router.delete("/specifications/{specification_id}")
async def delete_specification(specification_id: int, db: Session = Depends(get_db)):
db_specification = db.query(Specification).filter(Specification.id == specification_id).first()
if not db_specification:
raise HTTPException(status_code=404, detail="Specification not found")
db.delete(db_specification)
db.commit()
return {"message": "Specification deleted successfully"}
# SpecificationItem routes
@router.get("/specification_items")
async def get_specification_items(db: Session = Depends(get_db)):
items = db.query(SpecificationItem).all()
return items
@router.get("/specification_items/{item_id}")
async def get_specification_item(item_id: int, db: Session = Depends(get_db)):
item = db.query(SpecificationItem).filter(SpecificationItem.id == item_id).first()
if not item:
raise HTTPException(status_code=404, detail="Specification item not found")
return item
@router.post("/specification_items")
async def create_specification_item(item: SpecificationItemCreate, db: Session = Depends(get_db)):
db_item = SpecificationItem(**item.dict())
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
@router.put("/specification_items/{item_id}")
async def update_specification_item(item_id: int, item: SpecificationItemUpdate, db: Session = Depends(get_db)):
db_item = db.query(SpecificationItem).filter(SpecificationItem.id == item_id).first()
if not db_item:
raise HTTPException(status_code=404, detail="Specification item not found")
for key, value in item.dict(exclude_unset=True).items():
setattr(db_item, key, value)
db.commit()
db.refresh(db_item)
return db_item
@router.delete("/specification_items/{item_id}")
async def delete_specification_item(item_id: int, db: Session = Depends(get_db)):
db_item = db.query(SpecificationItem).filter(SpecificationItem.id == item_id).first()
if not db_item:
raise HTTPException(status_code=404, detail="Specification item not found")
db.delete(db_item)
db.commit()
return {"message": "Specification item deleted successfully"}
+139
View File
@@ -0,0 +1,139 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import Customer, LegalEntity, Project
from app.schemas import CustomerCreate, CustomerUpdate, LegalEntityCreate, LegalEntityUpdate, ProjectCreate, ProjectUpdate
router = APIRouter()
# Customer routes
@router.get("/customers")
async def get_customers(db: Session = Depends(get_db)):
customers = db.query(Customer).all()
return customers
@router.get("/customers/{customer_id}")
async def get_customer(customer_id: int, db: Session = Depends(get_db)):
customer = db.query(Customer).filter(Customer.id == customer_id).first()
if not customer:
raise HTTPException(status_code=404, detail="Customer not found")
return customer
@router.post("/customers")
async def create_customer(customer: CustomerCreate, db: Session = Depends(get_db)):
db_customer = Customer(**customer.dict())
db.add(db_customer)
db.commit()
db.refresh(db_customer)
return db_customer
@router.put("/customers/{customer_id}")
async def update_customer(customer_id: int, customer: CustomerUpdate, db: Session = Depends(get_db)):
db_customer = db.query(Customer).filter(Customer.id == customer_id).first()
if not db_customer:
raise HTTPException(status_code=404, detail="Customer not found")
for key, value in customer.dict(exclude_unset=True).items():
setattr(db_customer, key, value)
db.commit()
db.refresh(db_customer)
return db_customer
@router.delete("/customers/{customer_id}")
async def delete_customer(customer_id: int, db: Session = Depends(get_db)):
db_customer = db.query(Customer).filter(Customer.id == customer_id).first()
if not db_customer:
raise HTTPException(status_code=404, detail="Customer not found")
db.delete(db_customer)
db.commit()
return {"message": "Customer deleted successfully"}
# Legal Entity routes
@router.get("/legal_entities")
async def get_legal_entities(db: Session = Depends(get_db)):
entities = db.query(LegalEntity).all()
return entities
@router.get("/legal_entities/{entity_id}")
async def get_legal_entity(entity_id: int, db: Session = Depends(get_db)):
entity = db.query(LegalEntity).filter(LegalEntity.id == entity_id).first()
if not entity:
raise HTTPException(status_code=404, detail="Legal entity not found")
return entity
@router.post("/legal_entities")
async def create_legal_entity(entity: LegalEntityCreate, db: Session = Depends(get_db)):
db_entity = LegalEntity(**entity.dict())
db.add(db_entity)
db.commit()
db.refresh(db_entity)
return db_entity
@router.put("/legal_entities/{entity_id}")
async def update_legal_entity(entity_id: int, entity: LegalEntityUpdate, db: Session = Depends(get_db)):
db_entity = db.query(LegalEntity).filter(LegalEntity.id == entity_id).first()
if not db_entity:
raise HTTPException(status_code=404, detail="Legal entity not found")
for key, value in entity.dict(exclude_unset=True).items():
setattr(db_entity, key, value)
db.commit()
db.refresh(db_entity)
return db_entity
@router.delete("/legal_entities/{entity_id}")
async def delete_legal_entity(entity_id: int, db: Session = Depends(get_db)):
db_entity = db.query(LegalEntity).filter(LegalEntity.id == entity_id).first()
if not db_entity:
raise HTTPException(status_code=404, detail="Legal entity not found")
db.delete(db_entity)
db.commit()
return {"message": "Legal entity deleted successfully"}
# Project routes
@router.get("/projects")
async def get_projects(db: Session = Depends(get_db)):
projects = db.query(Project).all()
return projects
@router.get("/projects/{project_id}")
async def get_project(project_id: int, db: Session = Depends(get_db)):
project = db.query(Project).filter(Project.id == project_id).first()
if not project:
raise HTTPException(status_code=404, detail="Project not found")
return project
@router.post("/projects")
async def create_project(project: ProjectCreate, db: Session = Depends(get_db)):
db_project = Project(**project.dict())
db.add(db_project)
db.commit()
db.refresh(db_project)
return db_project
@router.put("/projects/{project_id}")
async def update_project(project_id: int, project: ProjectUpdate, db: Session = Depends(get_db)):
db_project = db.query(Project).filter(Project.id == project_id).first()
if not db_project:
raise HTTPException(status_code=404, detail="Project not found")
for key, value in project.dict(exclude_unset=True).items():
setattr(db_project, key, value)
db.commit()
db.refresh(db_project)
return db_project
@router.delete("/projects/{project_id}")
async def delete_project(project_id: int, db: Session = Depends(get_db)):
db_project = db.query(Project).filter(Project.id == project_id).first()
if not db_project:
raise HTTPException(status_code=404, detail="Project not found")
db.delete(db_project)
db.commit()
return {"message": "Project deleted successfully"}
+139
View File
@@ -0,0 +1,139 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import Document, DocumentFile, DocumentDispatch
from app.schemas import DocumentCreate, DocumentUpdate, DocumentFileCreate, DocumentFileUpdate, DocumentDispatchCreate, DocumentDispatchUpdate
router = APIRouter()
# Document routes
@router.get("/documents")
async def get_documents(db: Session = Depends(get_db)):
documents = db.query(Document).all()
return documents
@router.get("/documents/{document_id}")
async def get_document(document_id: int, db: Session = Depends(get_db)):
document = db.query(Document).filter(Document.id == document_id).first()
if not document:
raise HTTPException(status_code=404, detail="Document not found")
return document
@router.post("/documents")
async def create_document(document: DocumentCreate, db: Session = Depends(get_db)):
db_document = Document(**document.dict())
db.add(db_document)
db.commit()
db.refresh(db_document)
return db_document
@router.put("/documents/{document_id}")
async def update_document(document_id: int, document: DocumentUpdate, db: Session = Depends(get_db)):
db_document = db.query(Document).filter(Document.id == document_id).first()
if not db_document:
raise HTTPException(status_code=404, detail="Document not found")
for key, value in document.dict(exclude_unset=True).items():
setattr(db_document, key, value)
db.commit()
db.refresh(db_document)
return db_document
@router.delete("/documents/{document_id}")
async def delete_document(document_id: int, db: Session = Depends(get_db)):
db_document = db.query(Document).filter(Document.id == document_id).first()
if not db_document:
raise HTTPException(status_code=404, detail="Document not found")
db.delete(db_document)
db.commit()
return {"message": "Document deleted successfully"}
# DocumentFile routes
@router.get("/document_files")
async def get_document_files(db: Session = Depends(get_db)):
files = db.query(DocumentFile).all()
return files
@router.get("/document_files/{file_id}")
async def get_document_file(file_id: int, db: Session = Depends(get_db)):
file = db.query(DocumentFile).filter(DocumentFile.id == file_id).first()
if not file:
raise HTTPException(status_code=404, detail="Document file not found")
return file
@router.post("/document_files")
async def create_document_file(file: DocumentFileCreate, db: Session = Depends(get_db)):
db_file = DocumentFile(**file.dict())
db.add(db_file)
db.commit()
db.refresh(db_file)
return db_file
@router.put("/document_files/{file_id}")
async def update_document_file(file_id: int, file: DocumentFileUpdate, db: Session = Depends(get_db)):
db_file = db.query(DocumentFile).filter(DocumentFile.id == file_id).first()
if not db_file:
raise HTTPException(status_code=404, detail="Document file not found")
for key, value in file.dict(exclude_unset=True).items():
setattr(db_file, key, value)
db.commit()
db.refresh(db_file)
return db_file
@router.delete("/document_files/{file_id}")
async def delete_document_file(file_id: int, db: Session = Depends(get_db)):
db_file = db.query(DocumentFile).filter(DocumentFile.id == file_id).first()
if not db_file:
raise HTTPException(status_code=404, detail="Document file not found")
db.delete(db_file)
db.commit()
return {"message": "Document file deleted successfully"}
# DocumentDispatch routes
@router.get("/document_dispatches")
async def get_document_dispatches(db: Session = Depends(get_db)):
dispatches = db.query(DocumentDispatch).all()
return dispatches
@router.get("/document_dispatches/{dispatch_id}")
async def get_document_dispatch(dispatch_id: int, db: Session = Depends(get_db)):
dispatch = db.query(DocumentDispatch).filter(DocumentDispatch.id == dispatch_id).first()
if not dispatch:
raise HTTPException(status_code=404, detail="Document dispatch not found")
return dispatch
@router.post("/document_dispatches")
async def create_document_dispatch(dispatch: DocumentDispatchCreate, db: Session = Depends(get_db)):
db_dispatch = DocumentDispatch(**dispatch.dict())
db.add(db_dispatch)
db.commit()
db.refresh(db_dispatch)
return db_dispatch
@router.put("/document_dispatches/{dispatch_id}")
async def update_document_dispatch(dispatch_id: int, dispatch: DocumentDispatchUpdate, db: Session = Depends(get_db)):
db_dispatch = db.query(DocumentDispatch).filter(DocumentDispatch.id == dispatch_id).first()
if not db_dispatch:
raise HTTPException(status_code=404, detail="Document dispatch not found")
for key, value in dispatch.dict(exclude_unset=True).items():
setattr(db_dispatch, key, value)
db.commit()
db.refresh(db_dispatch)
return db_dispatch
@router.delete("/document_dispatches/{dispatch_id}")
async def delete_document_dispatch(dispatch_id: int, db: Session = Depends(get_db)):
db_dispatch = db.query(DocumentDispatch).filter(DocumentDispatch.id == dispatch_id).first()
if not db_dispatch:
raise HTTPException(status_code=404, detail="Document dispatch not found")
db.delete(db_dispatch)
db.commit()
return {"message": "Document dispatch deleted successfully"}
+139
View File
@@ -0,0 +1,139 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import License, LicenseType, Product
from app.schemas import LicenseCreate, LicenseUpdate, LicenseTypeCreate, LicenseTypeUpdate, ProductCreate, ProductUpdate
router = APIRouter()
# License routes
@router.get("/licenses")
async def get_licenses(db: Session = Depends(get_db)):
licenses = db.query(License).all()
return licenses
@router.get("/licenses/{license_id}")
async def get_license(license_id: int, db: Session = Depends(get_db)):
license = db.query(License).filter(License.id == license_id).first()
if not license:
raise HTTPException(status_code=404, detail="License not found")
return license
@router.post("/licenses")
async def create_license(license: LicenseCreate, db: Session = Depends(get_db)):
db_license = License(**license.dict())
db.add(db_license)
db.commit()
db.refresh(db_license)
return db_license
@router.put("/licenses/{license_id}")
async def update_license(license_id: int, license: LicenseUpdate, db: Session = Depends(get_db)):
db_license = db.query(License).filter(License.id == license_id).first()
if not db_license:
raise HTTPException(status_code=404, detail="License not found")
for key, value in license.dict(exclude_unset=True).items():
setattr(db_license, key, value)
db.commit()
db.refresh(db_license)
return db_license
@router.delete("/licenses/{license_id}")
async def delete_license(license_id: int, db: Session = Depends(get_db)):
db_license = db.query(License).filter(License.id == license_id).first()
if not db_license:
raise HTTPException(status_code=404, detail="License not found")
db.delete(db_license)
db.commit()
return {"message": "License deleted successfully"}
# LicenseType routes
@router.get("/license_types")
async def get_license_types(db: Session = Depends(get_db)):
license_types = db.query(LicenseType).all()
return license_types
@router.get("/license_types/{license_type_id}")
async def get_license_type(license_type_id: int, db: Session = Depends(get_db)):
license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
if not license_type:
raise HTTPException(status_code=404, detail="License type not found")
return license_type
@router.post("/license_types")
async def create_license_type(license_type: LicenseTypeCreate, db: Session = Depends(get_db)):
db_license_type = LicenseType(**license_type.dict())
db.add(db_license_type)
db.commit()
db.refresh(db_license_type)
return db_license_type
@router.put("/license_types/{license_type_id}")
async def update_license_type(license_type_id: int, license_type: LicenseTypeUpdate, db: Session = Depends(get_db)):
db_license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
if not db_license_type:
raise HTTPException(status_code=404, detail="License type not found")
for key, value in license_type.dict(exclude_unset=True).items():
setattr(db_license_type, key, value)
db.commit()
db.refresh(db_license_type)
return db_license_type
@router.delete("/license_types/{license_type_id}")
async def delete_license_type(license_type_id: int, db: Session = Depends(get_db)):
db_license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
if not db_license_type:
raise HTTPException(status_code=404, detail="License type not found")
db.delete(db_license_type)
db.commit()
return {"message": "License type deleted successfully"}
# Product routes
@router.get("/products")
async def get_products(db: Session = Depends(get_db)):
products = db.query(Product).all()
return products
@router.get("/products/{product_id}")
async def get_product(product_id: int, db: Session = Depends(get_db)):
product = db.query(Product).filter(Product.id == product_id).first()
if not product:
raise HTTPException(status_code=404, detail="Product not found")
return product
@router.post("/products")
async def create_product(product: ProductCreate, db: Session = Depends(get_db)):
db_product = Product(**product.dict())
db.add(db_product)
db.commit()
db.refresh(db_product)
return db_product
@router.put("/products/{product_id}")
async def update_product(product_id: int, product: ProductUpdate, db: Session = Depends(get_db)):
db_product = db.query(Product).filter(Product.id == product_id).first()
if not db_product:
raise HTTPException(status_code=404, detail="Product not found")
for key, value in product.dict(exclude_unset=True).items():
setattr(db_product, key, value)
db.commit()
db.refresh(db_product)
return db_product
@router.delete("/products/{product_id}")
async def delete_product(product_id: int, db: Session = Depends(get_db)):
db_product = db.query(Product).filter(Product.id == product_id).first()
if not db_product:
raise HTTPException(status_code=404, detail="Product not found")
db.delete(db_product)
db.commit()
return {"message": "Product deleted successfully"}
+183
View File
@@ -0,0 +1,183 @@
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"}
+227
View File
@@ -0,0 +1,227 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import Vendor, Purchase, PurchaseItem, Invoice, Shipment
from app.schemas import VendorCreate, VendorUpdate, PurchaseCreate, PurchaseUpdate, PurchaseItemCreate, PurchaseItemUpdate, InvoiceCreate, InvoiceUpdate, ShipmentCreate, ShipmentUpdate
router = APIRouter()
# Vendor routes
@router.get("/vendors")
async def get_vendors(db: Session = Depends(get_db)):
vendors = db.query(Vendor).all()
return vendors
@router.get("/vendors/{vendor_id}")
async def get_vendor(vendor_id: int, db: Session = Depends(get_db)):
vendor = db.query(Vendor).filter(Vendor.id == vendor_id).first()
if not vendor:
raise HTTPException(status_code=404, detail="Vendor not found")
return vendor
@router.post("/vendors")
async def create_vendor(vendor: VendorCreate, db: Session = Depends(get_db)):
db_vendor = Vendor(**vendor.dict())
db.add(db_vendor)
db.commit()
db.refresh(db_vendor)
return db_vendor
@router.put("/vendors/{vendor_id}")
async def update_vendor(vendor_id: int, vendor: VendorUpdate, db: Session = Depends(get_db)):
db_vendor = db.query(Vendor).filter(Vendor.id == vendor_id).first()
if not db_vendor:
raise HTTPException(status_code=404, detail="Vendor not found")
for key, value in vendor.dict(exclude_unset=True).items():
setattr(db_vendor, key, value)
db.commit()
db.refresh(db_vendor)
return db_vendor
@router.delete("/vendors/{vendor_id}")
async def delete_vendor(vendor_id: int, db: Session = Depends(get_db)):
db_vendor = db.query(Vendor).filter(Vendor.id == vendor_id).first()
if not db_vendor:
raise HTTPException(status_code=404, detail="Vendor not found")
db.delete(db_vendor)
db.commit()
return {"message": "Vendor deleted successfully"}
# Purchase routes
@router.get("/purchases")
async def get_purchases(db: Session = Depends(get_db)):
purchases = db.query(Purchase).all()
return purchases
@router.get("/purchases/{purchase_id}")
async def get_purchase(purchase_id: int, db: Session = Depends(get_db)):
purchase = db.query(Purchase).filter(Purchase.id == purchase_id).first()
if not purchase:
raise HTTPException(status_code=404, detail="Purchase not found")
return purchase
@router.post("/purchases")
async def create_purchase(purchase: PurchaseCreate, db: Session = Depends(get_db)):
db_purchase = Purchase(**purchase.dict())
db.add(db_purchase)
db.commit()
db.refresh(db_purchase)
return db_purchase
@router.put("/purchases/{purchase_id}")
async def update_purchase(purchase_id: int, purchase: PurchaseUpdate, db: Session = Depends(get_db)):
db_purchase = db.query(Purchase).filter(Purchase.id == purchase_id).first()
if not db_purchase:
raise HTTPException(status_code=404, detail="Purchase not found")
for key, value in purchase.dict(exclude_unset=True).items():
setattr(db_purchase, key, value)
db.commit()
db.refresh(db_purchase)
return db_purchase
@router.delete("/purchases/{purchase_id}")
async def delete_purchase(purchase_id: int, db: Session = Depends(get_db)):
db_purchase = db.query(Purchase).filter(Purchase.id == purchase_id).first()
if not db_purchase:
raise HTTPException(status_code=404, detail="Purchase not found")
db.delete(db_purchase)
db.commit()
return {"message": "Purchase deleted successfully"}
# PurchaseItem routes
@router.get("/purchase_items")
async def get_purchase_items(db: Session = Depends(get_db)):
items = db.query(PurchaseItem).all()
return items
@router.get("/purchase_items/{item_id}")
async def get_purchase_item(item_id: int, db: Session = Depends(get_db)):
item = db.query(PurchaseItem).filter(PurchaseItem.id == item_id).first()
if not item:
raise HTTPException(status_code=404, detail="Purchase item not found")
return item
@router.post("/purchase_items")
async def create_purchase_item(item: PurchaseItemCreate, db: Session = Depends(get_db)):
db_item = PurchaseItem(**item.dict())
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
@router.put("/purchase_items/{item_id}")
async def update_purchase_item(item_id: int, item: PurchaseItemUpdate, db: Session = Depends(get_db)):
db_item = db.query(PurchaseItem).filter(PurchaseItem.id == item_id).first()
if not db_item:
raise HTTPException(status_code=404, detail="Purchase item not found")
for key, value in item.dict(exclude_unset=True).items():
setattr(db_item, key, value)
db.commit()
db.refresh(db_item)
return db_item
@router.delete("/purchase_items/{item_id}")
async def delete_purchase_item(item_id: int, db: Session = Depends(get_db)):
db_item = db.query(PurchaseItem).filter(PurchaseItem.id == item_id).first()
if not db_item:
raise HTTPException(status_code=404, detail="Purchase item not found")
db.delete(db_item)
db.commit()
return {"message": "Purchase item deleted successfully"}
# Invoice routes
@router.get("/invoices")
async def get_invoices(db: Session = Depends(get_db)):
invoices = db.query(Invoice).all()
return invoices
@router.get("/invoices/{invoice_id}")
async def get_invoice(invoice_id: int, db: Session = Depends(get_db)):
invoice = db.query(Invoice).filter(Invoice.id == invoice_id).first()
if not invoice:
raise HTTPException(status_code=404, detail="Invoice not found")
return invoice
@router.post("/invoices")
async def create_invoice(invoice: InvoiceCreate, db: Session = Depends(get_db)):
db_invoice = Invoice(**invoice.dict())
db.add(db_invoice)
db.commit()
db.refresh(db_invoice)
return db_invoice
@router.put("/invoices/{invoice_id}")
async def update_invoice(invoice_id: int, invoice: InvoiceUpdate, db: Session = Depends(get_db)):
db_invoice = db.query(Invoice).filter(Invoice.id == invoice_id).first()
if not db_invoice:
raise HTTPException(status_code=404, detail="Invoice not found")
for key, value in invoice.dict(exclude_unset=True).items():
setattr(db_invoice, key, value)
db.commit()
db.refresh(db_invoice)
return db_invoice
@router.delete("/invoices/{invoice_id}")
async def delete_invoice(invoice_id: int, db: Session = Depends(get_db)):
db_invoice = db.query(Invoice).filter(Invoice.id == invoice_id).first()
if not db_invoice:
raise HTTPException(status_code=404, detail="Invoice not found")
db.delete(db_invoice)
db.commit()
return {"message": "Invoice deleted successfully"}
# Shipment routes
@router.get("/shipments")
async def get_shipments(db: Session = Depends(get_db)):
shipments = db.query(Shipment).all()
return shipments
@router.get("/shipments/{shipment_id}")
async def get_shipment(shipment_id: int, db: Session = Depends(get_db)):
shipment = db.query(Shipment).filter(Shipment.id == shipment_id).first()
if not shipment:
raise HTTPException(status_code=404, detail="Shipment not found")
return shipment
@router.post("/shipments")
async def create_shipment(shipment: ShipmentCreate, db: Session = Depends(get_db)):
db_shipment = Shipment(**shipment.dict())
db.add(db_shipment)
db.commit()
db.refresh(db_shipment)
return db_shipment
@router.put("/shipments/{shipment_id}")
async def update_shipment(shipment_id: int, shipment: ShipmentUpdate, db: Session = Depends(get_db)):
db_shipment = db.query(Shipment).filter(Shipment.id == shipment_id).first()
if not db_shipment:
raise HTTPException(status_code=404, detail="Shipment not found")
for key, value in shipment.dict(exclude_unset=True).items():
setattr(db_shipment, key, value)
db.commit()
db.refresh(db_shipment)
return db_shipment
@router.delete("/shipments/{shipment_id}")
async def delete_shipment(shipment_id: int, db: Session = Depends(get_db)):
db_shipment = db.query(Shipment).filter(Shipment.id == shipment_id).first()
if not db_shipment:
raise HTTPException(status_code=404, detail="Shipment not found")
db.delete(db_shipment)
db.commit()
return {"message": "Shipment deleted successfully"}
+491
View File
@@ -0,0 +1,491 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app.models import Project, System, Key, Guardian, License, LicenseType, Product, ResourcePool, PoolResource, PoolReceipt, PoolAllocation
from app.schemas import ProjectCreate, ProjectUpdate, SystemCreate, SystemUpdate, KeyCreate, KeyUpdate, GuardianCreate, GuardianUpdate, LicenseCreate, LicenseUpdate, LicenseTypeCreate, LicenseTypeUpdate, ProductCreate, ProductUpdate, ResourcePoolCreate, ResourcePoolUpdate, PoolResourceCreate, PoolResourceUpdate, PoolReceiptCreate, PoolReceiptUpdate, PoolAllocationCreate, PoolAllocationUpdate
router = APIRouter()
# Project routes
@router.get("/projects")
async def get_projects(db: Session = Depends(get_db)):
projects = db.query(Project).all()
return projects
@router.get("/projects/{project_id}")
async def get_project(project_id: int, db: Session = Depends(get_db)):
project = db.query(Project).filter(Project.id == project_id).first()
if not project:
raise HTTPException(status_code=404, detail="Project not found")
return project
@router.post("/projects")
async def create_project(project: ProjectCreate, db: Session = Depends(get_db)):
db_project = Project(**project.dict())
db.add(db_project)
db.commit()
db.refresh(db_project)
return db_project
@router.put("/projects/{project_id}")
async def update_project(project_id: int, project: ProjectUpdate, db: Session = Depends(get_db)):
db_project = db.query(Project).filter(Project.id == project_id).first()
if not db_project:
raise HTTPException(status_code=404, detail="Project not found")
for key, value in project.dict(exclude_unset=True).items():
setattr(db_project, key, value)
db.commit()
db.refresh(db_project)
return db_project
@router.delete("/projects/{project_id}")
async def delete_project(project_id: int, db: Session = Depends(get_db)):
db_project = db.query(Project).filter(Project.id == project_id).first()
if not db_project:
raise HTTPException(status_code=404, detail="Project not found")
db.delete(db_project)
db.commit()
return {"message": "Project deleted successfully"}
# System routes
@router.get("/systems")
async def get_systems(db: Session = Depends(get_db)):
systems = db.query(System).all()
return systems
@router.get("/systems/{system_id}")
async def get_system(system_id: int, db: Session = Depends(get_db)):
system = db.query(System).filter(System.id == system_id).first()
if not system:
raise HTTPException(status_code=404, detail="System not found")
return system
@router.post("/systems")
async def create_system(system: SystemCreate, db: Session = Depends(get_db)):
db_system = System(**system.dict())
db.add(db_system)
db.commit()
db.refresh(db_system)
return db_system
@router.put("/systems/{system_id}")
async def update_system(system_id: int, system: SystemUpdate, db: Session = Depends(get_db)):
db_system = db.query(System).filter(System.id == system_id).first()
if not db_system:
raise HTTPException(status_code=404, detail="System not found")
for key, value in system.dict(exclude_unset=True).items():
setattr(db_system, key, value)
db.commit()
db.refresh(db_system)
return db_system
@router.delete("/systems/{system_id}")
async def delete_system(system_id: int, db: Session = Depends(get_db)):
db_system = db.query(System).filter(System.id == system_id).first()
if not db_system:
raise HTTPException(status_code=404, detail="System not found")
db.delete(db_system)
db.commit()
return {"message": "System deleted successfully"}
# Key routes
@router.get("/keys")
async def get_keys(db: Session = Depends(get_db)):
keys = db.query(Key).all()
return keys
@router.get("/keys/{key_id}")
async def get_key(key_id: int, db: Session = Depends(get_db)):
key = db.query(Key).filter(Key.id == key_id).first()
if not key:
raise HTTPException(status_code=404, detail="Key not found")
return key
@router.post("/keys")
async def create_key(key: KeyCreate, db: Session = Depends(get_db)):
db_key = Key(**key.dict())
db.add(db_key)
db.commit()
db.refresh(db_key)
return db_key
@router.put("/keys/{key_id}")
async def update_key(key_id: int, key: KeyUpdate, db: Session = Depends(get_db)):
db_key = db.query(Key).filter(Key.id == key_id).first()
if not db_key:
raise HTTPException(status_code=404, detail="Key not found")
for key, value in key.dict(exclude_unset=True).items():
setattr(db_key, key, value)
db.commit()
db.refresh(db_key)
return db_key
@router.delete("/keys/{key_id}")
async def delete_key(key_id: int, db: Session = Depends(get_db)):
db_key = db.query(Key).filter(Key.id == key_id).first()
if not db_key:
raise HTTPException(status_code=404, detail="Key not found")
db.delete(db_key)
db.commit()
return {"message": "Key deleted successfully"}
# Guardian routes
@router.get("/guardians")
async def get_guardians(db: Session = Depends(get_db)):
guardians = db.query(Guardian).all()
return guardians
@router.get("/guardians/{guardian_id}")
async def get_guardian(guardian_id: int, db: Session = Depends(get_db)):
guardian = db.query(Guardian).filter(Guardian.id == guardian_id).first()
if not guardian:
raise HTTPException(status_code=404, detail="Guardian not found")
return guardian
@router.post("/guardians")
async def create_guardian(guardian: GuardianCreate, db: Session = Depends(get_db)):
db_guardian = Guardian(**guardian.dict())
db.add(db_guardian)
db.commit()
db.refresh(db_guardian)
return db_guardian
@router.put("/guardians/{guardian_id}")
async def update_guardian(guardian_id: int, guardian: GuardianUpdate, db: Session = Depends(get_db)):
db_guardian = db.query(Guardian).filter(Guardian.id == guardian_id).first()
if not db_guardian:
raise HTTPException(status_code=404, detail="Guardian not found")
for key, value in guardian.dict(exclude_unset=True).items():
setattr(db_guardian, key, value)
db.commit()
db.refresh(db_guardian)
return db_guardian
@router.delete("/guardians/{guardian_id}")
async def delete_guardian(guardian_id: int, db: Session = Depends(get_db)):
db_guardian = db.query(Guardian).filter(Guardian.id == guardian_id).first()
if not db_guardian:
raise HTTPException(status_code=404, detail="Guardian not found")
db.delete(db_guardian)
db.commit()
return {"message": "Guardian deleted successfully"}
# License routes
@router.get("/licenses")
async def get_licenses(db: Session = Depends(get_db)):
licenses = db.query(License).all()
return licenses
@router.get("/licenses/{license_id}")
async def get_license(license_id: int, db: Session = Depends(get_db)):
license = db.query(License).filter(License.id == license_id).first()
if not license:
raise HTTPException(status_code=404, detail="License not found")
return license
@router.post("/licenses")
async def create_license(license: LicenseCreate, db: Session = Depends(get_db)):
db_license = License(**license.dict())
db.add(db_license)
db.commit()
db.refresh(db_license)
return db_license
@router.put("/licenses/{license_id}")
async def update_license(license_id: int, license: LicenseUpdate, db: Session = Depends(get_db)):
db_license = db.query(License).filter(License.id == license_id).first()
if not db_license:
raise HTTPException(status_code=404, detail="License not found")
for key, value in license.dict(exclude_unset=True).items():
setattr(db_license, key, value)
db.commit()
db.refresh(db_license)
return db_license
@router.delete("/licenses/{license_id}")
async def delete_license(license_id: int, db: Session = Depends(get_db)):
db_license = db.query(License).filter(License.id == license_id).first()
if not db_license:
raise HTTPException(status_code=404, detail="License not found")
db.delete(db_license)
db.commit()
return {"message": "License deleted successfully"}
# LicenseType routes
@router.get("/license_types")
async def get_license_types(db: Session = Depends(get_db)):
license_types = db.query(LicenseType).all()
return license_types
@router.get("/license_types/{license_type_id}")
async def get_license_type(license_type_id: int, db: Session = Depends(get_db)):
license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
if not license_type:
raise HTTPException(status_code=404, detail="License type not found")
return license_type
@router.post("/license_types")
async def create_license_type(license_type: LicenseTypeCreate, db: Session = Depends(get_db)):
db_license_type = LicenseType(**license_type.dict())
db.add(db_license_type)
db.commit()
db.refresh(db_license_type)
return db_license_type
@router.put("/license_types/{license_type_id}")
async def update_license_type(license_type_id: int, license_type: LicenseTypeUpdate, db: Session = Depends(get_db)):
db_license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
if not db_license_type:
raise HTTPException(status_code=404, detail="License type not found")
for key, value in license_type.dict(exclude_unset=True).items():
setattr(db_license_type, key, value)
db.commit()
db.refresh(db_license_type)
return db_license_type
@router.delete("/license_types/{license_type_id}")
async def delete_license_type(license_type_id: int, db: Session = Depends(get_db)):
db_license_type = db.query(LicenseType).filter(LicenseType.id == license_type_id).first()
if not db_license_type:
raise HTTPException(status_code=404, detail="License type not found")
db.delete(db_license_type)
db.commit()
return {"message": "License type deleted successfully"}
# Product routes
@router.get("/products")
async def get_products(db: Session = Depends(get_db)):
products = db.query(Product).all()
return products
@router.get("/products/{product_id}")
async def get_product(product_id: int, db: Session = Depends(get_db)):
product = db.query(Product).filter(Product.id == product_id).first()
if not product:
raise HTTPException(status_code=404, detail="Product not found")
return product
@router.post("/products")
async def create_product(product: ProductCreate, db: Session = Depends(get_db)):
db_product = Product(**product.dict())
db.add(db_product)
db.commit()
db.refresh(db_product)
return db_product
@router.put("/products/{product_id}")
async def update_product(product_id: int, product: ProductUpdate, db: Session = Depends(get_db)):
db_product = db.query(Product).filter(Product.id == product_id).first()
if not db_product:
raise HTTPException(status_code=404, detail="Product not found")
for key, value in product.dict(exclude_unset=True).items():
setattr(db_product, key, value)
db.commit()
db.refresh(db_product)
return db_product
@router.delete("/products/{product_id}")
async def delete_product(product_id: int, db: Session = Depends(get_db)):
db_product = db.query(Product).filter(Product.id == product_id).first()
if not db_product:
raise HTTPException(status_code=404, detail="Product not found")
db.delete(db_product)
db.commit()
return {"message": "Product deleted successfully"}
# 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"}
+363
View File
@@ -0,0 +1,363 @@
"""
UI Routes for Specs Accounting System.
FastAPI + Jinja2 templates.
"""
from fastapi import APIRouter, Request, Query, HTTPException, Depends
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
from sqlalchemy.orm import Session
from typing import Optional
from app.database import get_db
from app.services.resource_allocation_service import ResourceAllocationService
from app.services.procurement_flow_service import ProcurementFlowService
from app.models import Project, System, Guardian, Module, Resource, ResourceAllocation, Specification, Purchase
router = APIRouter(prefix="/ui", tags=["UI"])
templates = Jinja2Templates(directory="app/templates")
def get_project_stats(db: Session, project_id: int) -> dict:
"""Get statistics for a project."""
systems_count = db.query(System).filter(
System.project_id == project_id
).count()
resources = db.query(Resource).filter(
Resource.project_id == project_id
).all()
total_received = sum(r.quantity for r in resources)
total_allocated = sum(
db.query(ResourceAllocation.quantity).filter(
ResourceAllocation.resource_id == r.id
).scalar() or 0
for r in resources
)
return {
'systems_count': systems_count,
'total_received': total_received,
'total_allocated': total_allocated,
'total_remaining': total_received - total_allocated
}
@router.get("/")
async def home(request: Request, db: Session = Depends(get_db)):
"""Home page with project list."""
projects = db.query(Project).all()
project_stats = {p.id: get_project_stats(db, p.id) for p in projects}
return templates.TemplateResponse(
"home.html",
{
"request": request,
"projects": projects,
"stats": project_stats
}
)
@router.get("/projects")
async def projects_list(request: Request, db: Session = Depends(get_db)):
"""List all projects."""
projects = db.query(Project).all()
return templates.TemplateResponse(
"projects/list.html",
{"request": request, "projects": projects}
)
@router.get("/projects/{project_id}")
async def project_detail(request: Request, project_id: int, db: Session = Depends(get_db)):
"""Project detail page."""
project = db.query(Project).filter(Project.id == project_id).first()
if not project:
raise HTTPException(status_code=404, detail="Project not found")
customers = db.query(Project).filter(Project.id == project_id).first().customers
systems = db.query(System).filter(System.project_id == project_id).all()
contracts = db.query(Project).filter(Project.id == project_id).first().contracts
resources = db.query(Resource).filter(Resource.project_id == project_id).all()
purchases = db.query(Purchase).filter(Purchase.project_id == project_id).all()
stats = get_project_stats(db, project_id)
return templates.TemplateResponse(
"projects/detail.html",
{
"request": request,
"project": project,
"customers": customers,
"systems": systems,
"contracts": contracts,
"resources": resources,
"purchases": purchases,
"stats": stats
}
)
@router.get("/systems")
async def systems_list(request: Request, project_id: Optional[int] = None, db: Session = Depends(get_db)):
"""List all systems."""
if project_id:
systems = db.query(System).filter(System.project_id == project_id).all()
else:
systems = db.query(System).all()
return templates.TemplateResponse(
"systems/list.html",
{"request": request, "systems": systems}
)
@router.get("/systems/{system_id}")
async def system_detail(request: Request, system_id: int, db: Session = Depends(get_db)):
"""System detail page - main UI page."""
system = db.query(System).filter(System.id == system_id).first()
if not system:
raise HTTPException(status_code=404, detail="System not found")
# Get Common part (guardian_id = NULL)
common_modules = db.query(SystemModule).filter(
SystemModule.system_id == system_id,
SystemModule.guardian_id == None
).all()
# Get Guardians
guardians = db.query(Guardian).filter(Guardian.system_id == system_id).all()
guardian_data = []
for guardian in guardians:
core = db.query(SystemModule).filter(
SystemModule.system_id == system_id,
SystemModule.guardian_id == guardian.id,
Module.name == "Core"
).first()
other_modules = db.query(SystemModule).filter(
SystemModule.system_id == system_id,
SystemModule.guardian_id == guardian.id
).all()
guardian_data.append({
'guardian': guardian,
'core': core,
'modules': [m for m in other_modules if m.module.name != 'Core']
})
return templates.TemplateResponse(
"systems/detail.html",
{
"request": request,
"system": system,
"common_modules": common_modules,
"guardian_data": guardian_data
}
)
@router.get("/guardians/{guardian_id}")
async def guardian_detail(request: Request, guardian_id: int, db: Session = Depends(get_db)):
"""Guardian detail page."""
guardian = db.query(Guardian).filter(Guardian.id == guardian_id).first()
if not guardian:
raise HTTPException(status_code=404, detail="Guardian not found")
core = db.query(SystemModule).filter(
SystemModule.guardian_id == guardian_id,
Module.name == "Core"
).first()
modules = db.query(SystemModule).filter(
SystemModule.guardian_id == guardian_id
).all()
return templates.TemplateResponse(
"guardians/detail.html",
{
"request": request,
"guardian": guardian,
"core": core,
"modules": modules
}
)
@router.get("/resources")
async def resources_list(request: Request, project_id: Optional[int] = None, db: Session = Depends(get_db)):
"""Resource pool page."""
query = db.query(Resource)
if project_id:
query = query.filter(Resource.project_id == project_id)
resources = query.all()
return templates.TemplateResponse(
"resources/list.html",
{"request": request, "resources": resources, "project_id": project_id}
)
@router.get("/resources/{resource_id}")
async def resource_detail(request: Request, resource_id: int, db: Session = Depends(get_db)):
"""Resource detail page."""
resource = db.query(Resource).filter(Resource.id == resource_id).first()
if not resource:
raise HTTPException(status_code=404, detail="Resource not found")
# Get allocations for this resource
allocations = db.query(ResourceAllocation).filter(
ResourceAllocation.resource_id == resource_id
).all()
# Calculate remaining
allocated = sum(a.quantity for a in allocations)
remaining = resource.quantity - allocated
# Get system and guardian info for allocations
allocation_details = []
for alloc in allocations:
system = db.query(System).filter(System.id == alloc.system_id).first()
guardian = db.query(Guardian).filter(Guardian.id == alloc.guardian_id).first() if alloc.guardian_id else None
allocation_details.append({
'allocation': alloc,
'system': system,
'guardian': guardian,
'is_common': guardian is None
})
return templates.TemplateResponse(
"resources/detail.html",
{
"request": request,
"resource": resource,
"allocations": allocation_details,
"remaining": remaining
}
)
@router.post("/resources/{resource_id}/allocate")
async def allocate_resource(
request: Request,
resource_id: int,
system_id: int = Query(..., description="System ID"),
guardian_id: Optional[int] = Query(None, description="Guardian ID (optional)"),
quantity: int = Query(..., description="Quantity to allocate"),
db: Session = Depends(get_db)
):
"""Allocate resource to system/guardian."""
try:
service = ResourceAllocationService(db)
allocation = service.allocate_resource(
resource_id=resource_id,
system_id=system_id,
quantity=quantity,
guardian_id=guardian_id,
description="UI allocation"
)
return templates.TemplateResponse(
"resources/allocated.html",
{
"request": request,
"allocation": allocation,
"resource_id": resource_id
}
)
except Exception as e:
return templates.TemplateResponse(
"resources/allocate_error.html",
{
"request": request,
"resource_id": resource_id,
"error": str(e)
}
)
@router.get("/specifications")
async def specifications_list(request: Request, project_id: Optional[int] = None, db: Session = Depends(get_db)):
"""Specifications list page."""
query = db.query(Specification)
if project_id:
query = query.filter(Specification.project_id == project_id)
specifications = query.all()
return templates.TemplateResponse(
"specifications/list.html",
{"request": request, "specifications": specifications, "project_id": project_id}
)
@router.get("/specifications/{specification_id}")
async def specification_detail(request: Request, specification_id: int, db: Session = Depends(get_db)):
"""Specification detail page."""
specification = db.query(Specification).filter(Specification.id == specification_id).first()
if not specification:
raise HTTPException(status_code=404, detail="Specification not found")
items = db.query(SpecificationItem).filter(
SpecificationItem.specification_id == specification_id
).all()
return templates.TemplateResponse(
"specifications/detail.html",
{
"request": request,
"specification": specification,
"items": items
}
)
@router.get("/purchases")
async def purchases_list(request: Request, project_id: Optional[int] = None, db: Session = Depends(get_db)):
"""Purchases list page."""
query = db.query(Purchase)
if project_id:
query = query.filter(Purchase.project_id == project_id)
purchases = query.all()
return templates.TemplateResponse(
"purchases/list.html",
{"request": request, "purchases": purchases, "project_id": project_id}
)
@router.get("/purchases/{purchase_id}")
async def purchase_detail(request: Request, purchase_id: int, db: Session = Depends(get_db)):
"""Purchase detail page."""
purchase = db.query(Purchase).filter(Purchase.id == purchase_id).first()
if not purchase:
raise HTTPException(status_code=404, detail="Purchase not found")
items = db.query(PurchaseItem).filter(
PurchaseItem.purchase_id == purchase_id
).all()
specifications = db.query(Specification).filter(
Specification.id.in_(
db.query(PurchaseSpecification.specification_id).filter(
PurchaseSpecification.purchase_id == purchase_id
)
)
).all()
return templates.TemplateResponse(
"purchases/detail.html",
{
"request": request,
"purchase": purchase,
"items": items,
"specifications": specifications
}
)
@router.get("/seed")
async def seed_demo_data(db: Session = Depends(get_db)):
"""Seed demo data endpoint."""
from app.utils.seed_data import seed_demo_data
seed_demo_data(db)
return {"message": "Demo data seeded successfully"}