public ActionResult Save(CategoryFormModel form)
        {
            if (ModelState.IsValid)
            {
                var command = new CreateOrUpdateCategoryCommand(form.CategoryId, form.Name, form.Description);
                IEnumerable<ValidationResult> errors = commandBus.Validate(command);
                ModelState.AddModelErrors(errors);
                if (ModelState.IsValid)
                {
                    var result = commandBus.Submit(command);
                    if (result.Success) return RedirectToAction("Index");
                }
            }

            if (form.CategoryId == 0)
                return View("Create", form);
            else
                return View("Edit", form);
        }
Exemple #2
0
        public void CategoryCreateTest()
        {
            using (var lifetime = container.BeginLifetimeScope())
            {
                ICategoryRepository categoryRepository = lifetime.Resolve<ICategoryRepository>();
                DefaultCommandBus commandBus = lifetime.Resolve<DefaultCommandBus>();

                Category category = new Category()
                {
                    Name = "Test Category",
                    Description = "This is a test category"
                };

                CreateOrUpdateCategoryCommand command = new CreateOrUpdateCategoryCommand(category);
                IValidationHandler<CreateOrUpdateCategoryCommand> validationHandler = lifetime.Resolve<IValidationHandler<CreateOrUpdateCategoryCommand>>();
                IEnumerable<ValidationResult> validations = commandBus.Validate(command, validationHandler);
                foreach (var val in validations)
                {
                    Assert.IsNull(val, "Error: Category creation did not validate " + val.Message);
                }
                ICommandHandler<CreateOrUpdateCategoryCommand> commnadHandler = lifetime.Resolve<ICommandHandler<CreateOrUpdateCategoryCommand>>();
                ICommandResult result = commandBus.Submit(command, commnadHandler);
                Assert.IsNotNull(result, "Error: Category was not created by commandBus");
                Assert.IsTrue(result.Success, "Error: Category was not created by commandBus");
            }
        }
Exemple #3
0
        public void CategoryUpdateTest()
        {
            Category category;

            using (var lifetime = container.BeginLifetimeScope())
            {
                ICategoryRepository categoryRepository = lifetime.Resolve<ICategoryRepository>();
                DefaultCommandBus commandBus = lifetime.Resolve<DefaultCommandBus>();

                category = categoryRepository.Get(c => c.Name == "Test Category");
                Assert.IsNotNull(category, "Error: Category was now found.");

                category.Name = "Updated Test Category";

                CreateOrUpdateCategoryCommand command = new CreateOrUpdateCategoryCommand(category);
                IValidationHandler<CreateOrUpdateCategoryCommand> validationHandler = lifetime.Resolve<IValidationHandler<CreateOrUpdateCategoryCommand>>();
                IEnumerable<ValidationResult> validations = commandBus.Validate(command, validationHandler);

                foreach (var val in validations)
                {
                    Assert.IsNull(val, "Error: Category creation did not validate " + val.Message);
                }
            }

            using (var lifetime = container.BeginLifetimeScope())
            {
                DefaultCommandBus commandBus = lifetime.Resolve<DefaultCommandBus>();

                CreateOrUpdateCategoryCommand command = new CreateOrUpdateCategoryCommand(category);

                ICommandHandler<CreateOrUpdateCategoryCommand> commnadHandler = lifetime.Resolve<ICommandHandler<CreateOrUpdateCategoryCommand>>();
                ICommandResult result = commandBus.Submit(command, commnadHandler);
                Assert.IsNotNull(result, "Error: Category was not updated by CommandBus");
                Assert.IsTrue(result.Success, "Error: Provincia was not updated by CommandBus");
            }
        }