Automated Action 4dc182713c Implement Small Business Inventory Management System
- Created project structure with FastAPI framework
- Set up SQLite database with SQLAlchemy ORM
- Implemented models: User, Item, Supplier, Transaction
- Created CRUD operations for all models
- Added API endpoints for all resources
- Implemented JWT authentication and authorization
- Added Alembic migration scripts
- Created health endpoint
- Updated documentation

generated with BackendIM... (backend.im)
2025-05-13 09:48:37 +00:00

129 lines
3.7 KiB
Python

from typing import Any, List, Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from app import crud
from app.api import deps
from app.schemas.item import Item, ItemCreate, ItemUpdate
router = APIRouter()
@router.get("/", response_model=List[Item])
def read_items(
db: Session = Depends(deps.get_db),
skip: int = 0,
limit: int = 100,
category: Optional[str] = None,
supplier_id: Optional[int] = None,
current_user: Any = Depends(deps.get_current_active_user),
) -> Any:
"""
Retrieve items.
"""
if supplier_id:
items = crud.item.get_by_supplier(db, supplier_id=supplier_id, skip=skip, limit=limit)
elif category:
items = crud.item.get_by_category(db, category=category, skip=skip, limit=limit)
else:
items = crud.item.get_multi(db, skip=skip, limit=limit)
return items
@router.post("/", response_model=Item)
def create_item(
*,
db: Session = Depends(deps.get_db),
item_in: ItemCreate,
current_user: Any = Depends(deps.get_current_active_user),
) -> Any:
"""
Create new item.
"""
# Check if the item with this SKU already exists
item = crud.item.get_by_sku(db, sku=item_in.sku)
if item:
raise HTTPException(
status_code=400,
detail="An item with this SKU already exists in the system.",
)
# Check if supplier exists if supplier_id is provided
if item_in.supplier_id and not crud.supplier.get(db, id=item_in.supplier_id):
raise HTTPException(
status_code=400,
detail=f"Supplier with ID {item_in.supplier_id} does not exist.",
)
item = crud.item.create(db, obj_in=item_in)
return item
@router.put("/{id}", response_model=Item)
def update_item(
*,
db: Session = Depends(deps.get_db),
id: int,
item_in: ItemUpdate,
current_user: Any = Depends(deps.get_current_active_user),
) -> Any:
"""
Update an item.
"""
item = crud.item.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
# Check if the new SKU already exists and belongs to a different item
if item_in.sku and item_in.sku != item.sku:
existing_item = crud.item.get_by_sku(db, sku=item_in.sku)
if existing_item and existing_item.id != id:
raise HTTPException(
status_code=400,
detail="An item with this SKU already exists in the system.",
)
# Check if supplier exists if supplier_id is provided
if item_in.supplier_id and item_in.supplier_id != item.supplier_id:
if not crud.supplier.get(db, id=item_in.supplier_id):
raise HTTPException(
status_code=400,
detail=f"Supplier with ID {item_in.supplier_id} does not exist.",
)
item = crud.item.update(db, db_obj=item, obj_in=item_in)
return item
@router.get("/{id}", response_model=Item)
def read_item(
*,
db: Session = Depends(deps.get_db),
id: int,
current_user: Any = Depends(deps.get_current_active_user),
) -> Any:
"""
Get item by ID.
"""
item = crud.item.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return item
@router.delete("/{id}", response_model=Item)
def delete_item(
*,
db: Session = Depends(deps.get_db),
id: int,
current_user: Any = Depends(deps.get_current_active_superuser),
) -> Any:
"""
Delete an item.
"""
item = crud.item.get(db, id=id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
item = crud.item.remove(db, id=id)
return item