fix: контроллеры
добавлен try catch в контроллер пользователя, рефактор контроллер авторизации
This commit is contained in:
parent
078b469752
commit
43b00bfc2f
@ -4,61 +4,60 @@ using Contracts.ViewModels;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Services.Support.Exceptions;
|
||||
|
||||
namespace Controllers.Controllers
|
||||
namespace Controllers.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController : ControllerBase
|
||||
private readonly IAuthService _authService;
|
||||
|
||||
public AuthController(IAuthService authService)
|
||||
{
|
||||
private readonly IAuthService _authService;
|
||||
_authService = authService;
|
||||
}
|
||||
|
||||
public AuthController(IAuthService authService)
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<UserViewModel>> Login([FromBody] UserLoginDTO loginData)
|
||||
{
|
||||
try
|
||||
{
|
||||
_authService = authService;
|
||||
var user = await _authService.Login(loginData);
|
||||
return Ok(user);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult<UserViewModel>> Login([FromBody] UserLoginDTO loginData)
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await _authService.Login(loginData);
|
||||
return Ok(user);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (UserNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex.Message);
|
||||
}
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<UserViewModel>> Register([FromBody] UserDto user)
|
||||
catch (UserNotFoundException ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
var createdUser = await _authService.Register(user);
|
||||
return CreatedAtAction(nameof(Login), new { name = createdUser.Name }, createdUser);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (AlreadyExistsException ex)
|
||||
{
|
||||
return Conflict(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex.Message);
|
||||
}
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("register")]
|
||||
public async Task<ActionResult<UserViewModel>> Register([FromBody] UserDto user)
|
||||
{
|
||||
try
|
||||
{
|
||||
var createdUser = await _authService.Register(user);
|
||||
return CreatedAtAction(nameof(Login), new { name = createdUser.Name }, createdUser);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (AlreadyExistsException ex)
|
||||
{
|
||||
return Conflict(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
@ -3,42 +3,71 @@ using Contracts.SearchModels;
|
||||
using Contracts.Services;
|
||||
using Contracts.ViewModels;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Services.Support.Exceptions;
|
||||
|
||||
namespace Controllers.Controllers
|
||||
namespace Controllers.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UserController : ControllerBase
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UserController : ControllerBase
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public UserController(IUserService userService)
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public UserController(IUserService userService)
|
||||
{
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<UserViewModel>> GetUser([FromQuery] UserSearch search)
|
||||
_userService = userService;
|
||||
}
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<UserViewModel>> GetUser([FromQuery] UserSearch search)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await _userService.GetDetails(search);
|
||||
|
||||
return Ok(user);
|
||||
}
|
||||
catch (UserNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<ActionResult<UserViewModel>> UpdateUser([FromBody] UserDto user)
|
||||
[HttpPatch]
|
||||
public async Task<ActionResult<UserViewModel>> UpdateUser([FromBody] UserDto user)
|
||||
{
|
||||
try
|
||||
{
|
||||
var updatedUser = await _userService.UpdateUserData(user);
|
||||
|
||||
return Ok(updatedUser);
|
||||
}
|
||||
catch (UserNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
public async Task<ActionResult<UserViewModel>> DeleteUser([FromQuery] UserSearch search)
|
||||
[HttpDelete]
|
||||
public async Task<ActionResult<UserViewModel>> DeleteUser([FromQuery] UserSearch search)
|
||||
{
|
||||
try
|
||||
{
|
||||
var deletedUser = await _userService.Delete(search);
|
||||
|
||||
return Ok(deletedUser);
|
||||
}
|
||||
catch (UserNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user