
- Set up project structure with FastAPI and SQLite - Create database models for inventory management - Set up SQLAlchemy and Alembic for database migrations - Create initial database migrations - Implement CRUD operations for products, categories, suppliers - Implement stock movement tracking and inventory management - Add authentication and user management - Add API endpoints for all entities - Add health check endpoint - Update README with project information and usage instructions
42 lines
1.9 KiB
Python
42 lines
1.9 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_by_barcode(self, db: Session, *, barcode: str) -> Optional[Product]:
|
|
return db.query(Product).filter(Product.barcode == barcode).first()
|
|
|
|
def get_by_category(self, db: Session, *, category_id: str, skip: int = 0, limit: int = 100) -> List[Product]:
|
|
return db.query(Product).filter(Product.category_id == category_id).offset(skip).limit(limit).all()
|
|
|
|
def get_by_supplier(self, db: Session, *, supplier_id: str, skip: int = 0, limit: int = 100) -> List[Product]:
|
|
return db.query(Product).filter(Product.supplier_id == supplier_id).offset(skip).limit(limit).all()
|
|
|
|
def get_low_stock(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[Product]:
|
|
return db.query(Product).filter(Product.current_stock <= Product.reorder_level).offset(skip).limit(limit).all()
|
|
|
|
def search(self, db: Session, *, query: str, skip: int = 0, limit: int = 100) -> List[Product]:
|
|
return db.query(Product).filter(
|
|
Product.name.ilike(f"%{query}%") |
|
|
Product.sku.ilike(f"%{query}%") |
|
|
Product.barcode.ilike(f"%{query}%")
|
|
).offset(skip).limit(limit).all()
|
|
|
|
def update_stock(self, db: Session, *, product_id: str, quantity: int) -> Product:
|
|
product = self.get(db, id=product_id)
|
|
if product:
|
|
product.current_stock += quantity
|
|
db.add(product)
|
|
db.commit()
|
|
db.refresh(product)
|
|
return product
|
|
|
|
|
|
product = CRUDProduct(Product) |