Initial commit
This commit is contained in:
47
api/app/category/routes.py
Normal file
47
api/app/category/routes.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from fastapi_pagination import Page
|
||||
from fastapi_pagination.ext.sqlmodel import paginate
|
||||
|
||||
from category.models import Category, CategoryCreate, CategoryRead, CategoryUpdate
|
||||
from db import SessionDep
|
||||
from user.manager import get_current_user
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("")
|
||||
def create_category(category: CategoryCreate, session: SessionDep, current_user=Depends(get_current_user)) -> CategoryRead:
|
||||
Category.create(category, session)
|
||||
return category
|
||||
|
||||
@router.get("")
|
||||
def read_categories(session: SessionDep, current_user=Depends(get_current_user)) -> Page[CategoryRead]:
|
||||
return paginate(session, Category.list())
|
||||
|
||||
@router.get("/{category_id}")
|
||||
def read_category(category_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> CategoryRead:
|
||||
category = Category.get(session, category_id)
|
||||
if not category:
|
||||
raise HTTPException(status_code=404, detail="Category not found")
|
||||
return category
|
||||
|
||||
@router.put("/{category_id}")
|
||||
def update_category(category_id: UUID, category: CategoryUpdate, session: SessionDep, current_user=Depends(get_current_user)) -> CategoryRead:
|
||||
db_category = Category.get(session, category_id)
|
||||
if not db_category:
|
||||
raise HTTPException(status_code=404, detail="Category not found")
|
||||
|
||||
category_data = category.model_dump(exclude_unset=True)
|
||||
category = Category.update(session, db_category, category_data)
|
||||
return category
|
||||
|
||||
@router.delete("/{category_id}")
|
||||
def delete_category(category_id: UUID, session: SessionDep, current_user=Depends(get_current_user)):
|
||||
category = Category.get(session, category_id)
|
||||
if not category:
|
||||
raise HTTPException(status_code=404, detail="Category not found")
|
||||
|
||||
Category.delete(session, category)
|
||||
return {"ok": True}
|
||||
Reference in New Issue
Block a user