Skip to content

Commit

Permalink
Merge pull request #29 from mlibrary/tidy-switch-and-named-tuple
Browse files Browse the repository at this point in the history
Tidy switch and named tuple
  • Loading branch information
niquerio authored Nov 25, 2024
2 parents ccf9b9d + 1428c39 commit 5d81e57
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 554 deletions.
59 changes: 35 additions & 24 deletions aim/digifeeds/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class NotFoundError(Exception):
pass


class AlreadyExistsError(Exception):
pass


def get_item(db: Session, barcode: str):
"""
Get item from the database
Expand Down Expand Up @@ -69,33 +73,33 @@ def get_items(

def get_items_statement(filter: schemas.ItemFilters = None):
stmnt = select(models.Item)

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


Expand All @@ -110,6 +114,13 @@ def add_item(db: Session, item: schemas.ItemCreate):
aim.digifeeds.database.models.Item: Item object
"""
db_item = models.Item(barcode=item.barcode)

stmnt = select(models.Item).filter_by(barcode=item.barcode)
already_in_db_item = db.scalars(stmnt).first()

if already_in_db_item is not None:
raise AlreadyExistsError()

db.add(db_item)
db.commit()
db.refresh(db_item)
Expand Down
9 changes: 4 additions & 5 deletions aim/digifeeds/database/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from aim.digifeeds.database import crud, schemas
from aim.digifeeds.database.crud import NotFoundError
from aim.digifeeds.database.crud import NotFoundError, AlreadyExistsError
from aim.services import S

# This is here so SessionLocal won't have a problem in tests in github
Expand Down Expand Up @@ -117,12 +117,11 @@ def create_item(

item = schemas.ItemCreate(barcode=barcode)
try:
crud.get_item(barcode=item.barcode, db=db)
except NotFoundError:
db_item = crud.add_item(item=item, db=db)
return db_item
else:
except AlreadyExistsError:
raise HTTPException(status_code=400, detail="Item already exists")
else:
return db_item


desc_put_404 = """
Expand Down
6 changes: 4 additions & 2 deletions aim/services.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import NamedTuple
# from typing import NamedTuple
from dataclasses import dataclass
import os
import sqlalchemy as sa
import structlog
Expand Down Expand Up @@ -32,7 +33,8 @@
structlog.configure(processors)


class Services(NamedTuple):
@dataclass(frozen=True)
class Services:
"""
Global Configuration Services
"""
Expand Down
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"sphinx.ext.autodoc",
"myst_parser",
"sphinxcontrib.mermaid",
"sphinx_toolbox.more_autodoc.autonamedtuple",
]
autosummary_generate = True

Expand Down
Loading

0 comments on commit 5d81e57

Please sign in to comment.