78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from app.db.session import get_db
|
|
from app.models.product import Product
|
|
from app.schemas.product import Product as ProductSchema, ProductCreate, ProductUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/", response_model=ProductSchema)
|
|
def create_product(product: ProductCreate, db: Session = Depends(get_db)):
|
|
db_product = Product(**product.dict())
|
|
db.add(db_product)
|
|
db.commit()
|
|
db.refresh(db_product)
|
|
return db_product
|
|
|
|
@router.get("/", response_model=List[ProductSchema])
|
|
def read_products(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
category_id: Optional[int] = Query(None),
|
|
supplier_id: Optional[int] = Query(None),
|
|
is_active: Optional[bool] = Query(None),
|
|
low_stock: Optional[bool] = Query(None),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
query = db.query(Product)
|
|
|
|
if category_id:
|
|
query = query.filter(Product.category_id == category_id)
|
|
if supplier_id:
|
|
query = query.filter(Product.supplier_id == supplier_id)
|
|
if is_active is not None:
|
|
query = query.filter(Product.is_active == is_active)
|
|
if low_stock:
|
|
query = query.filter(Product.quantity_in_stock <= Product.min_stock_level)
|
|
|
|
products = query.offset(skip).limit(limit).all()
|
|
return products
|
|
|
|
@router.get("/{product_id}", response_model=ProductSchema)
|
|
def read_product(product_id: int, db: Session = Depends(get_db)):
|
|
product = db.query(Product).filter(Product.id == product_id).first()
|
|
if product is None:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
return product
|
|
|
|
@router.put("/{product_id}", response_model=ProductSchema)
|
|
def update_product(product_id: int, product: ProductUpdate, db: Session = Depends(get_db)):
|
|
db_product = db.query(Product).filter(Product.id == product_id).first()
|
|
if db_product is None:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
|
|
update_data = product.dict(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(db_product, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(db_product)
|
|
return db_product
|
|
|
|
@router.delete("/{product_id}")
|
|
def delete_product(product_id: int, db: Session = Depends(get_db)):
|
|
db_product = db.query(Product).filter(Product.id == product_id).first()
|
|
if db_product is None:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
|
|
db.delete(db_product)
|
|
db.commit()
|
|
return {"message": "Product deleted successfully"}
|
|
|
|
@router.get("/sku/{sku}", response_model=ProductSchema)
|
|
def read_product_by_sku(sku: str, db: Session = Depends(get_db)):
|
|
product = db.query(Product).filter(Product.sku == sku).first()
|
|
if product is None:
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
return product |