Skip to content

Commit

Permalink
get collections for detailed rom info
Browse files Browse the repository at this point in the history
  • Loading branch information
zurdi15 committed Jul 3, 2024
1 parent 9a42838 commit 5126c56
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions backend/endpoints/responses/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import NotRequired, get_type_hints

from endpoints.responses.assets import SaveSchema, ScreenshotSchema, StateSchema
from endpoints.responses.collection import CollectionSchema
from fastapi import Request
from fastapi.responses import StreamingResponse
from handler.metadata.igdb_handler import IGDBMetadata
Expand Down Expand Up @@ -147,6 +148,7 @@ class DetailedRomSchema(RomSchema):
user_states: list[StateSchema] = Field(default_factory=list)
user_screenshots: list[ScreenshotSchema] = Field(default_factory=list)
user_notes: list[UserNotesSchema] = Field(default_factory=list)
collections: list[CollectionSchema] = Field(default_factory=list)

Check failure on line 151 in backend/endpoints/responses/rom.py

View check run for this annotation

Trunk.io / Trunk Check

mypy(assignment)

[new] Incompatible types in assignment (expression has type "list[CollectionSchema]", base class "RomSchema" defined the type as "list[str]")

Check notice on line 151 in backend/endpoints/responses/rom.py

View check run for this annotation

Trunk.io / Trunk Check

mypy(note)

[new] "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance. Consider using "Sequence" instead, which is covariant

Check failure on line 151 in backend/endpoints/responses/rom.py

View workflow job for this annotation

GitHub Actions / Trunk Check

mypy(assignment)

[new] Incompatible types in assignment (expression has type "list[CollectionSchema]", base class "RomSchema" defined the type as "list[str]")

Check notice on line 151 in backend/endpoints/responses/rom.py

View workflow job for this annotation

GitHub Actions / Trunk Check

mypy(note)

[new] "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance. Consider using "Sequence" instead, which is covariant

@classmethod
def from_orm_with_request(cls, db_rom: Rom, request: Request) -> DetailedRomSchema:
Expand All @@ -169,6 +171,9 @@ def from_orm_with_request(cls, db_rom: Rom, request: Request) -> DetailedRomSche
for s in db_rom.screenshots
if s.user_id == user_id
]
rom.collections = [
CollectionSchema.model_validate(c) for c in db_rom.get_collections()
]

return rom

Expand Down
14 changes: 14 additions & 0 deletions backend/handler/database/roms_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ def get_sibling_roms(
)
).all()

@begin_session
def get_rom_collections(
self, rom_id: int, session: Session = None
) -> list[Collection]:
return (
session.scalars(
select(Collection)
.filter(Collection.roms.contains([rom_id]))
.order_by(Collection.name.asc())
)
.unique()
.all()
)

@begin_session
def update_rom(self, id: int, data: dict, session: Session = None) -> Rom:
return session.execute(
Expand Down
6 changes: 6 additions & 0 deletions backend/models/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

if TYPE_CHECKING:
from models.assets import Save, Screenshot, State
from models.collection import Collection
from models.platform import Platform
from models.user import User

Expand Down Expand Up @@ -103,6 +104,11 @@ def get_sibling_roms(self) -> list[Rom]:

return db_rom_handler.get_sibling_roms(self)

def get_collections(self) -> list[Collection]:
from handler.database import db_rom_handler

return db_rom_handler.get_rom_collections(self.id)

# Metadata fields
@property
def alternative_names(self) -> list[str]:
Expand Down

0 comments on commit 5126c56

Please sign in to comment.