Esempio n. 1
0
        public ActionResult Action(int?ID)
        {
            CategoryActionViewModel model = new CategoryActionViewModel();

            if (ID.HasValue)
            {
                var category = CategoriesService.Instance.GetCategoryByID(ID.Value);

                if (category == null)
                {
                    return(HttpNotFound());
                }

                model.PageTitle       = "Edit Category";
                model.PageDescription = string.Format("Edit Category {0}.", category.Name);

                model.ParentCategoryID = category.ParentCategoryID.HasValue ? category.ParentCategoryID.Value : 0;
                model.ID          = category.ID;
                model.Name        = category.Name;
                model.ArName      = category.ArName;
                model.Description = category.Description;
                model.isFeatured  = category.isFeatured;
            }
            else
            {
                model.PageTitle       = "Create Category";
                model.PageDescription = "Create New Category.";
            }

            model.Categories = CategoriesService.Instance.GetAllCategories();

            return(View(model));
        }
        public ActionResult Action(int?ID)
        {
            CategoryActionViewModel model = new CategoryActionViewModel();

            if (ID.HasValue)
            {
                var category = CategoriesService.Instance.GetCategoryByID(ID.Value);

                if (category == null)
                {
                    return(HttpNotFound());
                }

                var currentLanguageRecord = category.CategoryRecords.FirstOrDefault(x => x.LanguageID == AppDataHelper.CurrentLanguage.ID);

                currentLanguageRecord = currentLanguageRecord ?? new CategoryRecord();

                model.CategoryID       = category.ID;
                model.ParentCategoryID = category.ParentCategoryID.HasValue ? category.ParentCategoryID.Value : 0;
                model.isFeatured       = category.isFeatured;
                model.SanitizedName    = category.SanitizedName;
                model.Picture          = category.Picture;
                model.PictureID        = category.PictureID;

                model.CategoryRecordID = currentLanguageRecord.ID;
                model.Name             = currentLanguageRecord.Name;
                model.Description      = currentLanguageRecord.Description;
                model.Summary          = currentLanguageRecord.Summary;
            }

            model.Categories = CategoriesService.Instance.GetCategories();

            return(View(model));
        }
Esempio n. 3
0
        public ActionResult Action(CategoryActionViewModel model)
        {
            if (model.ID > 0)
            {
                var category = CategoriesService.Instance.GetCategoryByID(model.ID);

                if (category == null)
                {
                    return(HttpNotFound());
                }

                if (model.ParentCategoryID > 0)
                {
                    category.ParentCategoryID = model.ParentCategoryID;
                }
                else
                {
                    category.ParentCategoryID = null;
                    category.ParentCategory   = null;
                }

                category.Name          = model.Name;
                category.ArName        = model.ArName;
                category.SanitizedName = model.Name.SanitizeLowerString();
                category.Description   = model.Description;
                category.isFeatured    = model.isFeatured;
                category.DisplaySeqNo  = 1;

                CategoriesService.Instance.UpdateCategory(category);
            }
            else
            {
                Category category = new Category();

                if (model.ParentCategoryID > 0)
                {
                    category.ParentCategoryID = model.ParentCategoryID;
                }

                category.Name          = model.Name;
                category.ArName        = model.ArName;
                category.SanitizedName = model.Name.SanitizeLowerString();
                category.Description   = model.Description;
                category.isFeatured    = model.isFeatured;
                category.DisplaySeqNo  = 1;

                CategoriesService.Instance.SaveCategory(category);
            }

            return(RedirectToAction("Index"));
        }
        public JsonResult Action(CategoryActionViewModel model)
        {
            JsonResult json = new JsonResult();

            try
            {
                if (model.CategoryID > 0)
                {
                    var category = CategoriesService.Instance.GetCategoryByID(model.CategoryID);

                    if (category == null)
                    {
                        throw new Exception("Dashboard.Categories.Action.Validation.CategoryNotFound".LocalizedString());
                    }

                    if (model.ParentCategoryID > 0)
                    {
                        category.ParentCategoryID = model.ParentCategoryID;
                    }
                    else
                    {
                        category.ParentCategoryID = null;
                        category.ParentCategory   = null;
                    }

                    category.PictureID     = model.PictureID;
                    category.isFeatured    = model.isFeatured;
                    category.SanitizedName = !string.IsNullOrEmpty(model.SanitizedName) ? model.SanitizedName : model.Name.SanitizeLowerString();

                    category.ModifiedOn = DateTime.Now;

                    if (!CategoriesService.Instance.UpdateCategory(category))
                    {
                        throw new Exception("Dashboard.Categories.Action.Validation.UnableToUpdateCategory".LocalizedString());
                    }

                    var currentCategoryRecord = category.CategoryRecords.FirstOrDefault(x => x.LanguageID == AppDataHelper.CurrentLanguage.ID);

                    var isNewRecord = false;

                    if (currentCategoryRecord == null)
                    {
                        currentCategoryRecord = new CategoryRecord();
                        isNewRecord           = true;
                    }

                    currentCategoryRecord.CategoryID  = category.ID;
                    currentCategoryRecord.LanguageID  = AppDataHelper.CurrentLanguage.ID;
                    currentCategoryRecord.Name        = model.Name;
                    currentCategoryRecord.Description = model.Description;
                    currentCategoryRecord.Summary     = model.Summary;

                    currentCategoryRecord.ModifiedOn = DateTime.Now;

                    var result = false;
                    if (isNewRecord)
                    {
                        result = CategoriesService.Instance.SaveCategoryRecord(currentCategoryRecord);
                    }
                    else
                    {
                        result = CategoriesService.Instance.UpdateCategoryRecord(currentCategoryRecord);
                    }

                    if (!result)
                    {
                        throw new Exception("Dashboard.Categories.Action.Validation.UnableToUpdateCategoryRecord".LocalizedString());
                    }
                }
                else
                {
                    var category = new Category();

                    if (model.ParentCategoryID > 0)
                    {
                        category.ParentCategoryID = model.ParentCategoryID;
                    }
                    else
                    {
                        category.ParentCategoryID = null;
                        category.ParentCategory   = null;
                    }

                    category.PictureID     = model.PictureID;
                    category.isFeatured    = model.isFeatured;
                    category.SanitizedName = !string.IsNullOrEmpty(model.SanitizedName) ? model.SanitizedName : model.Name.SanitizeLowerString();

                    var currentLanguageCategoryRecord = new CategoryRecord
                    {
                        Category    = category,
                        LanguageID  = AppDataHelper.CurrentLanguage.ID,
                        Name        = model.Name,
                        Description = model.Description,
                        Summary     = model.Summary,
                        ModifiedOn  = DateTime.Now
                    };

                    var result = CategoriesService.Instance.SaveCategoryRecord(currentLanguageCategoryRecord);

                    if (!result)
                    {
                        throw new Exception("Dashboard.Categories.Action.Validation.UnableToCreateCategory".LocalizedString());
                    }
                }

                json.Data = new { Success = true };
            }
            catch (Exception ex)
            {
                json.Data = new { Success = false, Message = ex.Message };
            }

            return(json);
        }