complete updating the endpoints and models

This commit is contained in:
Georges-Antoine Assi
2024-12-20 22:41:56 -05:00
parent 0850c0cbcf
commit 3fcce6606c
26 changed files with 332 additions and 201 deletions

View File

@@ -1,6 +1,8 @@
from typing import Sequence
from decorators.database import begin_session
from models.collection import Collection
from sqlalchemy import Select, delete, select, update
from sqlalchemy import delete, select, update
from sqlalchemy.orm import Session
from .base_handler import DBBaseHandler
@@ -10,10 +12,17 @@ class DBCollectionsHandler(DBBaseHandler):
@begin_session
def add_collection(
self, collection: Collection, session: Session = None
) -> Collection | None:
) -> Collection:
collection = session.merge(collection)
session.flush()
return session.scalar(select(Collection).filter_by(id=collection.id).limit(1))
new_collection = session.scalar(
select(Collection).filter_by(id=collection.id).limit(1)
)
if not new_collection:
raise ValueError("Could not find newly created collection")
return new_collection
@begin_session
def get_collection(self, id: int, session: Session = None) -> Collection | None:
@@ -28,9 +37,9 @@ class DBCollectionsHandler(DBBaseHandler):
)
@begin_session
def get_collections(self, session: Session = None) -> Select[tuple[Collection]]:
def get_collections(self, session: Session = None) -> Sequence[Collection]:
return (
session.scalars(select(Collection).order_by(Collection.name.asc())) # type: ignore[attr-defined]
session.scalars(select(Collection).order_by(Collection.name.asc()))
.unique()
.all()
)
@@ -38,7 +47,7 @@ class DBCollectionsHandler(DBBaseHandler):
@begin_session
def get_collections_by_rom_id(
self, rom_id: int, session: Session = None
) -> list[Collection]:
) -> Sequence[Collection]:
return session.scalars(
select(Collection).filter(Collection.roms.contains(rom_id))
).all()
@@ -56,8 +65,8 @@ class DBCollectionsHandler(DBBaseHandler):
return session.query(Collection).filter_by(id=id).one()
@begin_session
def delete_collection(self, id: int, session: Session = None) -> int:
return session.execute(
def delete_collection(self, id: int, session: Session = None) -> None:
session.execute(
delete(Collection)
.where(Collection.id == id)
.execution_options(synchronize_session="evaluate")