sghorbal commited on
Commit
6fe7076
·
1 Parent(s): 02c056c

expose an endpoint listing players

Browse files
Files changed (1) hide show
  1. src/main.py +33 -2
src/main.py CHANGED
@@ -41,7 +41,11 @@ from src.entity.match import (
41
  MatchApiBase,
42
  MatchApiDetail
43
  )
44
- from src.entity.player import Player, PlayerApiDetail
 
 
 
 
45
  from src.entity.tournament import Tournament
46
  from src.repository.common import get_session
47
  from src.repository.sql import list_tournaments as _list_tournaments
@@ -232,7 +236,34 @@ async def list_tournament_names(
232
 
233
  return tournaments
234
 
235
- # Get a player
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  @app.get("/player/{player_id}", tags=["player"], description="Get a player from the database", response_model=PlayerApiDetail)
237
  async def get_player(
238
  player_id: int,
 
41
  MatchApiBase,
42
  MatchApiDetail
43
  )
44
+ from src.entity.player import (
45
+ Player,
46
+ PlayerApiDetail,
47
+ PlayerApiBase
48
+ )
49
  from src.entity.tournament import Tournament
50
  from src.repository.common import get_session
51
  from src.repository.sql import list_tournaments as _list_tournaments
 
236
 
237
  return tournaments
238
 
239
+
240
+ class ListPlayersInput(BaseModel):
241
+ ids: List[int] = Field(
242
+ description="List of player IDs",
243
+ )
244
+
245
+ # Get a list of players
246
+ @app.get("/players", tags=["player"], description="Get a list of players from the database", response_model=Dict[str|int, Optional[PlayerApiDetail]])
247
+ async def list_players(
248
+ session: Annotated[Session, Depends(get_session)],
249
+ params: Annotated[ListPlayersInput, Query()],
250
+ ):
251
+ """
252
+ Get a list of players from the database
253
+ """
254
+ ids = sorted(params.ids)
255
+ players = session.query(Player).filter(Player.id.in_(ids)).all()
256
+
257
+ if not players:
258
+ raise HTTPException(
259
+ status_code=HTTP_404_NOT_FOUND,
260
+ detail=f"Players {ids} not found"
261
+ )
262
+
263
+ players = {player.id: player for player in players}
264
+
265
+ return {id: players.get(id) for id in ids}
266
+
267
  @app.get("/player/{player_id}", tags=["player"], description="Get a player from the database", response_model=PlayerApiDetail)
268
  async def get_player(
269
  player_id: int,