
- Created customer, driver, and order models with SQLAlchemy - Implemented CRUD API endpoints for all entities - Set up SQLite database with Alembic migrations - Added health check and base URL endpoints - Configured CORS middleware for all origins - Updated README with comprehensive documentation
52 lines
2.0 KiB
Python
52 lines
2.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from app.db.session import get_db
|
|
from app.models.customer import Customer
|
|
from app.schemas.customer import Customer as CustomerSchema, CustomerCreate, CustomerUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/", response_model=CustomerSchema)
|
|
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.get("/", response_model=List[CustomerSchema])
|
|
def read_customers(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|
customers = db.query(Customer).offset(skip).limit(limit).all()
|
|
return customers
|
|
|
|
@router.get("/{customer_id}", response_model=CustomerSchema)
|
|
def read_customer(customer_id: int, db: Session = Depends(get_db)):
|
|
customer = db.query(Customer).filter(Customer.id == customer_id).first()
|
|
if customer is None:
|
|
raise HTTPException(status_code=404, detail="Customer not found")
|
|
return customer
|
|
|
|
@router.put("/{customer_id}", response_model=CustomerSchema)
|
|
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 db_customer is None:
|
|
raise HTTPException(status_code=404, detail="Customer not found")
|
|
|
|
update_data = customer.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(db_customer, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_customer)
|
|
return db_customer
|
|
|
|
@router.delete("/{customer_id}")
|
|
def delete_customer(customer_id: int, db: Session = Depends(get_db)):
|
|
customer = db.query(Customer).filter(Customer.id == customer_id).first()
|
|
if customer is None:
|
|
raise HTTPException(status_code=404, detail="Customer not found")
|
|
|
|
db.delete(customer)
|
|
db.commit()
|
|
return {"message": "Customer deleted successfully"} |