public async Task <ActionResult> CreateAccount([FromBody] CreateAccountBody credentials, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } UserNameOrEmailExistsQuery existsQuery = _mapper.Map <CreateAccountBody, UserNameOrEmailExistsQuery>(credentials); bool exists = await _mediator.Send(existsQuery, cancellationToken); if (exists) { return(StatusCode(StatusCodes.Status403Forbidden, new ErrorResource { StatusCode = StatusCodes.Status403Forbidden, Message = "A user with the same user name or email already exists. Please use different credentials for creating an account" })); } CreateAccountCommand registerCommand = _mapper.Map <CreateAccountBody, CreateAccountCommand>(credentials); int userId = await _mediator.Send(registerCommand, cancellationToken); return(CreatedAtAction(nameof(GetUserProfile), new { userId }, null)); }
public async Task <ActionResult> UserExists([FromQuery] UserExistsQueryParams queryParams, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Check whether user name or e-mail exists UserNameOrEmailExistsQuery existsQuery = _mapper.Map <UserExistsQueryParams, UserNameOrEmailExistsQuery>(queryParams); bool exists = await _mediator.Send(existsQuery, cancellationToken); if (exists) { return(Ok()); } return(NotFound()); }
public async Task GetUserNameOrEmailExistsQueryHandler_ShouldReturnFalse_WhenCredentialsDontNotExist() { UserNameOrEmailExistsQuery request = new UserNameOrEmailExistsQuery { Email = "*****@*****.**", UserName = "******" }; // Arrange _unitOfWorkMock .Setup(m => m.Users.UserNameOrEmailExists(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync(false); UserNameOrEmailExistsQuery.Handler handler = new UserNameOrEmailExistsQuery.Handler(_unitOfWorkMock.Object); // Act bool exists = await handler.Handle(request); // Assert Assert.False(exists); }