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