public void Validate_DtoNameAlreadyExists_ReturnsCorrectErrorMessage()
        {
            // Arrange
            var name = "Squat";
            var user = new User {
                Id = NextId
            };

            Context.Users.Add(user);

            var exercise = new Exercise {
                UserId = user.Id, Name = name
            };

            Context.Exercises.Add(exercise);
            Context.SaveChanges();

            var request = new ExerciseCreateDto {
                UserId = user.Id, Name = name
            };

            // Act
            var validator = new ExerciseCreateDtoValidator(Context);
            var result    = validator.Validate(request);

            // Assert
            var hasCorrectErrorMessage = result.Errors.Any(x =>
                                                           x.ErrorMessage == $"You already have an exercise of '{request.Name}'" &&
                                                           x.PropertyName == nameof(ExerciseCreateDto.Name));

            Assert.IsTrue(hasCorrectErrorMessage);
        }
        public void CreateExercise_UserIdHasValue_ReturnsUserIdThatWasGiven()
        {
            // Arrange
            var UserId  = NextId;
            var request = new ExerciseCreateDto {
                UserId = UserId
            };

            // Act
            var repo   = new ExerciseRepository(Context);
            var result = repo.CreateExercise(request);

            // Assert
            Assert.AreEqual(UserId, result.UserId);
        }
        public void CreateExercise_NameHasValue_ReturnsNameThatWasGiven()
        {
            // Arrange
            var name    = $"{NextId}";
            var request = new ExerciseCreateDto {
                Name = name
            };

            // Act
            var repo   = new ExerciseRepository(Context);
            var result = repo.CreateExercise(request);

            // Assert
            Assert.AreEqual(name, result.Name);
        }
Beispiel #4
0
        public IActionResult Post([FromBody] ExerciseCreateDto exerciseCreateDto)
        {
            var userIdString = User.FindFirst(ClaimTypes.Name)?.Value;
            var userId = int.Parse(userIdString);

            exerciseCreateDto.UserId = userId;

            var validationResult = _exerciseCreateDtoValidator.Validate(exerciseCreateDto);

            if (!validationResult.IsValid)
                return BadRequest(ValidationHelpers.ConvertValidationResult(validationResult));

            var exercise = _exerciseRepository.CreateExercise(exerciseCreateDto);
            return Created($"api/exercise/{exercise.Id}", exercise);
        }
        public void CreateExercise_MuscleGroupHasValue_ContextHasNewExerciseWithCorrectMuscleGroup()
        {
            // Arrange
            var muscleGroup = $"{NextId}";
            var request     = new ExerciseCreateDto {
                MuscleGroup = muscleGroup
            };

            // Act
            var repo   = new ExerciseRepository(Context);
            var result = repo.CreateExercise(request);
            var exerciseFromContext = Context.Exercises.Find(result.Id);

            // Assert
            Assert.AreEqual(muscleGroup, exerciseFromContext.MuscleGroup);
        }
        public void CreateExercise_NameHasValue_ContextHasNewExerciseWithCorrectName()
        {
            // Arrange
            var name    = $"{NextId}";
            var request = new ExerciseCreateDto {
                Name = name
            };

            // Act
            var repo   = new ExerciseRepository(Context);
            var result = repo.CreateExercise(request);
            var exerciseFromContext = Context.Exercises.Find(result.Id);

            // Assert
            Assert.AreEqual(name, exerciseFromContext.Name);
        }
        public void CreateExercise_UserIdHasValue_ContextHasNewExerciseWithCorrectUserId()
        {
            // Arrange
            var UserId  = NextId;
            var request = new ExerciseCreateDto {
                UserId = UserId
            };

            // Act
            var repo   = new ExerciseRepository(Context);
            var result = repo.CreateExercise(request);
            var exerciseFromContext = Context.Exercises.Find(result.Id);

            // Assert
            Assert.AreEqual(UserId, exerciseFromContext.UserId);
        }
        public void Validate_DtoMuscleGroupIsEmpty_ReturnsCorrectErrorMessage()
        {
            // Arrange
            var request = new ExerciseCreateDto();

            // Act
            var validator = new ExerciseCreateDtoValidator(Context);
            var result    = validator.Validate(request);

            // Assert
            var hasCorrectErrorMessage = result.Errors.Any(x =>
                                                           x.ErrorMessage == "'Muscle Group' must not be empty." &&
                                                           x.PropertyName == nameof(ExerciseCreateDto.MuscleGroup));

            Assert.IsTrue(hasCorrectErrorMessage);
        }
        public void Validate_UserIdDoesNotExist_ReturnsCorrectErrorMessage()
        {
            // Arrange
            var request = new ExerciseCreateDto {
                UserId = NextId
            };

            // Act
            var validator = new ExerciseCreateDtoValidator(Context);
            var result    = validator.Validate(request);

            // Assert
            var hasCorrectErrorMessage = result.Errors.Any(x =>
                                                           x.ErrorMessage == "The 'User' does not exist." &&
                                                           x.PropertyName == nameof(ExerciseCreateDto.UserId));

            Assert.IsTrue(hasCorrectErrorMessage);
        }
        public void Validate_EverythingWorks_ReturnsNoErrorMessage()
        {
            // Arrange
            var name    = "Squat";
            var request = new ExerciseCreateDto {
                UserId = NextId, Name = name
            };

            // Act
            var validator = new ExerciseCreateDtoValidator(Context);
            var result    = validator.Validate(request);

            // Assert
            var hasCorrectErrorMessage = result.Errors.Any(x =>
                                                           x.ErrorMessage == "The 'User' does not exist." &&
                                                           x.PropertyName == "");

            Assert.IsTrue(!hasCorrectErrorMessage);
        }
Beispiel #11
0
        public ExerciseGetDto CreateExercise(ExerciseCreateDto exerciseCreateDto)
        {
            var exercise = new Exercise
            {
                Name        = exerciseCreateDto.Name,
                MuscleGroup = exerciseCreateDto.MuscleGroup,
                UserId      = exerciseCreateDto.UserId
            };

            _context.Set <Exercise>().Add(exercise);
            _context.SaveChanges();

            var exerciseGetDto = new ExerciseGetDto
            {
                Id          = exercise.Id,
                Name        = exercise.Name,
                MuscleGroup = exercise.MuscleGroup,
                UserId      = exercise.Id
            };

            exerciseGetDto.Id = exercise.Id;

            return(exerciseGetDto);
        }