in/crud.py

34 lines
735 B
Python

from enum import Enum
from sqlalchemy import DECIMAL, cast
from sqlalchemy.orm import Session
import models
import schemas
def get_items(db: Session) -> list[models.Item]:
return db.query(models.Item).all()
def put_item(db: Session, id: str, item: schemas.Item):
updated = bool(
db.query(models.Item).filter(models.Item.id == id).update(item.dict())
)
if not updated:
db.add(models.Item(**item.dict()))
db.commit()
if updated:
return PutItemResult.UPDATED
else:
return PutItemResult.ADDED
class PutItemResult(Enum):
ADDED = 1
UPDATED = 2
def delete_item(db: Session, id: str):
db.query(models.Item).filter(models.Item.id == id).delete()
db.commit()