Add app/routers/categories.py
This commit is contained in:
40
app/routers/categories.py
Normal file
40
app/routers/categories.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from app.database import get_db
|
||||||
|
from app.schemas.category import CategoryCreate, CategoryUpdate, CategoryResponse
|
||||||
|
from app.services import category_service
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("", response_model=CategoryResponse, status_code=201)
|
||||||
|
def create(data: CategoryCreate, db: Session = Depends(get_db)):
|
||||||
|
return category_service.create_category(db, data)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_model=list[CategoryResponse])
|
||||||
|
def listar(db: Session = Depends(get_db)):
|
||||||
|
return category_service.get_categories(db)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{category_id}", response_model=CategoryResponse)
|
||||||
|
def obtener(category_id: str, db: Session = Depends(get_db)):
|
||||||
|
cat = category_service.get_category(db, category_id)
|
||||||
|
if not cat:
|
||||||
|
raise HTTPException(status_code=404, detail="Categoría no encontrada")
|
||||||
|
return cat
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/{category_id}", response_model=CategoryResponse)
|
||||||
|
def actualizar(category_id: str, data: CategoryUpdate, db: Session = Depends(get_db)):
|
||||||
|
cat = category_service.update_category(db, category_id, data)
|
||||||
|
if not cat:
|
||||||
|
raise HTTPException(status_code=404, detail="Categoría no encontrada")
|
||||||
|
return cat
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{category_id}", status_code=204)
|
||||||
|
def eliminar(category_id: str, db: Session = Depends(get_db)):
|
||||||
|
ok = category_service.delete_category(db, category_id)
|
||||||
|
if not ok:
|
||||||
|
raise HTTPException(status_code=404, detail="Categoría no encontrada")
|
||||||
Reference in New Issue
Block a user