128 lines
5.0 KiB
Python
128 lines
5.0 KiB
Python
from fastapi import FastAPI, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from models import Base, engine, SessionLocal, Product, StorageLocation, Transfer
|
|
from schemas import ProductCreate, StorageLocationCreate, TransferCreate, ProductResponse, StorageLocationResponse, TransferResponse
|
|
|
|
app = FastAPI()
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
@app.post("/products/", response_model=ProductResponse, status_code=201)
|
|
def create_product(item: ProductCreate, db: Session = Depends(get_db)):
|
|
db_item = Product(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.get("/products/", response_model=list[ProductResponse])
|
|
def list_products(db: Session = Depends(get_db)):
|
|
return db.query(Product).all()
|
|
|
|
@app.get("/products/{item_id}", response_model=ProductResponse)
|
|
def get_product(item_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(Product).filter(Product.id == item_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
return item
|
|
|
|
@app.put("/products/{item_id}", response_model=ProductResponse)
|
|
def update_product(item_id: int, item: ProductCreate, db: Session = Depends(get_db)):
|
|
db_item = db.query(Product).filter(Product.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
for key, value in item.model_dump().items():
|
|
setattr(db_item, key, value)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.delete("/products/{item_id}", status_code=204)
|
|
def delete_product(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = db.query(Product).filter(Product.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
db.delete(db_item)
|
|
db.commit()
|
|
|
|
@app.post("/storage_locations/", response_model=StorageLocationResponse, status_code=201)
|
|
def create_storagelocation(item: StorageLocationCreate, db: Session = Depends(get_db)):
|
|
db_item = StorageLocation(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.get("/storage_locations/", response_model=list[StorageLocationResponse])
|
|
def list_storagelocations(db: Session = Depends(get_db)):
|
|
return db.query(StorageLocation).all()
|
|
|
|
@app.get("/storage_locations/{item_id}", response_model=StorageLocationResponse)
|
|
def get_storagelocation(item_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(StorageLocation).filter(StorageLocation.id == item_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="StorageLocation not found")
|
|
return item
|
|
|
|
@app.put("/storage_locations/{item_id}", response_model=StorageLocationResponse)
|
|
def update_storagelocation(item_id: int, item: StorageLocationCreate, db: Session = Depends(get_db)):
|
|
db_item = db.query(StorageLocation).filter(StorageLocation.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="StorageLocation not found")
|
|
for key, value in item.model_dump().items():
|
|
setattr(db_item, key, value)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.delete("/storage_locations/{item_id}", status_code=204)
|
|
def delete_storagelocation(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = db.query(StorageLocation).filter(StorageLocation.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="StorageLocation not found")
|
|
db.delete(db_item)
|
|
db.commit()
|
|
|
|
@app.post("/transfers/", response_model=TransferResponse, status_code=201)
|
|
def create_transfer(item: TransferCreate, db: Session = Depends(get_db)):
|
|
db_item = Transfer(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.get("/transfers/", response_model=list[TransferResponse])
|
|
def list_transfers(db: Session = Depends(get_db)):
|
|
return db.query(Transfer).all()
|
|
|
|
@app.get("/transfers/{item_id}", response_model=TransferResponse)
|
|
def get_transfer(item_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(Transfer).filter(Transfer.id == item_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Transfer not found")
|
|
return item
|
|
|
|
@app.put("/transfers/{item_id}", response_model=TransferResponse)
|
|
def update_transfer(item_id: int, item: TransferCreate, db: Session = Depends(get_db)):
|
|
db_item = db.query(Transfer).filter(Transfer.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Transfer not found")
|
|
for key, value in item.model_dump().items():
|
|
setattr(db_item, key, value)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.delete("/transfers/{item_id}", status_code=204)
|
|
def delete_transfer(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = db.query(Transfer).filter(Transfer.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Transfer not found")
|
|
db.delete(db_item)
|
|
db.commit()
|