Skip to content

Commit

Permalink
Use primary constructor syntax for all controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
zysim committed Sep 7, 2024
1 parent 1f0d747 commit 5585f18
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 47 deletions.
17 changes: 5 additions & 12 deletions LeaderboardBackend/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@
namespace LeaderboardBackend.Controllers;

[Route("[controller]")]
public class AccountController : ApiController
public class AccountController(IUserService userService) : ApiController
{
private readonly IUserService _userService;

public AccountController(IUserService userService)
{
_userService = userService;
}

[AllowAnonymous]
[FeatureGate(Features.ACCOUNT_REGISTRATION)]
[HttpPost("register")]
Expand Down Expand Up @@ -62,7 +55,7 @@ public async Task<ActionResult<UserViewModel>> Register(
[FromServices] IAccountConfirmationService confirmationService
)
{
CreateUserResult result = await _userService.CreateUser(request);
CreateUserResult result = await userService.CreateUser(request);

if (result.TryPickT0(out User user, out CreateUserConflicts conflicts))
{
Expand Down Expand Up @@ -126,7 +119,7 @@ public async Task<ActionResult<LoginResponse>> Login(
)] LoginRequest request
)
{
LoginResult result = await _userService.LoginByEmailAndPassword(request.Email, request.Password);
LoginResult result = await userService.LoginByEmailAndPassword(request.Email, request.Password);

return result.Match<ActionResult<LoginResponse>>(
loginToken => Ok(new LoginResponse { Token = loginToken }),
Expand All @@ -149,7 +142,7 @@ [FromServices] IAccountConfirmationService confirmationService
{
// TODO: Handle rate limiting (429 case) - zysim

GetUserResult result = await _userService.GetUserFromClaims(HttpContext.User);
GetUserResult result = await userService.GetUserFromClaims(HttpContext.User);

if (result.TryPickT0(out User user, out OneOf<BadCredentials, UserNotFound> errors))
{
Expand Down Expand Up @@ -180,7 +173,7 @@ public async Task<ActionResult> RecoverAccount(
[FromBody, SwaggerRequestBody("The account recovery request.")] RecoverAccountRequest request
)
{
User? user = await _userService.GetUserByNameAndEmail(request.Username, request.Email);
User? user = await userService.GetUserByNameAndEmail(request.Username, request.Email);

if (user is null)
{
Expand Down
13 changes: 3 additions & 10 deletions LeaderboardBackend/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,16 @@

namespace LeaderboardBackend.Controllers;

public class CategoriesController : ApiController
public class CategoriesController(ICategoryService categoryService) : ApiController
{
private readonly ICategoryService _categoryService;

public CategoriesController(ICategoryService categoryService)
{
_categoryService = categoryService;
}

[AllowAnonymous]
[HttpGet("api/category/{id}")]
[SwaggerOperation("Gets a Category by its ID.", OperationId = "getCategory")]
[SwaggerResponse(200)]
[SwaggerResponse(404)]
public async Task<ActionResult<CategoryViewModel>> GetCategory(long id)
{
Category? category = await _categoryService.GetCategory(id);
Category? category = await categoryService.GetCategory(id);

if (category == null)
{
Expand Down Expand Up @@ -54,7 +47,7 @@ [FromBody] CreateCategoryRequest request
LeaderboardId = request.LeaderboardId,
};

await _categoryService.CreateCategory(category);
await categoryService.CreateCategory(category);

return CreatedAtAction(
nameof(GetCategory),
Expand Down
15 changes: 5 additions & 10 deletions LeaderboardBackend/Controllers/LeaderboardsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@

namespace LeaderboardBackend.Controllers;

public class LeaderboardsController : ApiController
public class LeaderboardsController(ILeaderboardService leaderboardService) : ApiController
{
private readonly ILeaderboardService _leaderboardService;

public LeaderboardsController(ILeaderboardService leaderboardService) =>
_leaderboardService = leaderboardService;

[AllowAnonymous]
[HttpGet("api/leaderboard/{id:long}")]
[SwaggerOperation("Gets a leaderboard by its ID.", OperationId = "getLeaderboard")]
[SwaggerResponse(200)]
[SwaggerResponse(404)]
public async Task<ActionResult<LeaderboardViewModel>> GetLeaderboard(long id)
{
Leaderboard? leaderboard = await _leaderboardService.GetLeaderboard(id);
Leaderboard? leaderboard = await leaderboardService.GetLeaderboard(id);

if (leaderboard == null)
{
Expand All @@ -40,7 +35,7 @@ public async Task<ActionResult<LeaderboardViewModel>> GetLeaderboard(long id)
[SwaggerResponse(404)]
public async Task<ActionResult<LeaderboardViewModel>> GetLeaderboardBySlug([FromQuery, SwaggerParameter(Required = true)] string slug)
{
Leaderboard? leaderboard = await _leaderboardService.GetLeaderboardBySlug(slug);
Leaderboard? leaderboard = await leaderboardService.GetLeaderboardBySlug(slug);

if (leaderboard == null)
{
Expand All @@ -58,7 +53,7 @@ public async Task<ActionResult<List<LeaderboardViewModel>>> GetLeaderboards(
[FromQuery] long[] ids
)
{
List<Leaderboard> result = await _leaderboardService.GetLeaderboards(ids);
List<Leaderboard> result = await leaderboardService.GetLeaderboards(ids);
return Ok(result.Select(LeaderboardViewModel.MapFrom));
}

Expand All @@ -75,7 +70,7 @@ [FromBody] CreateLeaderboardRequest request
{
Leaderboard leaderboard = new() { Name = request.Name, Slug = request.Slug, Info = request.Info };

await _leaderboardService.CreateLeaderboard(leaderboard);
await leaderboardService.CreateLeaderboard(leaderboard);

return CreatedAtAction(
nameof(GetLeaderboard),
Expand Down
18 changes: 3 additions & 15 deletions LeaderboardBackend/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
using LeaderboardBackend.Models.Entities;
using LeaderboardBackend.Models.Requests;
using LeaderboardBackend.Models.ViewModels;
using LeaderboardBackend.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using BCryptNet = BCrypt.Net.BCrypt;

namespace LeaderboardBackend.Controllers;

public class UsersController : ApiController
public class UsersController(IUserService userService) : ApiController
{
private readonly IUserService _userService;

public UsersController(IUserService userService)
{
_userService = userService;
}

[AllowAnonymous]
[HttpGet("api/user/{id:guid}")]
[SwaggerOperation("Gets a User by their ID.", OperationId = "getUser")]
Expand All @@ -27,7 +18,7 @@ public async Task<ActionResult<UserViewModel>> GetUserById(
[SwaggerParameter("The ID of the `User` which should be retrieved.")] Guid id
)
{
User? user = await _userService.GetUserById(id);
User? user = await userService.GetUserById(id);

if (user is null)
{
Expand All @@ -51,12 +42,9 @@ Call this method with the 'Authorization' header. A valid JWT bearer token must
[SwaggerResponse(200, "The `User` was found and returned successfully.")]
[SwaggerResponse(401, "An invalid JWT was passed in.")]
[SwaggerResponse(404, "The user was not found in the database.")]
public async Task<ActionResult<UserViewModel>> Me()
{
return (await _userService.GetUserFromClaims(HttpContext.User)).Match<ActionResult<UserViewModel>>(
public async Task<ActionResult<UserViewModel>> Me() => (await userService.GetUserFromClaims(HttpContext.User)).Match<ActionResult<UserViewModel>>(
user => Ok(UserViewModel.MapFrom(user)),
badCredentials => Unauthorized(),
userNotFound => NotFound()
);
}
}

0 comments on commit 5585f18

Please sign in to comment.