Skip to content

Commit

Permalink
feat(controllers): Update controller action return types
Browse files Browse the repository at this point in the history
- Updated the return type of the `ValidateAuth` action in the `AuthController` to `ActionResult`.
- Updated the return type of the `WgAuthCallbackAsync` action in the `AuthController` to `Task<IActionResult>`.
- Updated the return type of the `RenewSeed` action in the `AuthController` to `Task`.
- Updated the return type of the `RefreshToken` action in the `AuthController` to `Task<string>`.
- Updated the return type of the `ListPlayers` action in the `PlayerController` to `IAsyncEnumerable<uint>`.
- Updated the return type of the `SearchAccount` action in the 	`PlayerController` to 	`Task<ActionResult<AccountListingDTO>>`.
  • Loading branch information
SakuraIsayeki committed Aug 25, 2024
1 parent 226a5c5 commit 0ccb8a7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 102 deletions.
15 changes: 7 additions & 8 deletions WowsKarma.Api/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public AuthController(IConfiguration config, UserService userService, WargamingA
/// </summary>
/// <response code="200">Authentication successful.</response>
/// <response code="401">Authentication failed.</response>
[HttpHead, Authorize, ProducesResponseType(200), ProducesResponseType(401)]
public IActionResult ValidateAuth() => StatusCode(200);
[HttpHead, Authorize]
public ActionResult ValidateAuth() => Ok();

/// <summary>
/// Provides redirection to Wargaming OpenID Authentication.
Expand All @@ -52,13 +52,13 @@ public AuthController(IConfiguration config, UserService userService, WargamingA
/// <response code="200">Authentication successful.</response>
/// <response code="403">Invalid callback request.</response>
[HttpGet("wg-callback"), ProducesResponseType(302), ProducesResponseType(200), ProducesResponseType(403)]
public async Task<IActionResult> WgAuthCallback()
public async Task<IActionResult> WgAuthCallbackAsync()
{
bool valid = await _wargamingAuthService.VerifyIdentity(Request);

if (!valid)
{
return StatusCode(403);
return Forbid();
}

JwtSecurityToken token = await _userService.CreateTokenAsync(WargamingIdentity.FromUri(new(Request.Query["openid.identity"].FirstOrDefault()
Expand Down Expand Up @@ -89,10 +89,9 @@ public async Task<IActionResult> WgAuthCallback()
/// <response code="200">Seed Token successfully reset.</response>
/// <response code="401">Authentication failed.</response>
[HttpPost("renew-seed"), Authorize, ProducesResponseType(200), ProducesResponseType(401)]
public async Task<IActionResult> RenewSeed()
public async Task RenewSeed()
{
await _userService.RenewSeedTokenAsync(uint.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? throw new BadHttpRequestException("Missing NameIdentifier claim.")));
return Ok();
}

/// <summary>
Expand All @@ -101,9 +100,9 @@ public async Task<IActionResult> RenewSeed()
/// <response code="200">Token successfully refreshed.</response>
/// <response code="401">Authentication failed.</response>
[HttpGet("refresh-token"), Authorize, ProducesResponseType(typeof(string), 200), ProducesResponseType(401)]
public async Task<IActionResult> RefreshToken()
public async Task<string> RefreshToken()
{
JwtSecurityToken token = await _userService.CreateTokenAsync(new(User.Claims));
return StatusCode(200, _jwtService.TokenHandler.WriteToken(token));
return _jwtService.TokenHandler.WriteToken(token);
}
}
24 changes: 14 additions & 10 deletions WowsKarma.Api/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
Expand All @@ -23,7 +24,7 @@ public PlayerController(PlayerService playerService)
/// </summary>
/// <returns>A list of all players in the database.</returns>
/// <response code="200">Returns all players in the database.</response>
[HttpGet, ProducesResponseType(typeof(IEnumerable<uint>), 200)]
[HttpGet]
public IAsyncEnumerable<uint> ListPlayers() => _playerService.ListPlayerIds();

/// <summary>
Expand All @@ -34,7 +35,7 @@ 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)
public async Task<ActionResult<AccountListingDTO>> SearchAccount([StringLength(100, MinimumLength = 3), RegularExpression(@"^[a-zA-Z0-9_]*$")] string query)
=> await _playerService.ListPlayersAsync(query) is { Length: not 0 } accounts
? Ok(accounts)
: NoContent();
Expand All @@ -46,12 +47,13 @@ public async Task<IActionResult> SearchAccount([StringLength(100, MinimumLength
/// <param name="includeClanInfo">Include clan membership info while fetching player profile.</param>
/// <response code="200">Returns player profile</response>
/// <response code="204">No profile found</response>
[HttpGet("{id}"), ProducesResponseType(typeof(PlayerProfileDTO), 200), ProducesResponseType(204)]
public async Task<IActionResult> GetAccount(uint id, bool includeClanInfo = true)
[HttpGet("{id}")]
public async Task<ActionResult<PlayerProfileDTO>> GetAccount(uint id, bool includeClanInfo = true)
{
if (id is 0)
{
return BadRequest(new ArgumentException(null, nameof(id)));
ModelState.AddModelError(nameof(id), "Account ID cannot be zero.");
return BadRequest(ModelState);
}

Player? playerProfile = await _playerService.GetPlayerAsync(id, false, includeClanInfo);
Expand All @@ -67,15 +69,15 @@ public async Task<IActionResult> GetAccount(uint id, bool includeClanInfo = true
/// <param name="ids">List of Account IDs</param>
/// <response code="200">Returns "Account":"SiteKarma" Dictionary of Karma metrics for available accounts (may be empty).</response>
[HttpPost("karmas"), ProducesResponseType(typeof(Dictionary<uint, int>), 200)]
public IActionResult FetchKarmas([FromBody] uint[] ids) => Ok(AccountKarmaDTO.ToDictionary(_playerService.GetPlayersKarma(ids)));
public Dictionary<uint, int> FetchKarmas([FromBody] uint[] ids) => AccountKarmaDTO.ToDictionary(_playerService.GetPlayersKarma(ids));

/// <summary>
/// Fetches full Karma metrics (Site Karma and Flairs) for each provided Account ID, where available.
/// </summary>
/// <param name="ids">List of Account IDs</param>
/// <response code="200">Returns Full Karma metrics for available accounts (may be empty).</response>
[HttpPost("karmas-full"), ProducesResponseType(typeof(IEnumerable<AccountFullKarmaDTO>), 200)]
public IActionResult FetchFullKarmas([FromBody] uint[] ids) => Ok(_playerService.GetPlayersFullKarma(ids));
[HttpPost("karmas-full")]
public IEnumerable<AccountFullKarmaDTO> FetchFullKarmas([FromBody] uint[] ids) => _playerService.GetPlayersFullKarma(ids);

/// <summary>
/// Triggers recalculation of Karma metrics for a given account.
Expand All @@ -86,8 +88,10 @@ public async Task<IActionResult> GetAccount(uint id, bool includeClanInfo = true
/// <param name="playerId">Account ID of player profile</param>
/// <param name="ct"></param>
/// <response code="205">Profile Karma recalculation was processed.</response>
[HttpPatch("recalculate"), Authorize(Roles = ApiRoles.Administrator), ProducesResponseType(205), ProducesResponseType(401), ProducesResponseType(403)]
public IActionResult RecalculateMetrics([FromQuery] uint playerId, CancellationToken ct)
/// <response code="401">Unauthorized</response>
/// <response code="403">Forbidden</response>
[HttpPatch("recalculate"), Authorize(Roles = ApiRoles.Administrator)]
public AcceptedResult RecalculateMetrics([FromQuery] uint playerId, CancellationToken ct)
{
BackgroundJob.Enqueue<PlayerService>(p => p.RecalculatePlayerMetrics(playerId, ct));
return Accepted();
Expand Down
Loading

0 comments on commit 0ccb8a7

Please sign in to comment.