Files
romm/backend/handler/database/collections_handler.py
Michael Manganiello 7825bce7b8 fix: Use proper JSON contains function for PostgreSQL
Fix `json_array_contains_value` function to use the `@>` operator for
checking if a JSON array contains a value in PostgreSQL. This is
necessary because the `has_key` function only works for string values.

Also, remove `get_rom_collections` method, as it was doing the same
thing as `get_collections_by_rom_id`.

Fixes #1441.
2025-01-08 21:07:01 -03:00

78 lines
2.5 KiB
Python

from collections.abc import Sequence
from typing import Any
from decorators.database import begin_session
from models.collection import Collection
from sqlalchemy import Select, delete, select, update
from sqlalchemy.orm import Session
from sqlalchemy.sql import ColumnExpressionArgument
from utils.database import json_array_contains_value
from .base_handler import DBBaseHandler
class DBCollectionsHandler(DBBaseHandler):
@begin_session
def add_collection(
self, collection: Collection, session: Session = None
) -> Collection | None:
collection = session.merge(collection)
session.flush()
return session.scalar(select(Collection).filter_by(id=collection.id).limit(1))
@begin_session
def get_collection(self, id: int, session: Session = None) -> Collection | None:
return session.scalar(select(Collection).filter_by(id=id).limit(1))
@begin_session
def get_collection_by_name(
self, name: str, user_id: int, session: Session = None
) -> Collection | None:
return session.scalar(
select(Collection).filter_by(name=name, user_id=user_id).limit(1)
)
@begin_session
def get_collections(self, session: Session = None) -> Select[tuple[Collection]]:
return (
session.scalars(select(Collection).order_by(Collection.name.asc())) # type: ignore[attr-defined]
.unique()
.all()
)
@begin_session
def get_collections_by_rom_id(
self,
rom_id: int,
*,
order_by: Sequence[str | ColumnExpressionArgument[Any]] | None = None,
session: Session = None,
) -> list[Collection]:
query = select(Collection).filter(
json_array_contains_value(Collection.roms, rom_id, session=session)
)
if order_by is not None:
query = query.order_by(*order_by)
return session.scalars(query).all()
@begin_session
def update_collection(
self, id: int, data: dict, session: Session = None
) -> Collection:
session.execute(
update(Collection)
.where(Collection.id == id)
.values(**data)
.execution_options(synchronize_session="evaluate")
)
return session.query(Collection).filter_by(id=id).one()
@begin_session
def delete_collection(self, id: int, session: Session = None) -> int:
return session.execute(
delete(Collection)
.where(Collection.id == id)
.execution_options(synchronize_session="evaluate")
)