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)
            {
                //updating data to Cache
                var categories = categoryRepository.GetAll();
                cache.Put("categories", categories);
                return RedirectToAction("Index");
            }
        }                
     }   
     //if fail
     if (form.CategoryId == 0)
         return View("Create",form);
     else
         return View("Edit", form);
 }         
        public ActionResult Edit(int id)
        {

            var category = categoryRepository.GetById(id);
            var viewModel = new CategoryFormModel(category);
            return View(viewModel);
        }
 public void Create_Category_Redirects_To_Index()
 {
     // Arrange
     CategoryController controller = new CategoryController(commandBus.Object, categoryRepository.Object);
     commandBus.Setup(c => c.Submit(It.IsAny<CreateOrUpdateCategoryCommand>())).Returns(new CommandResult(true));
     // Act
     CategoryFormModel category = new CategoryFormModel();
     category.CategoryId = 0;
     category.Name = "Mock Category";
     Mapper.CreateMap<CategoryFormModel, CreateOrUpdateCategoryCommand>();
     var result = controller.Save(category) as RedirectToRouteResult;
     // Assert
     Assert.AreEqual("Index", result.RouteValues["action"]);
 }
 public void Cannot_Create_Empty_Category()
 {
     // Arrange
     CategoryController controller = new CategoryController(commandBus.Object, categoryRepository.Object);
     // The MVC pipeline doesn't run, so binding and validation don't run.
     controller.ModelState.AddModelError("", "mock error message");
     Mapper.CreateMap<CategoryFormModel, CreateOrUpdateCategoryCommand>();
     // Act
     CategoryFormModel category = new CategoryFormModel();
     category.CategoryId = 0;
     category.Name = string.Empty;
     var result = controller.Save(category) as ViewResult;
     // Assert -  check that we are passing an invalid model to the view
     Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
     Assert.AreEqual("Create", result.ViewName);
 }
 public ActionResult Save(CategoryFormModel form)
 {
     if (ModelState.IsValid)
     {
         var command = Mapper.Map<CategoryFormModel, CreateOrUpdateCategoryCommand>(form);
         IEnumerable<ValidationResult> errors = commandBus.Validate(command);
         ModelState.AddModelErrors(errors);
         if (ModelState.IsValid)
         {
             var result = commandBus.Submit(command);
             if (result.Success) return RedirectToAction("Index");
         }
     }
     //if fail
     if (form.CategoryId == 0)
         return View("Create", form);
     else
         return View("Edit", form);
 }