Automated Action a17fe518a9 Implement Small Business Inventory Management System
This commit includes:
- Project structure setup with FastAPI and SQLite
- Database models and schemas for inventory management
- CRUD operations for all entities
- API endpoints for product, category, supplier, and inventory management
- User authentication with JWT tokens
- Initial database migration
- Comprehensive README with setup instructions
2025-06-17 19:02:35 +00:00

112 lines
3.2 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.
"""
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("/{id}", response_model=schemas.Category)
def read_category(
*,
db: Session = Depends(deps.get_db),
id: int,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Get category by ID.
"""
category = crud.category.get(db, id=id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
return category
@router.put("/{id}", response_model=schemas.Category)
def update_category(
*,
db: Session = Depends(deps.get_db),
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=id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
# Check if the updated name conflicts with an existing category
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("/{id}", status_code=204, response_model=None)
def delete_category(
*,
db: Session = Depends(deps.get_db),
id: int,
current_user: models.User = Depends(deps.get_current_active_user),
) -> Any:
"""
Delete a category.
"""
category = crud.category.get(db, id=id)
if not category:
raise HTTPException(status_code=404, detail="Category not found")
# Check if the category has products
if category.products:
raise HTTPException(
status_code=400,
detail="Cannot delete category with associated products. Reassign or delete the products first.",
)
crud.category.remove(db, id=id)
return None