Automated Action 7fdb11e728 Create FastAPI REST API with SQLite database and CRUD operations
- Set up project structure with FastAPI, SQLAlchemy, and Alembic
- Create database models for User and Item
- Implement CRUD operations for all models
- Create API endpoints with validation
- Add health check endpoint
- Configure CORS middleware
- Set up database migrations
- Add comprehensive documentation in README
2025-05-21 08:51:23 +00:00

34 lines
920 B
Python

from typing import List
from fastapi.encoders import jsonable_encoder
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]):
def create_with_owner(
self, db: Session, *, obj_in: ItemCreate, owner_id: int
) -> Item:
obj_in_data = jsonable_encoder(obj_in)
db_obj = self.model(**obj_in_data, owner_id=owner_id)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def get_multi_by_owner(
self, db: Session, *, owner_id: int, skip: int = 0, limit: int = 100
) -> List[Item]:
return (
db.query(self.model)
.filter(Item.owner_id == owner_id)
.offset(skip)
.limit(limit)
.all()
)
item = CRUDItem(Item)