Skip to content

Commit

Permalink
Update Conventions (#100)
Browse files Browse the repository at this point in the history
* Add GetAnon Convention

* Add Doc Comments

* Remove Unused Route Param. Comments

Co-authored-by: Sim <[email protected]>
  • Loading branch information
zysim and Sim authored Jun 19, 2022
1 parent 1576aec commit d36081b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
16 changes: 15 additions & 1 deletion LeaderboardBackend/Controllers/Annotations/Conventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public static class Conventions
{
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static void Get(
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)]
Expand All @@ -15,16 +17,28 @@ public static void Get(
params object[] parameters)
{ }

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static void GetAnon(
[ApiConventionNameMatch(ApiConventionNameMatchBehavior.Suffix)]
[ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
object id,
params object[] parameters)
{ }

[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static void Post(params object[] parameters)
{ }

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static void Update(params object[] parameters)
{ }

Expand Down
24 changes: 8 additions & 16 deletions LeaderboardBackend/Controllers/BansController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ ILeaderboardService leaderboardService
/// <summary>Get bans by leaderboard ID</summary>
/// <param name="leaderboardId">The leaderboard ID.</param>
/// <response code="200">A list of bans. Can be an empty array.</response>
/// <response code="404">No bans found for the Leaderboard.
/// </response>
/// <response code="404">No bans found for the Leaderboard.</response>
[AllowAnonymous]
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Get))]
nameof(Conventions.GetAnon))]
[HttpGet("leaderboard/{leaderboardId:long}")]
public async Task<ActionResult<List<Ban>>> GetBansByLeaderboard(long leaderboardId)
{
Expand All @@ -55,11 +54,10 @@ public async Task<ActionResult<List<Ban>>> GetBansByLeaderboard(long leaderboard
/// <summary>Get bans by user ID.</summary>
/// <param name="bannedUserId">The user ID.</param>
/// <response code="200">A list of bans. Can be an empty array.</response>
/// <response code="404">No bans found for the User.
/// </response>
/// <response code="404">No bans found for the User.</response>
[AllowAnonymous]
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Get))]
nameof(Conventions.GetAnon))]
[HttpGet("leaderboard/{bannedUserId:Guid}")]
public async Task<ActionResult<List<Ban>>> GetBansByUser(Guid bannedUserId)
{
Expand Down Expand Up @@ -99,11 +97,8 @@ public async Task<ActionResult<Ban>> GetBan(long id)
/// <response code="401">If a non-admin calls this.</response>
/// <response code="403">If the banned user is also an admin.</response>
/// <response code="404">If the banned user is not found.</response>
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Post))]
[Authorize(Policy = UserTypes.Admin)]
[HttpPost]
public async Task<ActionResult<Ban>> CreateSiteBan([FromBody] CreateSiteBanRequest body)
Expand Down Expand Up @@ -145,11 +140,8 @@ public async Task<ActionResult<Ban>> CreateSiteBan([FromBody] CreateSiteBanReque
/// <response code="401">If a non-admin or mod calls this.</response>
/// <response code="403">If the banned user is an admin or a mod.</response>
/// <response code="404">If the banned user is not found.</response>
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Post))]
[Authorize(Policy = UserTypes.Mod)]
[HttpPost("leaderboard")]
public async Task<ActionResult<Ban>> CreateLeaderboardBan([FromBody] CreateLeaderboardBanRequest body)
Expand Down
2 changes: 1 addition & 1 deletion LeaderboardBackend/Controllers/JudgementsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ IAuthService authService
/// <response code="200">The Judgement with the provided ID.</response>
/// <response code="404">If no Judgement can be found.</response>
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Get))]
nameof(Conventions.GetAnon))]
[AllowAnonymous]
[HttpGet("{id}")]
public async Task<ActionResult<JudgementViewModel>> GetJudgement(long id)
Expand Down
2 changes: 1 addition & 1 deletion LeaderboardBackend/Controllers/LeaderboardsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public LeaderboardsController(ILeaderboardService leaderboardService)
/// <response code="200">The Leaderboard.</response>
/// <response code="404">If no Leaderboard can be found.</response>
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Get))]
nameof(Conventions.GetAnon))]
[AllowAnonymous]
[HttpGet("{id}")]
public async Task<ActionResult<Leaderboard>> GetLeaderboard(long id)
Expand Down
21 changes: 19 additions & 2 deletions LeaderboardBackend/Controllers/ParticipationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ IAuthService authService
AuthService = authService;
}

/// <summary>Gets a participation for a run.</summary>
/// <param name="id">The participation ID.</param>
/// <response code="200">The participation object.</response>
/// <response code="404">No participations found.</response>
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Get))]
nameof(Conventions.GetAnon))]
[AllowAnonymous]
[HttpGet("{id}")]
public async Task<ActionResult<Participation>> GetParticipation(long id)
Expand All @@ -44,9 +48,12 @@ public async Task<ActionResult<Participation>> GetParticipation(long id)
return Ok(participation);
}

/// <summary>Creates a participation of a user for a run.</summary>
/// <param name="request">The request body.</param>
/// <response code="201">The newly-created participation object.</response>
/// <response code="404">Either the runner or run could not be found.</response>
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Post))]
[Authorize]
[HttpPost]
public async Task<ActionResult> CreateParticipation([FromBody] CreateParticipationRequest request)
{
Expand All @@ -59,6 +66,8 @@ public async Task<ActionResult> CreateParticipation([FromBody] CreateParticipati
User? runner = await UserService.GetUserById(request.RunnerId);
Run? run = await RunService.GetRun(request.RunId);

// FIXME: runner null check should probably 500 if it equals the caller's ID. In fact, we might
// want to review this method. It's pretty weird. -zysim
if (runner is null || run is null)
{
return NotFound();
Expand All @@ -78,12 +87,20 @@ public async Task<ActionResult> CreateParticipation([FromBody] CreateParticipati
return CreatedAtAction(nameof(GetParticipation), new { id = participation.Id }, participation);
}

/// <summary>Updates the participation of a user for a run.</summary>
/// <remarks>Expects both a comment and a VoD link.</remarks>
/// <param name="request">The request body.</param>
/// <response code="200">A successful update.</response>
/// <response code="404">The participation could not be found.</response>
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Update))]
[Authorize]
[HttpPut]
public async Task<ActionResult> UpdateParticipation([FromBody] UpdateParticipationRequest request)
{
// FIXME: We should review this method. The way it handles things is pretty weird. For example we
// may want to allow updating other users' participations, on top of the fact that users may have
// multiple participations, which we should also handle. -zysim
Guid? userId = AuthService.GetUserIdFromClaims(HttpContext.User);
if (userId is null)
{
Expand Down
4 changes: 2 additions & 2 deletions LeaderboardBackend/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public UsersController(IUserService userService, IAuthService authService)
/// <response code="200">The User with the provided ID.</response>
/// <response code="404">If no User is found with the provided ID.</response>
[ApiConventionMethod(typeof(Conventions),
nameof(Conventions.Get))]
nameof(Conventions.GetAnon))]
[AllowAnonymous]
[HttpGet("{id:guid}")]
public async Task<ActionResult<User>> GetUserById(Guid id)
Expand All @@ -37,7 +37,7 @@ public async Task<ActionResult<User>> GetUserById(Guid id)
return NotFound();
}

// FIXME: Make user view model
// FIXME: Make user view model
user.Email = "";
return Ok(user);
}
Expand Down

0 comments on commit d36081b

Please sign in to comment.