
- Set up FastAPI application structure - Create database models for User, Product, Cart, CartItem, Order, and OrderItem - Set up Alembic for database migrations - Create Pydantic schemas for request/response models - Implement API endpoints for products, cart operations, and checkout process - Add health endpoint - Update README with project details and documentation
42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
from typing import List, Optional
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.product import Product
|
|
from app.schemas.product import ProductCreate, ProductUpdate
|
|
from app.crud.base import CRUDBase
|
|
|
|
|
|
class CRUDProduct(CRUDBase[Product, ProductCreate, ProductUpdate]):
|
|
"""CRUD operations for Product model."""
|
|
|
|
def get_by_sku(self, db: Session, *, sku: str) -> Optional[Product]:
|
|
"""Get a product by SKU."""
|
|
return db.query(Product).filter(Product.sku == sku).first()
|
|
|
|
def get_active(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[Product]:
|
|
"""Get active products only."""
|
|
return db.query(Product).filter(Product.is_active).offset(skip).limit(limit).all()
|
|
|
|
def search_by_name(self, db: Session, *, name: str, skip: int = 0, limit: int = 100) -> List[Product]:
|
|
"""Search products by name."""
|
|
return (
|
|
db.query(Product)
|
|
.filter(Product.name.ilike(f"%{name}%"))
|
|
.filter(Product.is_active)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def update_stock(self, db: Session, *, product_id: int, quantity: int) -> Optional[Product]:
|
|
"""Update product stock quantity."""
|
|
product = self.get(db, id=product_id)
|
|
if product:
|
|
product.stock_quantity = max(0, product.stock_quantity + quantity)
|
|
db.add(product)
|
|
db.commit()
|
|
db.refresh(product)
|
|
return product
|
|
|
|
|
|
product = CRUDProduct(Product) |