Automated Action 4aac37bc90 Implement inventory management system with FastAPI and SQLite
- Setup project structure with FastAPI application
- Create database models with SQLAlchemy
- Configure Alembic for database migrations
- Implement CRUD operations for products, categories, suppliers
- Add inventory transaction functionality
- Implement user authentication with JWT
- Add health check endpoint
- Create comprehensive documentation
2025-06-05 11:43:07 +00:00

112 lines
3.4 KiB
Python

from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app import crud, models, schemas
from app.api import deps
router = APIRouter()
@router.get("/", response_model=List[schemas.Category])
def read_categories(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Retrieve categories.
"""
categories = crud.category.get_multi(db, skip=skip, limit=limit)
return categories
@router.post("/", response_model=schemas.Category)
def create_category(
*,
db: Session = Depends(deps.get_db),
category_in: schemas.CategoryCreate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Create new category.
"""
# Check if category with the same name already exists
category = crud.category.get_by_name(db, name=category_in.name)
if category:
raise HTTPException(
status_code=400,
detail="The category with this name already exists in the system.",
)
category = crud.category.create(db, obj_in=category_in)
return category
@router.get("/{category_id}", response_model=schemas.Category)
def read_category(
*,
db: Session = Depends(deps.get_db),
category_id: int,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get category by ID.
"""
category = crud.category.get(db, id=category_id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
return category
@router.put("/{category_id}", response_model=schemas.Category)
def update_category(
*,
db: Session = Depends(deps.get_db),
category_id: int,
category_in: schemas.CategoryUpdate,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Update a category.
"""
category = crud.category.get(db, id=category_id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
# If name is being updated, check for duplicates
if category_in.name and category_in.name != category.name:
existing_category = crud.category.get_by_name(db, name=category_in.name)
if existing_category:
raise HTTPException(
status_code=400,
detail="The category with this name already exists in the system.",
)
category = crud.category.update(db, db_obj=category, obj_in=category_in)
return category
@router.delete("/{category_id}", response_model=schemas.Category)
def delete_category(
*,
db: Session = Depends(deps.get_db),
category_id: int,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Delete a category.
"""
category = crud.category.get(db, id=category_id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
# Check if any products are using this category
products = crud.product.get_by_category(db, category_id=category_id)
if products:
raise HTTPException(
status_code=400,
detail="Cannot delete category because it has associated products. Update or delete those products first.",
)
category = crud.category.remove(db, id=category_id)
return category