
This API provides endpoints for: - Product and inventory management - Customer management - Order processing - Payment processing with Stripe integration - User authentication generated with BackendIM... (backend.im)
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
from typing import List, Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud.base import CRUDBase
|
|
from app.models.product import Product
|
|
from app.schemas.product import ProductCreate, ProductUpdate
|
|
|
|
|
|
class CRUDProduct(CRUDBase[Product, ProductCreate, ProductUpdate]):
|
|
def get_by_sku(self, db: Session, *, sku: str) -> Optional[Product]:
|
|
return db.query(Product).filter(Product.sku == sku).first()
|
|
|
|
def get_active(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[Product]:
|
|
return db.query(Product).filter(Product.is_active == True).offset(skip).limit(limit).all()
|
|
|
|
def create_with_inventory(self, db: Session, *, obj_in: ProductCreate, inventory_quantity: int = 0) -> Product:
|
|
from app.models.inventory import Inventory
|
|
|
|
# Create product
|
|
db_obj = Product(
|
|
name=obj_in.name,
|
|
description=obj_in.description,
|
|
sku=obj_in.sku,
|
|
price=obj_in.price,
|
|
image_url=obj_in.image_url,
|
|
is_active=obj_in.is_active
|
|
)
|
|
db.add(db_obj)
|
|
db.flush()
|
|
|
|
# Create inventory record
|
|
inventory = Inventory(
|
|
product_id=db_obj.id,
|
|
quantity=inventory_quantity
|
|
)
|
|
db.add(inventory)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
|
|
return db_obj
|
|
|
|
|
|
product = CRUDProduct(Product) |