132 lines
3.1 KiB
Python
132 lines
3.1 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, schemas
|
|
from app.database.session import get_db
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get(
|
|
"/",
|
|
response_model=List[schemas.Item],
|
|
status_code=status.HTTP_200_OK,
|
|
summary="Get all items",
|
|
description="Retrieve all items with pagination and filtering options",
|
|
)
|
|
def read_items(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
active: Optional[bool] = None,
|
|
) -> Any:
|
|
"""
|
|
Retrieve all items with pagination and optional active status filtering.
|
|
"""
|
|
if active is not None:
|
|
items = crud.item.get_multi_by_active(db, active=active, skip=skip, limit=limit)
|
|
else:
|
|
items = crud.item.get_multi(db, skip=skip, limit=limit)
|
|
return items
|
|
|
|
|
|
@router.get(
|
|
"/{item_id}",
|
|
response_model=schemas.Item,
|
|
status_code=status.HTTP_200_OK,
|
|
summary="Get an item by ID",
|
|
description="Get a specific item by its ID",
|
|
)
|
|
def read_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
) -> Any:
|
|
"""
|
|
Get an item by ID.
|
|
"""
|
|
item = crud.item.get(db=db, id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
return item
|
|
|
|
|
|
@router.post(
|
|
"/",
|
|
response_model=schemas.Item,
|
|
status_code=status.HTTP_201_CREATED,
|
|
summary="Create a new item",
|
|
description="Create a new item with the provided data",
|
|
)
|
|
def create_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_in: schemas.ItemCreate,
|
|
) -> Any:
|
|
"""
|
|
Create a new item.
|
|
"""
|
|
item = crud.item.get_by_name(db=db, name=item_in.name)
|
|
if item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=f"An item with the name '{item_in.name}' already exists",
|
|
)
|
|
item = crud.item.create(db=db, obj_in=item_in)
|
|
return item
|
|
|
|
|
|
@router.put(
|
|
"/{item_id}",
|
|
response_model=schemas.Item,
|
|
status_code=status.HTTP_200_OK,
|
|
summary="Update an item",
|
|
description="Update an item with the provided data",
|
|
)
|
|
def update_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
item_in: schemas.ItemUpdate,
|
|
) -> Any:
|
|
"""
|
|
Update an item.
|
|
"""
|
|
item = crud.item.get(db=db, id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
item = crud.item.update(db=db, db_obj=item, obj_in=item_in)
|
|
return item
|
|
|
|
|
|
@router.delete(
|
|
"/{item_id}",
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
response_model=None,
|
|
summary="Delete an item",
|
|
description="Delete an item by its ID",
|
|
)
|
|
def delete_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
item_id: int,
|
|
) -> None:
|
|
"""
|
|
Delete an item.
|
|
"""
|
|
item = crud.item.get(db=db, id=item_id)
|
|
if not item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
crud.item.remove(db=db, id=item_id)
|
|
return None |