45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from pydantic import BaseModel
|
|
from config.database import SessionLocal
|
|
from models.production_output import ProductionOutput
|
|
|
|
router = APIRouter(prefix="/api/production", tags=["Production"])
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
# Pydantic schemas
|
|
class ProductionOutputCreate(BaseModel):
|
|
scenario_id: int
|
|
amount: float
|
|
description: Optional[str] = None
|
|
|
|
|
|
class ProductionOutputRead(ProductionOutputCreate):
|
|
id: int
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
@router.post("/", response_model=ProductionOutputRead)
|
|
async def create_production(item: ProductionOutputCreate, db: Session = Depends(get_db)):
|
|
db_item = ProductionOutput(**item.dict())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@router.get("/", response_model=List[ProductionOutputRead])
|
|
async def list_production(db: Session = Depends(get_db)):
|
|
return db.query(ProductionOutput).all()
|