Skip to content

Commit

Permalink
Chore: Refatoração de fastas relacionadas ao usuário.
Browse files Browse the repository at this point in the history
  • Loading branch information
LibardiFelipe committed Oct 8, 2022
1 parent ba96d5e commit 07a4669
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace TemplateBase.Application.Commands.Login
{
public class AuthenticateCommandHandler : CommandHandler,
IRequestHandler<AuthenticationCommand, Result>
IRequestHandler<UserLoginCommand, Result>
{
private readonly IAuthenticationService _authService;

Expand All @@ -18,7 +18,7 @@ public AuthenticateCommandHandler(IAuthenticationService authService)
_authService = authService;
}

public async Task<Result> Handle(AuthenticationCommand request, CancellationToken cancellationToken)
public async Task<Result> Handle(UserLoginCommand request, CancellationToken cancellationToken)
{
if (request.IsInvalid())
return new Result(DefaultMessages.Handler_ComandoInvalido, false, request.Notifications);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TemplateBase.Application.Commands.Login
{
public class AuthenticationCommand : Command
public class UserLoginCommand : Command
{
public string Email { get; set; }
public string Password { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion TemplateBase.WebAPI/AutoMapper/RequestsToCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public RequestsToCommands()
.IncludeAllDerived()
.AfterMap((_, command) => command.Validate());

CreateMap<AuthenticationRequest, AuthenticationCommand>()
CreateMap<UserLoginRequest, UserLoginCommand>()
.IncludeAllDerived()
.AfterMap((_, command) => command.Validate());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,36 @@

namespace TemplateBase.WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class LoginController : ControllerBase
[AllowAnonymous]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IMapper _mapper;

public LoginController(IMediator mediator, IMapper mapper)
public AuthController(IMediator mediator, IMapper mapper)
{
_mediator = mediator;
_mapper = mapper;
}

[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Authenticate([FromBody] AuthenticationRequest request)
public async Task<IActionResult> LoginAsync([FromBody] UserLoginRequest request)
{
var command = _mapper.Map<UserLoginCommand>(request);
var result = await _mediator.Send(command);
var response = _mapper.Map<ResultViewModel>(result);

return response.Success
? Ok(response)
: BadRequest(response);
}

[HttpPost("register")]
public async Task<IActionResult> RegisterAsync([FromBody] UserLoginRequest request)
{
var command = _mapper.Map<AuthenticationCommand>(request);
var command = _mapper.Map<UserLoginCommand>(request);
var result = await _mediator.Send(command);
var response = _mapper.Map<ResultViewModel>(result);

Expand Down
19 changes: 2 additions & 17 deletions TemplateBase.WebAPI/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using TemplateBase.Application.Commands.Persons;
using TemplateBase.Application.Queries.Users;
using TemplateBase.WebAPI.Models.Requests.Persons;
using TemplateBase.WebAPI.Models.ViewModels;

namespace TemplateBase.WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Route("api/[controller]")]
[Authorize(Roles = "Admin")]
public class UsersController : ControllerBase
{
private readonly IMediator _mediator;
Expand All @@ -24,7 +24,6 @@ public UsersController(IMediator mediator, IMapper mapper)
}

[HttpGet]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> GetAllAsync([FromQuery] FilterUserRequest request)
{
var query = _mapper.Map<UserQuery>(request);
Expand All @@ -37,7 +36,6 @@ public async Task<IActionResult> GetAllAsync([FromQuery] FilterUserRequest reque
}

[HttpGet("{id}")]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> GetByIdAsync([FromRoute] string id)
{
var query = new UserQuery(id);
Expand All @@ -48,18 +46,5 @@ public async Task<IActionResult> GetByIdAsync([FromRoute] string id)
? Ok(response)
: BadRequest(response);
}

[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> RegisterUserAsync([FromBody] RegisterUserRequest request)
{
var command = _mapper.Map<RegisterUserCommand>(request);
var result = await _mediator.Send(command);
var response = _mapper.Map<ResultViewModel>(result);

return response.Success
? Ok(response)
: BadRequest(response);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace TemplateBase.WebAPI.Models.Requests.Login
{
public class AuthenticationRequest
public class UserLoginRequest
{
public string Email { get; set; }
public string Password { get; set; }
Expand Down

0 comments on commit 07a4669

Please sign in to comment.