//GET: /Category/Create
        public IActionResult Create()
        {
            var model = new CategoryCreateOrUpdateModel();

            model.ParentCategorySelectList = _viewHelper.GetParentCategorySelectList();

            return(View(model));
        }
        public IActionResult Create(CategoryCreateOrUpdateModel model, bool continueEditing)
        {
            var hasError = false;

            if (ModelState.IsValid)
            {
                //check if name exists
                if (_dataHelper.CheckForDuplicate(ServiceType.Category, DataType.Name, model.Name))
                {
                    ModelState.AddModelError(string.Empty, "Category name already exists.");
                    hasError = true;
                }

                //create seo friendly url if the user didn't provide it
                if (string.IsNullOrEmpty(model.SeoUrl))
                {
                    model.SeoUrl = _dataHelper.GenerateSeoFriendlyUrl(ServiceType.Category, model.Name);
                }
                else
                {
                    //check if seo already exists
                    if (_dataHelper.CheckForDuplicate(ServiceType.Category, DataType.Seo, model.SeoUrl))
                    {
                        ModelState.AddModelError(string.Empty, "SEO Url already exists");
                        hasError = true;
                    }
                }

                //if everything is valid
                if (!hasError)
                {
                    //map model to entity
                    var categoryEntity = _mapper.Map <CategoryCreateOrUpdateModel, Category>(model);
                    categoryEntity.DateAdded    = DateTime.Now;
                    categoryEntity.DateModified = DateTime.Now;

                    //save
                    _categoryService.InsertCategory(categoryEntity);

                    if (continueEditing)
                    {
                        return(RedirectToAction("Edit", new { id = categoryEntity.Id, ActiveTab = model.ActiveTab }));
                    }

                    return(RedirectToAction("Edit", new { id = categoryEntity.Id, ActiveTab = model.ActiveTab }));
                }
            }

            //something went wrong, redisplay form
            model.ParentCategorySelectList = _viewHelper.GetParentCategorySelectList();
            return(View("List"));
        }
        public IActionResult Edit(CategoryCreateOrUpdateModel model, bool continueEditing)
        {
            var hasError = false;

            if (ModelState.IsValid)
            {
                // get model from session
                var sessionModel = JsonConvert.DeserializeObject <CategoryCreateOrUpdateModel>(Session.GetString(_sessionKey));
                model.Id        = sessionModel.Id;
                model.DateAdded = sessionModel.DateAdded;

                // check if user edit the name
                if (model.Name.ToLower() != sessionModel.Name.ToLower())
                {
                    // check if name exist
                    if (_dataHelper.CheckForDuplicate(ServiceType.Category, DataType.Name, model.Name))
                    {
                        ModelState.AddModelError(string.Empty, "Category name already exist");
                        hasError = true;
                    }
                }

                // create seo friendly url if the user didn't provide
                if (string.IsNullOrEmpty(model.SeoUrl))
                {
                    model.SeoUrl = _dataHelper.GenerateSeoFriendlyUrl(ServiceType.Category, model.Name);
                }
                else
                {
                    // check if user change seo url
                    if (model.SeoUrl.ToLower() != sessionModel.SeoUrl.ToLower())
                    {
                        // check if seo already exist
                        if (_dataHelper.CheckForDuplicate(ServiceType.Category, DataType.Seo, model.SeoUrl))
                        {
                            ModelState.AddModelError(string.Empty, "SEO Url already exist");
                            hasError = true;
                        }
                    }
                }

                // if everything works
                if (!hasError)
                {
                    // map model to entity
                    var categoryEntity = _mapper.Map <CategoryCreateOrUpdateModel, Category>(model);
                    categoryEntity.DateModified = DateTime.Now;

                    // save
                    _categoryService.UpdateCategory(categoryEntity);

                    if (continueEditing)
                    {
                        return(RedirectToAction("Edit", new { id = categoryEntity.Id, ActiveTab = model.ActiveTab }));
                    }

                    return(RedirectToAction("List"));
                }
            }

            // something went wrong, redisplay form
            model.ParentCategorySelectList = _viewHelper.GetParentCategorySelectList();
            return(View(model));
        }