diff --git a/backend/endpoints/responses/rom.py b/backend/endpoints/responses/rom.py index 7e17a7448..c400d6bab 100644 --- a/backend/endpoints/responses/rom.py +++ b/backend/endpoints/responses/rom.py @@ -98,6 +98,8 @@ class RomSchema(BaseModel): created_at: datetime updated_at: datetime + user_rom_props: UserRomPropsSchema = Field(default_factory=list) + class Config: from_attributes = True @@ -120,7 +122,6 @@ class DetailedRomSchema(RomSchema): user_saves: list[SaveSchema] = Field(default_factory=list) user_states: list[StateSchema] = Field(default_factory=list) user_screenshots: list[ScreenshotSchema] = Field(default_factory=list) - user_rom_props: list[UserRomPropsSchema] = Field(default_factory=list) @classmethod def from_orm_with_request( @@ -128,7 +129,6 @@ class DetailedRomSchema(RomSchema): ) -> "DetailedRomSchema": rom = cls.model_validate(db_rom) user_id = request.user.id - rom.sibling_roms = [ RomSchema.model_validate(r) for r in db_rom.get_sibling_roms() ] @@ -143,8 +143,6 @@ class DetailedRomSchema(RomSchema): for s in db_rom.screenshots if s.user_id == user_id ] - rom.user_rom_props = UserRomPropsSchema.for_user(db_rom, user_id) - return rom diff --git a/backend/endpoints/rom.py b/backend/endpoints/rom.py index f023df37f..87fe42ac6 100644 --- a/backend/endpoints/rom.py +++ b/backend/endpoints/rom.py @@ -114,6 +114,7 @@ def get_roms( order_by=order_by.lower(), order_dir=order_dir.lower(), limit=limit, + user_id=request.user.id, ) diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index 47ab29176..194a5aaf0 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -69,21 +69,22 @@ class DBRomsHandler(DBBaseHandler): search_term: str = "", order_by: str = "name", order_dir: str = "asc", - limit: int = None, + limit: int | None = None, + user_id: int | None = None, query: Query = None, session: Session = None, ) -> list[Rom] | Rom | None: - return ( - session.scalar(query.filter_by(id=id).limit(1)) - if id - else session.scalars( + # TODO: get user_rom_props from user_id + if id: + return session.scalar(query.filter_by(id=id).limit(1)) + else: + return session.scalars( self._order( self._filter(select(Rom), platform_id, search_term), order_by, order_dir, ).limit(limit) ).all() - ) @begin_session @with_assets diff --git a/frontend/src/__generated__/models/DetailedRomSchema.ts b/frontend/src/__generated__/models/DetailedRomSchema.ts index ebb45f6db..4d78bad12 100644 --- a/frontend/src/__generated__/models/DetailedRomSchema.ts +++ b/frontend/src/__generated__/models/DetailedRomSchema.ts @@ -50,11 +50,11 @@ export type DetailedRomSchema = { full_path: string; created_at: string; updated_at: string; + user_rom_props?: Array; merged_screenshots: Array; sibling_roms?: Array; user_saves?: Array; user_states?: Array; user_screenshots?: Array; - user_rom_props?: Array; readonly sort_comparator: string; }; diff --git a/frontend/src/__generated__/models/RomSchema.ts b/frontend/src/__generated__/models/RomSchema.ts index 80f8b6d3a..51baa7de8 100644 --- a/frontend/src/__generated__/models/RomSchema.ts +++ b/frontend/src/__generated__/models/RomSchema.ts @@ -5,6 +5,7 @@ import type { RomIGDBMetadata } from "./RomIGDBMetadata"; import type { RomMobyMetadata } from "./RomMobyMetadata"; +import type { UserRomPropsSchema } from "./UserRomPropsSchema"; export type RomSchema = { id: number; @@ -45,5 +46,6 @@ export type RomSchema = { full_path: string; created_at: string; updated_at: string; + user_rom_props?: Array; readonly sort_comparator: string; }; diff --git a/frontend/src/stores/roms.ts b/frontend/src/stores/roms.ts index 772486308..6d3e7f472 100644 --- a/frontend/src/stores/roms.ts +++ b/frontend/src/stores/roms.ts @@ -5,6 +5,7 @@ import type { ExtractPiniaStoreType } from "@/types"; import { groupBy, uniqBy } from "lodash"; import { nanoid } from "nanoid"; import { defineStore } from "pinia"; +import storeAuth from "@/stores/auth"; import storeGalleryFilter from "./galleryFilter"; type GalleryFilterStore = ExtractPiniaStoreType; @@ -51,6 +52,8 @@ export default defineStore("roms", { return; } + const auth = storeAuth(); + // Group roms by external id this._grouped = Object.values( groupBy( @@ -61,12 +64,14 @@ export default defineStore("roms", { ), ) .map((games) => { - const primaryGame = games.shift(); - // const favSiblingIndex = games.findIndex((game) => game.fav_sibling); - // const primaryGame = - // favSiblingIndex !== -1 - // ? games.splice(favSiblingIndex, 1)[0] - // : games.shift(); + const favSiblingIndex = games.findIndex((game) => + game.user_rom_props?.find((prop) => prop.user_id === auth.user?.id), + ); + console.log(favSiblingIndex); + const primaryGame = + favSiblingIndex !== -1 + ? games.splice(favSiblingIndex, 1)[0] + : games.shift(); return { ...(primaryGame as SimpleRom),