Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions backend/endpoints/responses/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,16 @@ def sort_user_states(cls, v: list[StateSchema]) -> list[StateSchema]:
@field_validator("user_screenshots")
def sort_user_screenshots(cls, v: list[ScreenshotSchema]) -> list[ScreenshotSchema]:
return sorted(v, key=lambda x: x.created_at, reverse=True)


class RomFiltersDict(TypedDict):
genres: list[str]
franchises: list[str]
collections: list[str]
companies: list[str]
game_modes: list[str]
age_ratings: list[str]
player_counts: list[str]
regions: list[str]
languages: list[str]
platforms: list[int]
48 changes: 45 additions & 3 deletions backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from endpoints.responses.rom import (
DetailedRomSchema,
RomFileSchema,
RomFiltersDict,
RomUserSchema,
SimpleRomSchema,
UserNoteSchema,
Expand Down Expand Up @@ -186,6 +187,7 @@ class CustomLimitOffsetParams(LimitOffsetParams):
class CustomLimitOffsetPage[T: BaseModel](LimitOffsetPage[T]):
char_index: dict[str, int]
rom_id_index: list[int]
filter_values: RomFiltersDict
__params_type__ = CustomLimitOffsetParams


Expand All @@ -196,6 +198,9 @@ def get_roms(
bool,
Query(description="Whether to get the char index."),
] = True,
with_filter_values: Annotated[
bool, Query(description="Whether to return filter values.")
] = True,
search_term: Annotated[
str | None,
Query(description="Search term to filter roms."),
Expand Down Expand Up @@ -407,15 +412,15 @@ def get_roms(
] = "asc",
) -> CustomLimitOffsetPage[SimpleRomSchema]:
"""Retrieve roms."""
query, order_by_attr = db_rom_handler.get_roms_query(
unfiltered_query, order_by_attr = db_rom_handler.get_roms_query(
user_id=request.user.id,
order_by=order_by.lower(),
order_dir=order_dir.lower(),
)

# Filter down the query
query = db_rom_handler.filter_roms(
query=query,
query=unfiltered_query,
user_id=request.user.id,
platform_ids=platform_ids,
collection_id=collection_id,
Expand Down Expand Up @@ -460,6 +465,33 @@ def get_roms(
)
char_index_dict = {char: index for (char, index) in char_index}

filter_values = RomFiltersDict(
genres=[],
franchises=[],
collections=[],
companies=[],
game_modes=[],
age_ratings=[],
player_counts=[],
regions=[],
languages=[],
platforms=[],
)
if with_filter_values:
# We use the unfiltered query so applied filters don't affect the list
filter_query = db_rom_handler.filter_roms(
query=unfiltered_query,
user_id=request.user.id,
platform_ids=platform_ids,
collection_id=collection_id,
virtual_collection_id=virtual_collection_id,
smart_collection_id=smart_collection_id,
search_term=search_term,
)
query_filters = db_rom_handler.with_filter_values(query=filter_query)
# trunk-ignore(mypy/typeddict-item)
filter_values = RomFiltersDict(**query_filters)

# Get all ROM IDs in order for the additional data
with sync_session.begin() as session:
rom_id_index = session.scalars(query.with_only_columns(Rom.id)).all() # type: ignore
Expand All @@ -473,6 +505,7 @@ def get_roms(
additional_data={
"char_index": char_index_dict,
"rom_id_index": rom_id_index,
"filter_values": filter_values,
},
)

Expand Down Expand Up @@ -671,6 +704,15 @@ def get_rom_by_hash(
return DetailedRomSchema.from_orm_with_request(rom, request)


@protected_route(router.get, "/filters", [Scope.ROMS_READ])
async def get_rom_filters(request: Request) -> RomFiltersDict:
from handler.database import db_rom_handler

filters = db_rom_handler.get_rom_filters()
# trunk-ignore(mypy/typeddict-item)
return RomFiltersDict(**filters)


@protected_route(
router.get,
"/{id}",
Expand Down Expand Up @@ -1502,7 +1544,7 @@ async def update_rom_user(

@protected_route(
router.get,
"files/{id}",
"/files/{id}",
[Scope.ROMS_READ],
responses={status.HTTP_404_NOT_FOUND: {}},
)
Expand Down
Loading
Loading