Skip to content

Commit

Permalink
support many and order by
Browse files Browse the repository at this point in the history
  • Loading branch information
extreme4all committed Feb 3, 2024
1 parent ed59e70 commit 97b5139
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/api/v2/highscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
@router.get("/highscore/latest")
async def get_highscore_latest(
player_id: int,
label_id: int = None,
many: bool = False,
limit: int = Query(default=10, ge=0, le=10_000),
):
repo = HighscoreLatest()
if many:
data = await repo.get_many(start=player_id, limit=limit)
data = await repo.get_many(start=player_id, limit=limit, label_id=label_id)
else:
data = await repo.get(id=player_id)
return data
Expand Down
2 changes: 2 additions & 0 deletions src/api/v2/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
async def get_player(
player_id: str = None,
player_name: str = None,
label_id: int = None,
greater_than: bool = None,
limit: int = Query(default=1_000, ge=0, le=100_000),
):
Expand All @@ -18,6 +19,7 @@ async def get_player(
player_id=player_id,
player_name=player_name,
greater_than=greater_than,
label_id=label_id,
limit=limit,
)
return data
13 changes: 12 additions & 1 deletion src/app/repositories/highscore.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,22 @@ async def get(self, id: int):
data = [{"name": d.pop("name"), **d["PlayerHiscoreDataLatest"]} for d in data]
return data

async def get_many(self, start: int, limit: int = 5000):
async def get_many(
self,
start: int,
label_id: int = None,
limit: int = 5000,
):
sql: Select = select(self.table, Player.name)
sql = sql.join(target=Player, onclause=self.table.Player_id == Player.id)
sql = sql.where(self.table.Player_id > start)

if label_id:
sql = sql.where(Player.label_id == label_id)

sql = sql.limit(limit)
sql = sql.order_by(self.table.Player_id.asc())

data: list[dict] = await self._simple_execute(sql)
# data = [{"PlayerHiscoreDataLatest":{"total": int, ...}, "name": str}]
data = [{"name": d.pop("name"), **d["PlayerHiscoreDataLatest"]} for d in data]
Expand Down
27 changes: 18 additions & 9 deletions src/app/repositories/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,32 @@ def __init__(self) -> None:
pass

async def get_player(
self, player_id: int, player_name: str, greater_than: bool, limit: int = 1_000
self,
player_id: int,
player_name: str,
label_id: int,
greater_than: bool,
limit: int = 1_000,
):
table = dbPlayer
sql_select: Select = select(table)
sql_select = sql_select.limit(limit)
sql: Select = select(table)
sql = sql.limit(limit)

if player_name:
sql_select = sql_select.where(dbPlayer.name >= player_name)
sql = sql.where(dbPlayer.name >= player_name)

if greater_than:
sql_select = sql_select.where(dbPlayer.id >= player_id)
elif player_id:
sql_select = sql_select.where(dbPlayer.id == player_id)
if label_id:
sql = sql.where(dbPlayer.label_id >= label_id)

comparison = (
(dbPlayer.id >= player_id) if greater_than else (dbPlayer.id == player_id)
)
sql = sql.where(comparison)
sql = sql.order_by(dbPlayer.id.asc())

async with SessionFactory() as session:
session: AsyncSession

result: AsyncResult = await session.execute(sql_select)
result: AsyncResult = await session.execute(sql)
result = result.scalars().all()
return jsonable_encoder(result)

0 comments on commit 97b5139

Please sign in to comment.