Пример #1
0
        public ActionResult Edit(int id)
        {
            var category  = categoryRepository.GetById(id);
            var viewModel = new CategoryFormModel(category);

            return(View(viewModel));
        }
Пример #2
0
 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));
     }
 }
Пример #3
0
        public ActionResult Save(CategoryFormModel form)
        {
            if (ModelState.IsValid)
            {
                //TODO: Create Automapper form => command
                var command = new CreateOrUpdateCategoryCommand()
                {
                    CategoryId  = form.CategoryId,
                    Name        = form.Name,
                    Description = 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));
            }
        }
Пример #4
0
 protected override async Task OnInitializedAsync()
 {
     this.categoryId = this.Router.GetQueryParam("categoryId");
     if (this.Action.ToLower() == "edit" && !string.IsNullOrWhiteSpace(this.categoryId))
     {
         this.categoryForm = await this.CategoryService.GetCategory(this.categoryId);
     }
 }
        public async Task <IActionResult> Create(CategoryFormModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            await category.Create(model.Name);

            TempData.AddSuccessMessage(string.Format(WebConstants.SuccessMessageCategoryCreatePlaceholder, model.Name));
            return(RedirectToAction(nameof(All)));
        }
Пример #6
0
        public async Task <IActionResult> Edit(int id, CategoryFormModel model)
        {
            if (!await this.adminCategoryService.ExistsAsync(id))
            {
                return(BadRequest());
            }

            await this.adminCategoryService.EditAsync(id, model.Name);

            TempData.AddSuccessMessage("The category was successfully edited.");

            return(RedirectToAction(nameof(Index)));
        }
Пример #7
0
        public IActionResult Create(CategoryFormModel model)
        {
            if (!ModelState.IsValid)
            {
                TempData.AddErrorMessage("Моля, проверете въведените данни и опитайте отново.");

                return(View(model));
            }

            this._categoryService.Create(model.Name);

            TempData.AddSuccessMessage("Успешно добавихте категория.");
            return(RedirectToAction(nameof(List)));
        }
Пример #8
0
        public async Task <IActionResult> Create(CategoryFormModel model)
        {
            if (await this.adminCategoryService.ExistsAsync(model.Name))
            {
                TempData.AddErrorMessage($"Category '{model.Name}' already exists.");

                return(View(model));
            }

            await this.adminCategoryService.CreateAsync(model.Name);

            TempData.AddSuccessMessage($"Category {model.Name} successfully created.");

            return(View());
        }
Пример #9
0
        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"]);
        }
Пример #10
0
        public ActionResult Create()
        {
            var model = new CategoryFormModel();

            try
            {
                var category = CategoryService.CategoryNew();

                this.Map(category, model, true);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
            }

            return(this.View(model));
        }
Пример #11
0
        public ActionResult Edit(int id, CategoryFormModel model)
        {
            var category = CategoryService.CategoryFetch(id);

            Csla.Data.DataMapper.Map(model, category, true);

            category = CategoryService.CategorySave(category);

            if (category.IsValid)
            {
                model.Message = Resources.SaveSuccessfulMessage;
            }

            this.Map(category, model, true);

            return(this.View(model));
        }
Пример #12
0
        public CategoryFormModel Map(Category category, CategoryFormModel model, bool ignoreBrokenRules)
        {
            Csla.Data.DataMapper.Map(category, model, true);

            model.Tab     = "Home";
            model.IsNew   = category.IsNew;
            model.IsValid = category.IsValid;

            if (!ignoreBrokenRules)
            {
                foreach (var brokenRule in category.BrokenRulesCollection)
                {
                    this.ModelState.AddModelError(string.Empty, brokenRule.Description);
                }
            }

            return(model);
        }
Пример #13
0
        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);
        }
Пример #14
0
        public ActionResult Create(CategoryFormModel model)
        {
            var category = CategoryService.CategoryNew();

            Csla.Data.DataMapper.Map(model, category, true);

            category = CategoryService.CategorySave(category);

            if (category.IsValid)
            {
                return(new JsonResult {
                    Data = this.Url.Action("Edit", new { id = category.CategoryId, message = Resources.SaveSuccessfulMessage })
                });
            }

            this.Map(category, model, false);

            return(this.View(model));
        }
Пример #15
0
        public ActionResult Edit(int id, string message)
        {
            var model = new CategoryFormModel();

            try
            {
                var category = CategoryService.CategoryFetch(id);

                model.Message = message;

                this.Map(category, model, true);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError(string.Empty, ex.Message);
            }

            return(this.View(model));
        }
Пример #16
0
 public async Task CreateCategory(CategoryFormModel category)
 {
     await this.httpClient.PostAsync <string>(this.categoriesUrl, category);
 }
Пример #17
0
 public async Task EditCategory(string categoryId, CategoryFormModel category)
 {
     await this.httpClient.PutAsync <string>(this.categoriesUrl + categoryId, category);
 }