
- Setup project structure and FastAPI application - Create SQLAlchemy models for users, products, carts, and orders - Implement Alembic migrations - Add CRUD operations and endpoints for all resources - Setup authentication with JWT - Add role-based access control - Update documentation
120 lines
3.1 KiB
Python
120 lines
3.1 KiB
Python
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.v1.deps import get_current_active_superuser
|
|
from app.core.deps import get_db
|
|
from app.crud.product import product as product_crud
|
|
from app.models.user import User
|
|
from app.schemas.product import Product, ProductCreate, ProductUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Product])
|
|
def read_products(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
search: Optional[str] = None,
|
|
) -> Any:
|
|
"""
|
|
Retrieve products.
|
|
|
|
If search query is provided, returns products matching the search query.
|
|
Otherwise, returns all products with pagination.
|
|
"""
|
|
if search:
|
|
products = product_crud.search_products(
|
|
db, query=search, skip=skip, limit=limit
|
|
)
|
|
else:
|
|
products = product_crud.get_multi(db, skip=skip, limit=limit)
|
|
return products
|
|
|
|
|
|
@router.post("/", response_model=Product)
|
|
def create_product(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
product_in: ProductCreate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new product.
|
|
|
|
Only superusers can create products.
|
|
"""
|
|
product = product_crud.get_product_by_name(db, name=product_in.name)
|
|
if product:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="A product with this name already exists",
|
|
)
|
|
product = product_crud.create(db, obj_in=product_in)
|
|
return product
|
|
|
|
|
|
@router.get("/{id}", response_model=Product)
|
|
def read_product(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
) -> Any:
|
|
"""
|
|
Get product by ID.
|
|
"""
|
|
product = product_crud.get(db, id=id)
|
|
if not product:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Product not found",
|
|
)
|
|
return product
|
|
|
|
|
|
@router.put("/{id}", response_model=Product)
|
|
def update_product(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
product_in: ProductUpdate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Update a product.
|
|
|
|
Only superusers can update products.
|
|
"""
|
|
product = product_crud.get(db, id=id)
|
|
if not product:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Product not found",
|
|
)
|
|
product = product_crud.update(db, db_obj=product, obj_in=product_in)
|
|
return product
|
|
|
|
|
|
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_product(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete a product.
|
|
|
|
Only superusers can delete products.
|
|
"""
|
|
product = product_crud.get(db, id=id)
|
|
if not product:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Product not found",
|
|
)
|
|
product_crud.remove(db, id=id)
|
|
return None
|