63 lines
1.5 KiB
C#
63 lines
1.5 KiB
C#
using Contracts.DTO;
|
|
using Contracts.Services;
|
|
using Contracts.ViewModels;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Services.Support.Exceptions;
|
|
|
|
namespace Controllers.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class AuthController : ControllerBase
|
|
{
|
|
private readonly IAuthService _authService;
|
|
|
|
public AuthController(IAuthService authService)
|
|
{
|
|
_authService = authService;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<UserViewModel>> Login([FromBody] UserLoginDto loginData)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|
|
} |