Files
specs/app/routers/customers.py
T
2026-10-07 19:57:07 +00:00

139 lines
5.1 KiB
Python

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"}