Automated Action 4e92bb1338 Create FastAPI E-commerce API with SQLite
This commit includes:
- Project structure setup with FastAPI
- Database models with SQLAlchemy (users, products, categories, orders)
- SQLite database configuration
- Alembic migration scripts
- API endpoints for authentication, users, products, categories, and orders
- JWT authentication
- Comprehensive documentation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-05-11 18:51:15 +00:00

108 lines
2.9 KiB
Python

from typing import Any, List
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from app.api.deps import get_current_admin_user, get_db
from app.crud.product import category
from app.models.user import User
from app.schemas.product import Category, CategoryCreate, CategoryUpdate
router = APIRouter()
@router.get("/", response_model=List[Category])
def read_categories(
db: Session = Depends(get_db),
skip: int = 0,
limit: int = 100,
) -> Any:
"""
Retrieve categories.
"""
return category.get_multi(db, skip=skip, limit=limit)
@router.post("/", response_model=Category)
def create_category(
*,
db: Session = Depends(get_db),
category_in: CategoryCreate,
current_user: User = Depends(get_current_admin_user),
) -> Any:
"""
Create new category. Only for admins.
"""
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("/{category_id}", response_model=Category)
def read_category(
*,
db: Session = Depends(get_db),
category_id: int,
) -> Any:
"""
Get category by ID.
"""
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(
*,
db: Session = Depends(get_db),
category_id: int,
category_in: CategoryUpdate,
current_user: User = Depends(get_current_admin_user),
) -> Any:
"""
Update a category. Only for admins.
"""
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",
)
if category_in.name:
existing = category.get_by_name(db, name=category_in.name)
if existing and existing.id != category_id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Category with this name already exists",
)
return category.update(db, db_obj=db_category, obj_in=category_in)
@router.delete("/{category_id}", response_model=Category)
def delete_category(
*,
db: Session = Depends(get_db),
category_id: int,
current_user: User = Depends(get_current_admin_user),
) -> Any:
"""
Delete a category. Only for admins.
"""
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.remove(db, id=category_id)