Exemplo n.º 1
0
        public void ModelValidation_RequiresShortName_WhenShortNameIsNullOrWhitespace(string shortName)
        {
            // Arrange
            var postDto = new PostExerciseDto {
                ShortName = shortName
            };

            // Act
            var modelValidation = AspHelpers.ValidateDtoProperty(postDto, nameof(postDto.ShortName));

            // Assert
            modelValidation.IsValid.ShouldBeFalse();
            modelValidation.Results.First().ErrorMessage.ShouldContain($"{nameof(postDto.ShortName)} field is required");
        }
Exemplo n.º 2
0
        public void ModelValidation_RequiresMaxLenghtOf30_ForLongNameProperty()
        {
            // Arrange
            var postDto = new PostExerciseDto {
                LongName = new string('a', 61)
            };

            // Act
            var modelValidation = AspHelpers.ValidateDtoProperty(postDto, nameof(postDto.LongName));

            // Assert
            modelValidation.IsValid.ShouldBeFalse();
            modelValidation.Results.Count.ShouldBe(1);
            modelValidation.Results.First().ErrorMessage.ShouldContain("'60'");
        }
        public async Task <IActionResult> PostExercise([FromBody] PostExerciseDto exerciseDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.Values));
            }

            var postedExercise = await _exercisesService.CreateExercise(exerciseDto.ToExercise());

            if (postedExercise == null)
            {
                return(BadRequest());
            }

            return(StatusCode(201, postedExercise));
        }