in/crud.py

35 lines
735 B
Python
Raw Permalink Normal View History

from enum import Enum
2024-01-14 18:56:43 +01:00
2022-08-04 05:41:51 +02:00
from sqlalchemy import DECIMAL, cast
2024-01-14 18:56:43 +01:00
from sqlalchemy.orm import Session
import models
import schemas
2022-08-04 05:41:51 +02:00
def get_items(db: Session) -> list[models.Item]:
2022-08-06 02:38:47 +02:00
return db.query(models.Item).all()
2022-08-04 05:41:51 +02:00
def put_item(db: Session, id: str, item: schemas.Item):
2024-01-14 18:56:43 +01:00
updated = bool(
db.query(models.Item).filter(models.Item.id == id).update(item.dict())
)
2022-08-04 05:41:51 +02:00
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
2022-08-04 05:41:51 +02:00
def delete_item(db: Session, id: str):
db.query(models.Item).filter(models.Item.id == id).delete()
db.commit()