Automated Action b00b44023a Add FastAPI REST API with SQLite database
- Set up project structure
- Add database models and SQLAlchemy setup
- Configure Alembic for migrations
- Create API endpoints for items
- Add health check endpoint
- Update documentation

generated with BackendIM... (backend.im)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-05-15 22:33:58 +00:00

52 lines
1.2 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import Session
from app.models.item import Item
from app.schemas.item import ItemCreate, ItemUpdate
def get(db: Session, item_id: int) -> Optional[Item]:
return db.query(Item).filter(Item.id == item_id).first()
def get_by_name(db: Session, name: str) -> Optional[Item]:
return db.query(Item).filter(Item.name == name).first()
def get_multi(
db: Session, *, skip: int = 0, limit: int = 100
) -> List[Item]:
return db.query(Item).offset(skip).limit(limit).all()
def create(db: Session, *, obj_in: ItemCreate) -> Item:
db_obj = Item(
name=obj_in.name,
description=obj_in.description,
is_active=obj_in.is_active,
)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
return db_obj
def update(
db: Session, *, db_obj: Item, obj_in: ItemUpdate
) -> Item:
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 remove(db: Session, *, item_id: int) -> Optional[Item]:
obj = db.query(Item).get(item_id)
if obj:
db.delete(obj)
db.commit()
return obj