Example #1
0
        public async Task UpdateAsync_ShouldThrowException_WhenExerciseExistsButNameIsNotUnique()
        {
            var existing = "randomExercise";
            var exercise = _fixture.Build <Exercise>()
                           .Create();

            _exerciseRepository.GetAsync(exercise.Id).Returns(exercise);

            _exerciseRepository.AnyAsync(e => e.Name == existing).ReturnsForAnyArgs(true);

            var exception = await Record.ExceptionAsync(() => _sut.UpdateAsync(exercise.Id, existing,
                                                                               exercise.PartOfBody.Name.ToString()));

            exception.ShouldNotBeNull();
            exception.ShouldBeOfType(typeof(NameInUseException));
            exception.Message.ShouldBe($"Exercise with name: '{existing}' already exists.");
        }
Example #2
0
        public async Task UpdateAsync(int id, string name, string partOfBody)
        {
            var exercise = await _exerciseRepository.GetOrFailAsync(id);

            // If exercise has the same name, ensure that name will be searching for exercise with this name
            if (await _exerciseRepository.AnyAsync(p => p.Name == name))
            {
                if (exercise.Name != name)
                {
                    throw new NameInUseException(nameof(Exercise), name);
                }
            }

            exercise.SetName(name);
            exercise.PartOfBody.Id = PartOfBody.GetPart(partOfBody).Id;

            await _exerciseRepository.UpdateAsync(exercise);
        }