Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move user repo logic to service and handle errors #25

Merged
merged 13 commits into from
Mar 20, 2024
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net;
using DotnetFoundation.Application.Exceptions;
using DotnetFoundation.Application.Interfaces.Services;
using DotnetFoundation.Application.Models.Common;
using DotnetFoundation.Application.Models.DTOs.AuthenticationDTO;
Expand Down Expand Up @@ -27,11 +29,25 @@ public AuthenticationController(IAuthenticationService authenticationService)
public async Task<ActionResult<BaseResponse<AuthenticationResponse>>> RegisterAsync(RegisterRequest request)
{
BaseResponse<AuthenticationResponse> response = new(ResponseStatus.Fail);
try
{
response.Data = await _authenticationService.RegisterAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

response.Data = await _authenticationService.RegisterAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
return Ok(response);
}
catch (IdentityUserException ex)
{
response.Message = ex.Message;
Harish-osmosys marked this conversation as resolved.
Show resolved Hide resolved
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}

/// <summary>
Expand All @@ -45,46 +61,101 @@ public async Task<ActionResult<BaseResponse<AuthenticationResponse>>> RegisterAs
public async Task<ActionResult<BaseResponse<AuthenticationResponse>>> LoginAsync(LoginRequest request)
{
BaseResponse<AuthenticationResponse> response = new(ResponseStatus.Fail);
try
{
response.Data = await _authenticationService.LoginAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

response.Data = await _authenticationService.LoginAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;
return Ok(response);
}
catch (InvalidCredentialsException ex)
{
response.Message = ex.Message;

return Ok(response);
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (LockoutException ex)
Harish-osmosys marked this conversation as resolved.
Show resolved Hide resolved
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}

/// <summary>
/// User password reset using reset token.
/// </summary>
/// <param name="request">New password details request</param>
[HttpPost("reset-password")]
[HttpPost("resetpassword")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<BaseResponse<AuthenticationResponse>>> ResetPasswordAsync(PasswordResetRequest request)
public async Task<ActionResult<BaseResponse<int>>> ResetPasswordAsync(PasswordResetRequest request)
{
BaseResponse<AuthenticationResponse> response = new(ResponseStatus.Fail);
BaseResponse<int> response = new(ResponseStatus.Fail);
try
{
await _authenticationService.ResetPasswordAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

response.Data = await _authenticationService.ResetPasswordAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
return Ok(response);
}
catch (NotFoundException ex)
Harish-osmosys marked this conversation as resolved.
Show resolved Hide resolved
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (InvalidTokenException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}

/// <summary>
/// Forgot user password.
/// </summary>
/// <param name="email">Email of user to reset password</param>
[HttpPost("forgot-password")]
[HttpPost("forgotpassword")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<BaseResponse<string>>> ForgotPasswordAsync(string email)
public async Task<ActionResult<BaseResponse<int>>> ForgotPasswordAsync(string email)
{
BaseResponse<string> response = new(ResponseStatus.Fail);

response.Data = await _authenticationService.ForgotPasswordAsync(email).ConfigureAwait(false);
response.Status = ResponseStatus.Success;
BaseResponse<int> response = new(ResponseStatus.Fail);
try
{
await _authenticationService.ForgotPasswordAsync(email).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
return Ok(response);
}
catch (NotFoundException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
}
115 changes: 94 additions & 21 deletions DotnetFoundation/DotnetFoundation.Api/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net;
using DotnetFoundation.Application.Exceptions;
using DotnetFoundation.Application.Interfaces.Services;
using DotnetFoundation.Application.Models.Common;
using DotnetFoundation.Application.Models.DTOs.UserDTO;
Expand Down Expand Up @@ -28,11 +30,19 @@ public UserController(IUserService userService)
public async Task<ActionResult<BaseResponse<List<UserResponse>>>> GetAllUsersAsync()
{
BaseResponse<List<UserResponse>> response = new(ResponseStatus.Fail);
try
{
response.Data = await _userService.GetAllUsersAsync().ConfigureAwait(false);
response.Status = ResponseStatus.Success;

response.Data = await _userService.GetAllUsersAsync().ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
return Ok(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
}

/// <summary>
Expand All @@ -47,31 +57,59 @@ public async Task<ActionResult<BaseResponse<List<UserResponse>>>> GetAllUsersAsy
public async Task<ActionResult<BaseResponse<UserResponse>>> GetUserByIdAsync(int userId)
{
BaseResponse<UserResponse> response = new(ResponseStatus.Fail);
try
{
response.Data = await _userService.GetUserByIdAsync(userId).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

response.Data = await _userService.GetUserByIdAsync(userId).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
return Ok(response);
}
catch (NotFoundException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}

/// <summary>
/// Add new user role.
/// Authorize - ADMIN role
/// </summary>
/// <param name="roleRequest">Role request details</param>
[HttpPost("add-role")]
[HttpPost("addrole")]
[Authorize(Roles = "ADMIN")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<BaseResponse<bool>>> AddUserRoleAsync(UserRoleRequest roleRequest)
{
BaseResponse<bool> response = new(ResponseStatus.Fail);
try
{
response.Data = await _userService.AddUserRoleAsync(roleRequest.Email, roleRequest.Role).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

response.Data = await _userService.AddUserRoleAsync(roleRequest.Email, roleRequest.Role).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
return Ok(response);
}
catch (NotFoundException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}

/// <summary>
Expand All @@ -84,17 +122,38 @@ public async Task<ActionResult<BaseResponse<bool>>> AddUserRoleAsync(UserRoleReq
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<BaseResponse<UserResponse>>> UpdateUserAsync(int userId, [FromBody] UpdateUserRequest updateUserRequest)
public async Task<ActionResult<BaseResponse<UserResponse>>> UpdateUserAsync(int userId, UpdateUserRequest updateUserRequest)
{
BaseResponse<UserResponse> response = new(ResponseStatus.Fail);
if (updateUserRequest == null)
{
return BadRequest("Invalid request data");
}
response.Data = await _userService.UpdateUserAsync(userId, updateUserRequest).ConfigureAwait(false);
response.Status = ResponseStatus.Success;
try
{
response.Data = await _userService.UpdateUserAsync(userId, updateUserRequest).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
return Ok(response);
}
catch (NotFoundException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (UserException ex)
Harish-osmosys marked this conversation as resolved.
Show resolved Hide resolved
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}

/// <summary>
Expand All @@ -109,10 +168,24 @@ public async Task<ActionResult<BaseResponse<UserResponse>>> UpdateUserAsync(int
public async Task<ActionResult<BaseResponse<UserResponse>>> DeleteUserAsync(int userId)
{
BaseResponse<UserResponse> response = new(ResponseStatus.Fail);
try
{
response.Data = await _userService.DeleteUserAsync(userId).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

response.Data = await _userService.DeleteUserAsync(userId).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

return Ok(response);
return Ok(response);
}
catch (NotFoundException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
}
2 changes: 1 addition & 1 deletion DotnetFoundation/DotnetFoundation.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"Emails": {
"ForgetPassword": {
"Path": "../DotnetFoundation.Api/wwwroot/Templates/Emails/ForgetPasswordTemplate.html",
"Path": "../DotnetFoundation.Api/wwwroot/Templates/Emails/ResetPasswordTemplate.html",
"Subject": "Forget password"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace DotnetFoundation.Application.Exceptions;


// Define a custom exception using this interface
public class IdentityUserException : Exception
{
public IdentityUserException()
{
}

public IdentityUserException(string message) : base(message)
{
}

public IdentityUserException(string message, Exception innerException) : base(message, innerException)
{
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotnetFoundation.Application.Exceptions;

public class InvalidCredentialsException : Exception
{
public InvalidCredentialsException()
{
}

public InvalidCredentialsException(string message) : base(message)
{
}

public InvalidCredentialsException(string message, Exception innerException) : base(message, innerException)
{
}
}
Loading
Loading