public async Task <ActionResult> Post([FromBody] CreateUserRequest request) { try { var command = _mapper.Map <CreateUserCommand>(request); await _commandHandler.HandleAsync(command); var query = new GetUserByUsernameQuery { Username = command.Username }; var user = await _queryHandler.HandleAsync(query); return(CreatedAtAction(nameof(Get), new { username = query.Username }, user)); } catch (Exception ex) { // Check if exception is cause of duplicated key if (ex.InnerException.Message.Contains("duplicate key")) { return(BadRequest(new { title = $"Username or email is already in use.", status = HttpStatusCode.BadRequest, })); } return(BadRequest()); } }
public async Task <UserDetail> HandleAsync(GetUserByUsernameQuery query) { var user = await _repository.GetByUsername(query.Username); if (user == null) { return(null); } return(_mapper.Map <UserDetail>(user)); }
public async Task <IActionResult> CheckUsernameAvailability([FromBody] CheckUsernameAvailabilityRequest request) { var query = new GetUserByUsernameQuery(request.Username); var result = await Task.Run(() => this.queryBus.Execute(query)); if (result != null) { return(StatusCode(409)); } return(Ok()); }
public async Task <ActionResult> Get(string username) { var query = new GetUserByUsernameQuery { Username = username }; var user = await _queryHandler.HandleAsync(query); if (user == null) { return(NotFound()); } return(Ok(user)); }
public async Task <Dictionary <string, object> > Handle(GetUserByUsernameQuery request, CancellationToken cancellationToken) { var response = new Dictionary <string, object>(); var users = UserContext.Users; var image = ""; var user = users.Where(u => u.UserName == request.UserName).FirstOrDefault(); if (user == null) { response.Add("succes", false); } else { response.Add("succes", true); image = Convert.ToBase64String(File.ReadAllBytes(user.ImagePath)); } response.Add("image", image); response.Add("user", user); return(response); }
public async Task Should_return_ok_result_when_database_is_connected() { const string emailStem = EmailData.FAKE_EMAIL_STEM; const int userNumber = 1; const Application application = Application.TestApi; var user = new UserBuilder(emailStem, userNumber) .WithUserType(UserType.Judge) .ForApplication(application) .BuildUserDto(); var query = new GetUserByUsernameQuery(user.Username); _controller = new HealthController(_mockQueryHandler.Object, _mockBookingsApiClient.Object, _mockUserApiClient.Object, _mockVideoApiClient.Object); _mockQueryHandler.Setup(x => x.Handle <GetUserByUsernameQuery, UserDto>(query)) .Returns(Task.FromResult(user)); var result = await _controller.Health(); var typedResult = (OkObjectResult)result; typedResult.StatusCode.Should().Be((int)HttpStatusCode.OK); }
public List <User> Handle(GetUserByUsernameQuery query) { return(_userRepository.Get(new UserByUsernameSpecification(query.Username))); }
public async Task <IActionResult> Health() { var response = new HealthResponse { Version = GetApplicationVersion() }; try { const string username = "******"; var query = new GetUserByUsernameQuery(username); await _queryHandler.Handle <GetUserByUsernameQuery, UserDto>(query); response.TestApiHealth.Successful = true; } catch (Exception ex) { response.TestApiHealth.Successful = false; response.TestApiHealth.ErrorMessage = ex.Message; response.TestApiHealth.Data = ex.Data; } try { await _bookingsApiClient.CheckServiceHealthAsync(); response.BookingsApiHealth.Successful = true; } catch (Exception ex) { response.BookingsApiHealth.Successful = false; response.BookingsApiHealth.ErrorMessage = ex.Message; response.BookingsApiHealth.Data = ex.Data; } try { await _userApiClient.CheckServiceHealthAsync(); response.UserApiHealth.Successful = true; } catch (Exception ex) { response.UserApiHealth.Successful = false; response.UserApiHealth.ErrorMessage = ex.Message; response.UserApiHealth.Data = ex.Data; } try { await _videoApiClient.CheckServiceHealthAsync(); response.VideoApiHealth.Successful = true; } catch (Exception ex) { response.VideoApiHealth.Successful = false; response.VideoApiHealth.ErrorMessage = ex.Message; response.VideoApiHealth.Data = ex.Data; } return(response.TestApiHealth.Successful && response.BookingsApiHealth.Successful && response.UserApiHealth.Successful && response.VideoApiHealth.Successful ? Ok(response) : StatusCode((int)HttpStatusCode.InternalServerError, response)); }