
Features include: - User management with JWT authentication and role-based access - Inventory items with SKU/barcode tracking and stock management - Categories and suppliers organization - Inventory transactions with automatic stock updates - Low stock alerts and advanced search/filtering - RESTful API with comprehensive CRUD operations - SQLite database with Alembic migrations - Auto-generated API documentation Co-Authored-By: Claude <noreply@anthropic.com>
78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from app.db.session import get_db
|
|
from app.crud import category
|
|
from app.schemas.category import Category, CategoryCreate, CategoryUpdate
|
|
from app.auth.auth_handler import get_current_active_user
|
|
from app.models.user import User
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/", response_model=Category)
|
|
def create_category(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
category_in: CategoryCreate,
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
db_category = category.get_by_name(db, name=category_in.name)
|
|
if db_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Category with this name already exists"
|
|
)
|
|
return category.create(db, obj_in=category_in)
|
|
|
|
@router.get("/", response_model=List[Category])
|
|
def read_categories(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
return category.get_multi(db, skip=skip, limit=limit)
|
|
|
|
@router.get("/{category_id}", response_model=Category)
|
|
def read_category(
|
|
category_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
db_category = category.get(db, id=category_id)
|
|
if not db_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found"
|
|
)
|
|
return db_category
|
|
|
|
@router.put("/{category_id}", response_model=Category)
|
|
def update_category(
|
|
category_id: int,
|
|
category_in: CategoryUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
db_category = category.get(db, id=category_id)
|
|
if not db_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found"
|
|
)
|
|
return category.update(db, db_obj=db_category, obj_in=category_in)
|
|
|
|
@router.delete("/{category_id}")
|
|
def delete_category(
|
|
category_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user)
|
|
):
|
|
db_category = category.get(db, id=category_id)
|
|
if not db_category:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Category not found"
|
|
)
|
|
category.remove(db, id=category_id)
|
|
return {"message": "Category deleted successfully"} |