67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from database import get_db
|
|
from models import Production
|
|
from schemas import ProductionCreate, ProductionUpdate, ProductionResponse
|
|
|
|
router = APIRouter(
|
|
prefix="/productions",
|
|
tags=["productions"],
|
|
)
|
|
|
|
@router.post("/", response_model=ProductionResponse, status_code=201)
|
|
async def create_production(production: ProductionCreate, db: Session = Depends(get_db)):
|
|
"""
|
|
Create a new production
|
|
"""
|
|
db_production = Production(**production.dict())
|
|
db.add(db_production)
|
|
db.commit()
|
|
db.refresh(db_production)
|
|
return db_production
|
|
|
|
@router.get("/", response_model=List[ProductionResponse])
|
|
async def get_productions(db: Session = Depends(get_db)):
|
|
"""
|
|
Get all productions
|
|
"""
|
|
productions = db.query(Production).all()
|
|
return productions
|
|
|
|
@router.get("/{production_id}", response_model=ProductionResponse)
|
|
async def get_production(production_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Get a production by ID
|
|
"""
|
|
production = db.query(Production).filter(Production.id == production_id).first()
|
|
if not production:
|
|
raise HTTPException(status_code=404, detail="Production not found")
|
|
return production
|
|
|
|
@router.put("/{production_id}", response_model=ProductionResponse)
|
|
async def update_production(production_id: int, production: ProductionUpdate, db: Session = Depends(get_db)):
|
|
"""
|
|
Update a production
|
|
"""
|
|
db_production = db.query(Production).filter(Production.id == production_id).first()
|
|
if not db_production:
|
|
raise HTTPException(status_code=404, detail="Production not found")
|
|
update_data = production.dict(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(db_production, key, value)
|
|
db.commit()
|
|
db.refresh(db_production)
|
|
return db_production
|
|
|
|
@router.delete("/{production_id}", status_code=204)
|
|
async def delete_production(production_id: int, db: Session = Depends(get_db)):
|
|
"""
|
|
Delete a production
|
|
"""
|
|
db_production = db.query(Production).filter(Production.id == production_id).first()
|
|
if not db_production:
|
|
raise HTTPException(status_code=404, detail="Production not found")
|
|
db.delete(db_production)
|
|
db.commit()
|
|
return None |