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 10, 2024
1 parent 7930545 commit 10c4482
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 37 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
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 10c4482

Please sign in to comment.