コード例 #1
0
        public async Task <CreateCategoryCommandResponse> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
        {
            var createCategoryCommandResponse = new CreateCategoryCommandResponse();

            var validator        = new CreateCategoryCommandValidator();
            var validationResult = await validator.ValidateAsync(request);

            if (validationResult.Errors.Count > 0)
            {
                createCategoryCommandResponse.Success          = false;
                createCategoryCommandResponse.ValidationErrors = new List <string>();
                foreach (var error in validationResult.Errors)
                {
                    createCategoryCommandResponse.ValidationErrors.Add(error.ErrorMessage);
                }
            }
            if (createCategoryCommandResponse.Success)
            {
                var category = new Category()
                {
                    Name = request.Name
                };
                category = await _categoryRepository.AddAsync(category);

                createCategoryCommandResponse.Category = _mapper.Map <CreateCategoryDto>(category);
            }

            return(createCategoryCommandResponse);
        }
コード例 #2
0
        public CreateCategoryCommand(string descricao)
        {
            Descricao = descricao;
            var validator = new CreateCategoryCommandValidator();

            Validation = validator.Validate(this);
        }
コード例 #3
0
 public void Create_Category_With_No_Name_Throws_Exception()
 {
     using (var context = GetContextWithData())
     {
         var validator = new CreateCategoryCommandValidator(context);
         validator.ShouldHaveValidationErrorFor(x => x.Name, string.Empty);
     }
 }
コード例 #4
0
        public void Should_have_error_when_category_title_is_empty()
        {
            var categoryRepository = new Mock <ICategoryRepository>();

            var validator = new CreateCategoryCommandValidator(categoryRepository.Object);

            validator.ShouldHaveValidationErrorFor(x => x.Title, new CreateCategoryCommand(Guid.NewGuid(), ""));
        }
コード例 #5
0
        public void CreateCategoryCommand_Has_Empty_CategoryName_ShouldBeInvalid()
        {
            var command = new CreateCategoryCommand {
                CategoryName = string.Empty
            };

            var validator = new CreateCategoryCommandValidator();
            var result    = validator.TestValidate(command);

            result.ShouldHaveValidationErrorFor(x => x.CategoryName);
        }
コード例 #6
0
 public void Create_Category_With_Name_That_Does_Not_Already_Exist()
 {
     using (var context = GetContextWithData())
     {
         var validator = new CreateCategoryCommandValidator(context);
         var result    = validator.TestValidate(new CreateCategoryCommand
         {
             Id   = new Guid(),
             Name = "New category"
         });
         result.ShouldNotHaveValidationErrorFor(x => x);
     }
 }
コード例 #7
0
 public void Create_Category_With_Name_That_Already_Exists_Throws_Exception()
 {
     using (var context = GetContextWithData())
     {
         var validator = new CreateCategoryCommandValidator(context);
         var result    = validator.TestValidate(new CreateCategoryCommand
         {
             Id   = new Guid(),
             Name = context.Categories.FirstOrDefault()?.Name
         });
         result.ShouldHaveValidationErrorFor(x => x);
     }
 }
コード例 #8
0
        public void Should_have_error_when_category_title_already_exists()
        {
            var          blogId        = Guid.NewGuid();
            const string categoryTitle = "My Category";

            var categoryRepository = new Mock <ICategoryRepository>();

            categoryRepository.Setup(x => x.GetByBlogIdAndTitle(blogId, categoryTitle)).Returns(Category.CreateNew(blogId, categoryTitle));

            var validator = new CreateCategoryCommandValidator(categoryRepository.Object);

            validator.ShouldHaveValidationErrorFor(x => x.Title, new CreateCategoryCommand(blogId, categoryTitle));
        }
コード例 #9
0
        public void IsValid_ShouldBeTrue_WhenListTitleIsUnique()
        {
            var command = new CreateCategoryCommand
            {
                CategoryName = "Food 2"
            };

            var validator = new CreateCategoryCommandValidator(Context);

            var result = validator.Validate(command);

            result.IsValid.ShouldBe(true);
        }
コード例 #10
0
        public void IsValid_ShouldBeFalse_WhenListTitleIsNotUnique()
        {
            Context.Categories.Add(new Category {
                CategoryName = "Shopping"
            });
            Context.SaveChanges();

            var command = new CreateCategoryCommand
            {
                CategoryName = "Shopping"
            };

            var validator = new CreateCategoryCommandValidator(Context);

            var result = validator.Validate(command);

            result.IsValid.ShouldBe(false);
        }