Skip to content

Commit

Permalink
feat(PlayerController): Refactor SearchAccount API methods
Browse files Browse the repository at this point in the history
- Simplified the implementation of the SearchAccount method in PlayerController.
- Replaced conditional statement with a ternary operator for better readability.
- Added 'await' keyword to the GetUserAsync method in UserService to ensure asynchronous execution.
- Simplified the implementation of the ListPlayersAsync method in PlayerService.
- Replaced conditional statement with a ternary operator for better readability.
- Updated return type to AccountListingDTO[] instead of IEnumerable<AccountListingDTO>.
  • Loading branch information
SakuraIsayeki committed Jan 14, 2024
1 parent 55ffe69 commit 02c09fe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
12 changes: 4 additions & 8 deletions WowsKarma.Api/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ public PlayerController(PlayerService playerService)
/// <response code="200">Account listings for given search query</response>
/// <response code="204">No results found for given search query</response>
[HttpGet("search/{query}"), ProducesResponseType(typeof(IEnumerable<AccountListingDTO>), 200), ProducesResponseType(204)]
public async Task<IActionResult> SearchAccount([StringLength(100, MinimumLength = 3), RegularExpression(@"^[a-zA-Z0-9_]*$")] string query)
{
IEnumerable<AccountListingDTO> accounts = await _playerService.ListPlayersAsync(query);

return accounts is null
? NoContent()
: Ok(accounts);
}
public async Task<IActionResult> SearchAccount([StringLength(100, MinimumLength = 3), RegularExpression(@"^[a-zA-Z0-9_]*$")] string query)
=> await _playerService.ListPlayersAsync(query) is { Length: not 0 } accounts
? Ok(accounts)
: NoContent();

/// <summary>
/// Fetches the player profile for a given Account ID.
Expand Down
2 changes: 1 addition & 1 deletion WowsKarma.Api/Services/Authentication/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public UserService(AuthDbContext context, IHubContext<AuthHub, IAuthHubPush> hub
/// </summary>
/// <param name="id">The user's ID.</param>
/// <returns>The user, or <see langword="null"/> if not found.</returns>
public Task<User?> GetUserAsync(uint id) => _context.Users.Include(u => u.Roles).FirstOrDefaultAsync(u => u.Id == id);
public async Task<User?> GetUserAsync(uint id) => await _context.Users.Include(u => u.Roles).FirstOrDefaultAsync(u => u.Id == id);

/// <summary>
/// Gets a user's claims
Expand Down
12 changes: 4 additions & 8 deletions WowsKarma.Api/Services/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,10 @@ public IEnumerable<AccountFullKarmaDTO> GetPlayersFullKarma(IEnumerable<uint> ac
.Where(p => accountIds.Contains(p.Id))
.Select(p => new AccountKarmaDTO(p.Id, p.SiteKarma)));

public async Task<IEnumerable<AccountListingDTO>> ListPlayersAsync(string search)
{
AccountListing[] result = (await _wgApi.ListPlayersAsync(search))?.Data?.ToArray();

return result is { Length: > 0 }
? result.Select(listing => listing.ToDto())
: null;
}
public async Task<AccountListingDTO[]> ListPlayersAsync(string search)
=> (await _wgApi.ListPlayersAsync(search))?.Data?.ToArray() is { Length: not 0 } result
? result.Select(Conversions.ToDto).ToArray()
: [];

internal async Task<Player> UpdatePlayerClanStatusAsync(Player player, CancellationToken ct = default)
{
Expand Down

0 comments on commit 02c09fe

Please sign in to comment.