Automated Action 538a985c8e Build e-commerce API with FastAPI and SQLite
Create a complete e-commerce application with the following features:
- User authentication and authorization
- Product and category management
- Shopping cart functionality
- Order processing
- Database migrations with Alembic
- Comprehensive documentation
2025-05-26 11:05:27 +00:00

50 lines
1.3 KiB
Python

import uuid
from typing import List, Optional
from sqlalchemy.orm import Session
from app.models.category import Category
from app.schemas.category import CategoryCreate, CategoryUpdate
def get_by_id(db: Session, category_id: str) -> Optional[Category]:
return db.query(Category).filter(Category.id == category_id).first()
def get_by_name(db: Session, name: str) -> Optional[Category]:
return db.query(Category).filter(Category.name == name).first()
def get_multi(db: Session, *, skip: int = 0, limit: int = 100) -> List[Category]:
return db.query(Category).offset(skip).limit(limit).all()
def create(db: Session, *, obj_in: CategoryCreate) -> Category:
db_obj = Category(
id=str(uuid.uuid4()),
name=obj_in.name,
description=obj_in.description,
)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def update(db: Session, *, db_obj: Category, obj_in: CategoryUpdate) -> Category:
update_data = obj_in.dict(exclude_unset=True)
for field, value in update_data.items():
setattr(db_obj, field, value)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def delete(db: Session, *, category_id: str) -> Category:
obj = db.query(Category).get(category_id)
db.delete(obj)
db.commit()
return obj