Add app/routers/products.py
This commit is contained in:
45
app/routers/products.py
Normal file
45
app/routers/products.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from app.database import get_db
|
||||
from app.schemas.product import ProductCreate, ProductUpdate, ProductResponse
|
||||
from app.services import product_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("", response_model=ProductResponse, status_code=201)
|
||||
def create(data: ProductCreate, db: Session = Depends(get_db)):
|
||||
return product_service.create_product(db, data)
|
||||
|
||||
|
||||
@router.get("", response_model=list[ProductResponse])
|
||||
def listar(skip: int = Query(0), limit: int = Query(20), db: Session = Depends(get_db)):
|
||||
return product_service.get_products(db, skip, limit)
|
||||
|
||||
|
||||
@router.get("/low-stock", response_model=list[ProductResponse])
|
||||
def low_stock(db: Session = Depends(get_db)):
|
||||
return product_service.get_low_stock_products(db)
|
||||
|
||||
|
||||
@router.get("/{product_id}", response_model=ProductResponse)
|
||||
def obtener(product_id: str, db: Session = Depends(get_db)):
|
||||
prod = product_service.get_product(db, product_id)
|
||||
if not prod:
|
||||
raise HTTPException(status_code=404, detail="Producto no encontrado")
|
||||
return prod
|
||||
|
||||
|
||||
@router.put("/{product_id}", response_model=ProductResponse)
|
||||
def actualizar(product_id: str, data: ProductUpdate, db: Session = Depends(get_db)):
|
||||
prod = product_service.update_product(db, product_id, data)
|
||||
if not prod:
|
||||
raise HTTPException(status_code=404, detail="Producto no encontrado")
|
||||
return prod
|
||||
|
||||
|
||||
@router.delete("/{product_id}", status_code=204)
|
||||
def eliminar(product_id: str, db: Session = Depends(get_db)):
|
||||
ok = product_service.delete_product(db, product_id)
|
||||
if not ok:
|
||||
raise HTTPException(status_code=404, detail="Producto no encontrado")
|
||||
Reference in New Issue
Block a user