Created
May 25, 2020 09:07
-
-
Save ginomessmer/568ce6b4c49b072f9322fe258fc7b035 to your computer and use it in GitHub Desktop.
Unidash MediatR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Unidash.Auth.Users.Requests | |
{ | |
public class GetUserRequest : IRequest<IActionResult> | |
{ | |
public string Id { get; set; } | |
public GetUserRequest(string id) | |
{ | |
Id = id; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Unidash.Auth.Users.Requests | |
{ | |
public class GetUserRequestHandler : IRequestHandler<GetUserRequest, IActionResult> | |
{ | |
private readonly IMediator _mediator; | |
private readonly IMapper _mapper; | |
public GetUserRequestHandler(IMediator mediator, IMapper mapper) | |
{ | |
_mediator = mediator; | |
_mapper = mapper; | |
} | |
public async Task<IActionResult> Handle(GetUserRequest request, CancellationToken cancellationToken) | |
{ | |
var user = await _mediator.Send(new FindUserQuery(request.Id), cancellationToken); | |
if (user == null) | |
return new NotFoundResult(); | |
var dto = _mapper.Map<UserDto>(user); | |
return new OkObjectResult(dto); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("users")] | |
[ApiController] | |
public class UsersController : ControllerBase | |
{ | |
private readonly IMediator _mediator; | |
public UsersController(IMediator mediator) | |
{ | |
_mediator = mediator; | |
} | |
[HttpGet("{id}")] | |
public async Task<IActionResult> GetById(string id) => await _mediator.Send(new GetUserRequest(id)); | |
[HttpGet] | |
[Authorize] | |
public async Task<IActionResult> Get() => | |
await _mediator.Send(new GetUserRequest(User.Identity.Name)); | |
[HttpDelete] | |
[Authorize] | |
public async Task<IActionResult> DeleteUser([FromBody] DeleteUserRequest request) => | |
await _mediator.Send(request); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment