Skip to content

Commit

Permalink
fix(PlayerService): Handle nullable player objects + otherfixes
Browse files Browse the repository at this point in the history
- Added a nullability check to the `playerProfile` variable in the `PlayerController` class.
- Previously, the `playerProfile` variable was of type `Player`, but it is now of type `Player?`.
- Modified the `GetPlayerAsync` and `UpdatePlayerRecordAsync` methods in the `PlayerService` class to handle nullable player objects.
- The return types of these methods have been changed from `Task<Player>` to `Task<Player?>`.
- If a player object is null, it will be handled appropriately and a null value will be returned.
  • Loading branch information
SakuraIsayeki committed Jan 25, 2024
1 parent 32c4aac commit b92e292
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion WowsKarma.Api/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<IActionResult> GetAccount(uint id, bool includeClanInfo = true
return BadRequest(new ArgumentException(null, nameof(id)));
}

Player playerProfile = await _playerService.GetPlayerAsync(id, false, includeClanInfo);
Player? playerProfile = await _playerService.GetPlayerAsync(id, false, includeClanInfo);

return playerProfile is null
? NotFound()
Expand Down
28 changes: 19 additions & 9 deletions WowsKarma.Api/Services/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public async Task<IEnumerable<Player>> GetPlayersAsync(IEnumerable<uint> ids, bo

foreach (uint id in ids.AsParallel().WithCancellation(ct))
{
players.Add(await GetPlayerAsync(id, includeRelated, includeClanInfo, ct));
if (await GetPlayerAsync(id, includeRelated, includeClanInfo, ct) is { } player)
{
players.Add(player);
}
}

return players;
Expand All @@ -69,7 +72,7 @@ public async Task<IEnumerable<Player>> GetPlayersAsync(IEnumerable<uint> ids, bo
* Method no longer returns any tracked entity, resulting in dropped changes for EF Core
* Do not use unless readonly.
*/
public async Task<Player> GetPlayerAsync(uint accountId, bool includeRelated = false, bool includeClanInfo = false, CancellationToken ct = default)
public async Task<Player?> GetPlayerAsync(uint accountId, bool includeRelated = false, bool includeClanInfo = false, CancellationToken ct = default)
{
if (accountId is 0)
{
Expand All @@ -91,13 +94,19 @@ public async Task<Player> GetPlayerAsync(uint accountId, bool includeRelated = f
}


Player player = await dbPlayers.FirstOrDefaultAsync(p => p.Id == accountId, ct);
Player? player = await dbPlayers.FirstOrDefaultAsync(p => p.Id == accountId, ct);
bool updated = false;
bool insert = player is null;

if (insert || UpdateNeeded(player))
{
player = await UpdatePlayerRecordAsync(player, accountId);

if (player is null)
{
return null;
}

updated = true;

if (insert)
Expand Down Expand Up @@ -222,14 +231,15 @@ internal async Task<Player> UpdatePlayerClanStatusAsync(Player player, Cancellat
return player;
}

internal async Task<Player> UpdatePlayerRecordAsync(Player player, uint? accountId = null)
internal async Task<Player?> UpdatePlayerRecordAsync(Player? player, uint? accountId = null)
{
Player apiPlayer = (await _vortex.FetchAccountAsync(player?.Id ?? accountId ?? 0)).ToDbModel() ?? throw new ApplicationException("Account returned null.");
if ((await _vortex.FetchAccountAsync(player?.Id ?? accountId ?? 0))?.ToDbModel() is { } apiPlayer)
{
player = player is null
? _context.Players.Add(apiPlayer).Entity
: Player.MapFromApi(player, apiPlayer);
}

player = player is null
? _context.Players.Add(apiPlayer).Entity
: Player.MapFromApi(player, apiPlayer);

return player;
}

Expand Down

0 comments on commit b92e292

Please sign in to comment.