Skip to content

Commit

Permalink
Merge pull request #1591 from rommapp/simpler-rom-response-object
Browse files Browse the repository at this point in the history
Simpler rom response object
  • Loading branch information
gantoine authored Feb 8, 2025
2 parents c27eca2 + 0d32bb7 commit 5d9cbaa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions backend/endpoints/responses/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ def sort_comparator(self) -> str:
.lower()
)

@classmethod
def from_orm_with_request(cls, db_rom: Rom, _request: Request) -> RomSchema:
return cls.model_validate(db_rom)


class SimpleRomSchema(RomSchema):
sibling_roms: list[RomSchema]
Expand Down
13 changes: 10 additions & 3 deletions backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
)
from decorators.auth import protected_route
from endpoints.responses import MessageResponse
from endpoints.responses.rom import DetailedRomSchema, RomUserSchema, SimpleRomSchema
from endpoints.responses.rom import (
DetailedRomSchema,
RomSchema,
RomUserSchema,
SimpleRomSchema,
)
from exceptions.endpoint_exceptions import RomNotFoundInDatabaseException
from exceptions.fs_exceptions import RomAlreadyExistsException
from fastapi import HTTPException, Request, UploadFile, status
Expand Down Expand Up @@ -112,7 +117,8 @@ def get_roms(
offset: int | None = None,
order_by: str = "name",
order_dir: str = "asc",
) -> list[SimpleRomSchema]:
with_extra: bool = True,
) -> list[SimpleRomSchema | RomSchema]:
"""Get roms endpoint
Args:
Expand Down Expand Up @@ -157,7 +163,8 @@ def get_roms(
detail="Invalid order_by field",
)

roms = [SimpleRomSchema.from_orm_with_request(rom, request) for rom in roms]
SelectedSchema = SimpleRomSchema if with_extra else RomSchema
roms = [SelectedSchema.from_orm_with_request(rom, request) for rom in roms]
return [rom for rom in roms if rom]


Expand Down

0 comments on commit 5d9cbaa

Please sign in to comment.