public async Task <RunDto> CreateRunAsync(string userId, RunCreateRequest request) { try { var weatherConditions = await _weatherService.GetWeatherConditionsAsync(request.Date, request.Latitude, request.Longitude); var runDb = _mapper.Map <RunCreateRequest, RunDb>(request); runDb.UserId = userId; runDb.WeatherConditions = weatherConditions; await _dbContext.AddAsync(runDb); await _dbContext.SaveChangesAsync(); var response = _mapper.Map <RunDb, RunDto>(runDb); return(response); } catch (Exception ex) { throw new JoggingTrackerInternalServerErrorException($"{ErrorMessages.RunSaveErrorMessage} : {ex.Message}"); } }
public async Task <ActionResult> CreateCurrentUserRun([FromBody] RunCreateRequest request) { var currentUserId = HttpContext.GetCurrentUserId(); var response = await _runService.CreateRunAsync(currentUserId, request); return(StatusCode(StatusCodes.Status201Created, response)); }
public void From_CreateRunRequest_To_RunDb() { var createRunRequest = new RunCreateRequest() { Date = new DateTime(2020, 02, 11, 10, 45, 34), //Tuesday Distance = 5, Time = 6, Latitude = 7.4, Longitude = 6.5, }; var result = _mapper.Map <RunCreateRequest, RunDb>(createRunRequest); result.Date.Should().Be(createRunRequest.Date.Date); result.Distance.Should().Be(createRunRequest.Distance); result.Time.Should().Be(createRunRequest.Time); result.Latitude.Should().Be(createRunRequest.Latitude); result.Longitude.Should().Be(createRunRequest.Longitude); }
public void CreateRunAsync_SavesRunWithWeatherInfo() { // arrange var request = new RunCreateRequest() { Date = DateTime.Now, Distance = 1, Time = 2, Latitude = 3, Longitude = 4 }; var userId = Guid.NewGuid().ToString(); var forecast = "good weather"; _mockWeatherService.Setup(x => x.GetWeatherConditionsAsync(request.Date, request.Latitude, request.Longitude)) .Returns(Task.FromResult(forecast)); // act var result = _runService.CreateRunAsync(userId, request).Result; // assert _mockWeatherService.Verify(x => x.GetWeatherConditionsAsync(request.Date, request.Latitude, request.Longitude), Times.Once); _mockDbContext.Verify(x => x.AddAsync(It.IsAny <RunDb>(), new CancellationToken()), Times.Once); _mockDbContext.Verify(x => x.SaveChangesAsync(new CancellationToken()), Times.Once); result.UserId.Should().Be(userId); result.Date.Should().Be(request.Date.Date); result.Distance.Should().Be(request.Distance); result.Time.Should().Be(request.Time); result.Latitude.Should().Be(request.Latitude); result.Longitude.Should().Be(request.Longitude); result.WeatherConditions.Should().Be(forecast); }
public void CreateRunAsync_DbError_ThrowsCustomException() { // arrange var request = new RunCreateRequest() { Distance = 1, Time = 2, Latitude = 3, Longitude = 4, Date = DateTime.Now }; var userId = Guid.NewGuid().ToString(); var forecast = "good weather"; var exceptionMessage = "Db error"; _mockWeatherService.Setup(x => x.GetWeatherConditionsAsync(request.Date, request.Latitude, request.Longitude)).Returns(Task.FromResult(forecast)); _mockDbContext.Setup(x => x.SaveChangesAsync(new CancellationToken())) .Throws(new Exception(exceptionMessage)); var expectedErrorMessage = $"{ErrorMessages.RunSaveErrorMessage} : {exceptionMessage}"; // act var result = Assert.ThrowsAsync <JoggingTrackerInternalServerErrorException>(() => _runService.CreateRunAsync(userId, request)).Result; // assert _mockWeatherService.Verify(x => x.GetWeatherConditionsAsync(request.Date, request.Latitude, request.Longitude), Times.Once); _mockDbContext.Verify(x => x.AddAsync(It.IsAny <RunDb>(), new CancellationToken()), Times.Once); _mockDbContext.Verify(x => x.SaveChangesAsync(new CancellationToken()), Times.Once); result.Message.Should().Be(expectedErrorMessage); }
public async Task <ActionResult> CreateRun(string userId, [FromBody] RunCreateRequest request) { var response = await _runService.CreateRunAsync(userId, request); return(StatusCode(StatusCodes.Status201Created, response)); }
public void UserGetsRegistered_AddsRuns_UpdatesRun_RequestList_And_DeletesTheirRecord_RunShouldBeDeleted() { // user gets registered var newUserRequest = new UserRegisterRequest() { UserName = "******", Password = "******" }; var httpCreateResponse = _client.PostAsync(_baseUri, ContentHelper.GetStringContent(newUserRequest)).Result; httpCreateResponse.EnsureSuccessStatusCode(); var stringResponse = httpCreateResponse.Content.ReadAsStringAsync().Result; var user = JsonConvert.DeserializeObject <UserDto>(stringResponse); user.Id.Should().NotBeNull(); var newUserDb = _dbContext.Users.FirstOrDefault(x => x.Id == user.Id); newUserDb.Should().NotBeNull(); // user authenticates var token = GetUserToken(newUserRequest.UserName, newUserRequest.Password); token.Should().NotBeNull(); _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var newRun = new RunCreateRequest() { Date = new DateTime(2020, 2, 11), Distance = 500, Time = 500, Latitude = 2, Longitude = 2 }; var httpCreateRunResponse = _client.PostAsync(_baseRunsUri, ContentHelper.GetStringContent(newRun)).Result; httpCreateRunResponse.EnsureSuccessStatusCode(); var createRunStringContent = httpCreateRunResponse.Content.ReadAsStringAsync().Result; var createRunDtoResponse = JsonConvert.DeserializeObject <RunDto>(createRunStringContent); createRunDtoResponse.UserId.Should().Be(newUserDb.Id); createRunDtoResponse.Should().NotBeNull(); createRunDtoResponse.Date.Should().Be(newRun.Date); createRunDtoResponse.Distance.Should().Be(newRun.Distance); createRunDtoResponse.Time.Should().Be(newRun.Time); createRunDtoResponse.Latitude.Should().Be(newRun.Latitude); createRunDtoResponse.Longitude.Should().Be(newRun.Longitude); var dbRun = _dbContext.Runs.FirstOrDefault(x => x.Id == createRunDtoResponse.Id); dbRun.Should().NotBeNull(); // update run var updateRunRequest = new RunUpdateRequest() { Date = new DateTime(2012, 2, 11), Distance = 30, Time = 12, Latitude = 54, Longitude = 45 }; var httpUpdateRunResponse = _client.PutAsync($"{_baseRunsUri}/{dbRun.Id}", ContentHelper.GetStringContent(updateRunRequest)).Result; httpUpdateRunResponse.EnsureSuccessStatusCode(); var updateRunStringContent = httpUpdateRunResponse.Content.ReadAsStringAsync().Result; var updateRunDtoResponse = JsonConvert.DeserializeObject <RunDto>(updateRunStringContent); updateRunDtoResponse.UserId.Should().Be(newUserDb.Id); updateRunDtoResponse.Should().NotBeNull(); updateRunDtoResponse.Date.Should().Be(updateRunRequest.Date); updateRunDtoResponse.Distance.Should().Be(updateRunRequest.Distance); updateRunDtoResponse.Time.Should().Be(updateRunRequest.Time); updateRunDtoResponse.Latitude.Should().Be(updateRunRequest.Latitude); updateRunDtoResponse.Longitude.Should().Be(updateRunRequest.Longitude); // get the list of runs var httpGetRunsListResponse = _client.GetAsync($"{_baseRunsUri}/").Result; httpGetRunsListResponse.EnsureSuccessStatusCode(); var getRunsListStringContent = httpGetRunsListResponse.Content.ReadAsStringAsync().Result; var getRunsListDtoResponse = JsonConvert.DeserializeObject <PagedResult <RunDto> >(getRunsListStringContent); getRunsListDtoResponse.Items.Count.Should().Be(1); // user deletes their own record var httpDeleteResponse = _client.DeleteAsync($"{_baseUri}/me").Result; httpDeleteResponse.StatusCode.Should().Be(StatusCodes.Status204NoContent); // user is not in the database anymore var deletedUser = _dbContext.Users.FirstOrDefault(x => x.Id == user.Id); deletedUser.Should().BeNull(); var deletetRunDb = _dbContext.Runs.FirstOrDefault(x => x.Id == createRunDtoResponse.Id); deletetRunDb.Should().BeNull(); }