Automated Action 6776db0bbd Create REST API with FastAPI and SQLite
- Set up project structure with FastAPI
- Configure SQLAlchemy with SQLite
- Implement user and item models
- Set up Alembic for database migrations
- Create CRUD operations for models
- Implement API endpoints for users and items
- Add authentication functionality
- Add health check endpoint
- Configure Ruff for linting
- Update README with comprehensive documentation
2025-05-22 11:40:52 +00:00

47 lines
1.1 KiB
Python

from typing import List
from sqlalchemy.orm import Session
from app.crud.base import CRUDBase
from app.models.item import Item
from app.schemas.item import ItemCreate, ItemUpdate
class CRUDItem(CRUDBase[Item, ItemCreate, ItemUpdate]):
"""
CRUD operations for Item model.
"""
def get_multi_by_owner(
self, db: Session, *, owner_id: int, skip: int = 0, limit: int = 100
) -> List[Item]:
"""
Get multiple items by owner ID with pagination.
"""
return (
db.query(self.model)
.filter(Item.owner_id == owner_id)
.offset(skip)
.limit(limit)
.all()
)
def create_with_owner(
self, db: Session, *, obj_in: ItemCreate, owner_id: int
) -> Item:
"""
Create a new item with owner ID.
"""
db_obj = Item(
title=obj_in.title,
description=obj_in.description,
owner_id=owner_id,
)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
# Create an instance for convenience
item = CRUDItem(Item)