35 lines
929 B
Python
35 lines
929 B
Python
from typing import List, Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud.base import CRUDBase
|
|
from app.models.category import Category
|
|
from app.schemas.category import CategoryCreate, CategoryUpdate
|
|
|
|
|
|
class CRUDCategory(CRUDBase[Category, CategoryCreate, CategoryUpdate]):
|
|
"""
|
|
CRUD operations for Category model.
|
|
"""
|
|
def get_by_name(self, db: Session, *, name: str) -> Optional[Category]:
|
|
"""
|
|
Get a category by name.
|
|
"""
|
|
return db.query(Category).filter(Category.name == name).first()
|
|
|
|
def get_multi_with_items(
|
|
self, db: Session, *, skip: int = 0, limit: int = 100
|
|
) -> List[Category]:
|
|
"""
|
|
Get multiple categories with items.
|
|
"""
|
|
return (
|
|
db.query(Category)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
|
|
# Create a singleton instance
|
|
category = CRUDCategory(Category) |