say NO to try-catch in controllers
- added ExceptionsMiddleware.cs - added more Exception types - removed all exceptions logic in controllers
This commit is contained in:
parent
eb271793ad
commit
55e82425a9
8 changed files with 75 additions and 20 deletions
|
|
@ -1,7 +1,7 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.Core.Services;
|
||||
using SurveyBackend.DTOs;
|
||||
using SurveyBackend.Mappers.UserDTOs;
|
||||
using SurveyBackend.Services.Services;
|
||||
|
||||
namespace SurveyBackend.Controllers;
|
||||
|
||||
|
|
@ -9,9 +9,9 @@ namespace SurveyBackend.Controllers;
|
|||
[Route("auth")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly AuthorizationService _authorizationService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
|
||||
public AuthController(AuthorizationService authorizationService)
|
||||
public AuthController(IAuthorizationService authorizationService)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
}
|
||||
|
|
@ -20,21 +20,13 @@ public class AuthController : ControllerBase
|
|||
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
|
||||
{
|
||||
var token = await _authorizationService.LogInUser(loginData.Email, loginData.Password);
|
||||
return token is null ? Unauthorized() : Ok(new { token = token });
|
||||
return Ok(new { token = token });
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
|
||||
await _authorizationService.RegisterUser(UserRegistrationMapper.UserRegistrationToModel(registerData));
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue