Support query players when only account_id

This commit is contained in:
xtaodada 2024-07-30 16:18:42 +08:00
parent fdaeaa19e9
commit fb76a75ee1
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 18 additions and 2 deletions

View File

@ -62,8 +62,15 @@ class PlayersRepository(BaseService.Component):
async with AsyncSession(self.engine) as session:
statement = select(Player).where(Player.user_id == user_id)
results = await session.exec(statement)
players = results.all()
return players
return results.all()
async def get_all_by_account_id(self, account_id: int, region: Optional[RegionEnum] = None) -> List[Player]:
async with AsyncSession(self.engine) as session:
statement = select(Player).where(Player.account_id == account_id)
if region is not None:
statement = statement.where(Player.region == region)
results = await session.exec(statement)
return results.all()
class PlayerInfoRepository(BaseService.Component):

View File

@ -45,5 +45,14 @@ class PlayersService(BaseService):
for player in players:
await self._repository.delete(player)
async def get_all_by_account_id(self, account_id: int, region: Optional[RegionEnum] = None) -> List[Player]:
return await self._repository.get_all_by_account_id(account_id, region)
async def set_player_to_main(self, user_id: int, player_id: int) -> bool:
players = await self._repository.get_all_by_user_id(user_id)
for player in players:
player.is_chosen = player.player_id == player_id
await self._repository.update(player)
async def delete(self, player: Player):
await self._repository.delete(player)