todoappbackend-2kj8pz/app/crud/crud_category.py
Automated Action d60767d0ba Add categories and tags features
- Create Category and Tag models
- Create TodoTag association table
- Add category_id to Todo model
- Create Alembic migration for new tables
- Create schemas for Category and Tag
- Update Todo schemas to include Category and Tags
- Create CRUD operations for Categories and Tags
- Update Todo CRUD operations to handle categories and tags
- Create API endpoints for categories and tags
- Update Todo API endpoints with category and tag filtering
- Update documentation
2025-06-17 02:52:26 +00:00

51 lines
1.5 KiB
Python

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_category(db: Session, category_id: int) -> Optional[Category]:
return db.query(Category).filter(Category.id == category_id).first()
def get_category_by_name(db: Session, name: str, owner_id: int) -> Optional[Category]:
return db.query(Category).filter(Category.name == name, Category.owner_id == owner_id).first()
def get_categories(
db: Session, owner_id: int, skip: int = 0, limit: int = 100
) -> List[Category]:
return db.query(Category).filter(Category.owner_id == owner_id).offset(skip).limit(limit).all()
def create_category(db: Session, category_in: CategoryCreate, owner_id: int) -> Category:
db_category = Category(
name=category_in.name,
description=category_in.description,
owner_id=owner_id
)
db.add(db_category)
db.commit()
db.refresh(db_category)
return db_category
def update_category(
db: Session, db_obj: Category, obj_in: CategoryUpdate
) -> Category:
update_data = obj_in.model_dump(exclude_unset=True)
for field in update_data:
setattr(db_obj, field, update_data[field])
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def delete_category(db: Session, category_id: int) -> Category:
category = db.query(Category).filter(Category.id == category_id).first()
db.delete(category)
db.commit()
return category