Skip to content

Commit

Permalink
changes get_items query to select
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Nov 20, 2024
1 parent 34d6fb7 commit 2449b9d
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions aim/digifeeds/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Operations that act on the digifeeds database
"""

from sqlalchemy import select
from sqlalchemy import select, func
from sqlalchemy.orm import Session, joinedload
from aim.digifeeds.database import schemas
from aim.digifeeds.database import models
Expand Down Expand Up @@ -43,8 +43,8 @@ def get_item(db: Session, barcode: str):


def get_items_total(db: Session, filter: schemas.ItemFilters = None):
query = get_items_query(db=db, filter=filter)
return query.count()
stmnt = get_items_statement(filter=filter)
return db.execute(select(func.count()).select_from(stmnt.subquery())).scalar_one()


def get_items(
Expand All @@ -63,40 +63,40 @@ def get_items(
Returns:
aim.digifeeds.database.models.Item: Item object
"""
query = get_items_query(db=db, filter=filter)
return query.offset(offset).limit(limit).all()
stmnt = get_items_statement(filter=filter).offset(offset).limit(limit)
return db.scalars(stmnt).all()


def get_items_query(db: Session, filter: schemas.ItemFilters = None):
query = db.query(models.Item)
def get_items_statement(filter: schemas.ItemFilters = None):
stmnt = select(models.Item)

if filter == "in_zephir":
query = query.filter(
stmnt = stmnt.where(
models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir")
)
elif filter == "not_in_zephir":
query = query.filter(
stmnt = stmnt.where(
~models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir")
)
elif filter == "pending_deletion":
query = query.filter(
stmnt = stmnt.where(
models.Item.statuses.any(
models.ItemStatus.status_name == "pending_deletion"
)
)
elif filter == "not_pending_deletion":
query = query.filter(
stmnt = stmnt.where(
~models.Item.statuses.any(
models.ItemStatus.status_name == "pending_deletion"
)
)
elif filter == "not_found_in_alma":
query = query.filter(
stmnt = stmnt.where(
models.Item.statuses.any(
models.ItemStatus.status_name == "not_found_in_alma"
)
)
return query
return stmnt


def add_item(db: Session, item: schemas.ItemCreate):
Expand Down

0 comments on commit 2449b9d

Please sign in to comment.