Ejemplo n.º 1
0
        public async Task Should_return_newly_added_exercise(User user)
        {
            var newExercise = new NewExerciseRequest
            {
                UserId      = user.Id,
                Description = "Test description",
                Duration    = TimeSpan.FromMinutes(120).TotalMinutes
            };

            var formContent = new List <KeyValuePair <string, string> >
            {
                KeyValuePair.Create(nameof(newExercise.UserId), newExercise.UserId.ToString()),
                KeyValuePair.Create(nameof(newExercise.Description), newExercise.Description),
                KeyValuePair.Create(nameof(newExercise.Duration), newExercise.Duration.ToString()),
            };

            var httpMessage = new FormUrlEncodedContent(formContent);

            var response = await _client.PostAsync("/api/exercise/add", httpMessage);

            var json = await response.Content.ReadAsStringAsync();

            var serializerOptions = new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            };
            var result = JsonSerializer.Deserialize <NewExerciseResponse>(json, serializerOptions);

            Assert.Equal(user.Id, result._id);
            Assert.Equal(user.UserName, result.Username);
            Assert.Equal(newExercise.Description, result.Description);
            Assert.Equal(newExercise.Duration, result.Duration);
            Assert.NotEqual(default, DateTime.Parse(result.Date));
Ejemplo n.º 2
0
        public void Should_Add_New_Exercise(User user)
        {
            var exerciseRequest = new NewExerciseRequest
            {
                Date        = DateTime.UtcNow,
                UserId      = user.Id,
                Duration    = TimeSpan.FromMinutes(50).TotalMinutes,
                Description = "Go mountain biking with Janet"
            };

            var exerciseResponse = _sut.AddExerciseAsync(exerciseRequest).Result;

            Assert.NotNull(exerciseResponse);
            Assert.Equal(user.Id, exerciseResponse._id);
            Assert.Equal(user.UserName, exerciseResponse.Username);
            Assert.Equal(exerciseRequest.Duration, exerciseRequest.Duration);
            Assert.Equal(exerciseRequest.Date.ToString("D"), exerciseResponse.Date);
            Assert.Equal(exerciseRequest.Description, exerciseResponse.Description);
        }
Ejemplo n.º 3
0
        public async Task <NewExerciseResponse> AddExerciseAsync(NewExerciseRequest request)
        {
            var exercise = exerciseRepository.Add(new Entities.Exercise
            {
                UserId      = request.UserId,
                Date        = request.Date,
                Duration    = TimeSpan.FromMinutes(request.Duration),
                Description = request.Description
            });

            await exerciseRepository.UnitOfWork.SaveChangesAsync();

            return(new NewExerciseResponse
            {
                _id = exercise.UserId,
                Username = (await userRepository.GetUserAsync(exercise.UserId)).UserName,
                Date = exercise.Date.ToUniversalTime().ToString("D"),
                Duration = exercise.Duration.TotalMinutes,
                Description = exercise.Description
            });
        }