Пример #1
0
        public bool UpdateSubCategory(SubCategoryAdminIndexViewModel modelIn, IFormFile imageName)
        {
            bool result;

            try
            {
                var subCategory = this.subCategories.All().FirstOrDefault(x => x.Id == (int)modelIn.Id);
                subCategory.Name       = modelIn.Name;
                subCategory.CategoryId = modelIn.CategoryId;

                if (imageName != null && imageName.Length > 0)
                {
                    using (var ms = new MemoryStream())
                    {
                        imageName.CopyTo(ms);
                        var fileBytes = ms.ToArray();
                        subCategory.Image = fileBytes;
                    }
                }

                this.subCategories.Update(subCategory);
                this.subCategories.SaveChanges();
                result = true;
            }
            catch
            {
                result = false;
            }

            return(result);
        }
        public IActionResult AddEditGlobalCategories(SubCategoryAdminIndexViewModel modelIn)
        {
            if (modelIn.Id != null)
            {
                this.ViewBag.Title = "Edit Sub Category";
                var cat = this.categoryService.GetAllSubCategories().FirstOrDefault(x => x.Id == modelIn.Id);
                modelIn.Image = cat.Image;
            }
            else
            {
                this.ViewBag.Title = "New Sub Category";
            }

            return(this.View(modelIn));
        }
        public IActionResult AddEditGlobalCategory(SubCategoryAdminIndexViewModel modelIn, IFormFile imageName)
        {
            if (modelIn.CategoryId != 0)
            {
                modelIn.Category = this.categoryService.GetAllCategories().FirstOrDefault(x => x.Id == modelIn.CategoryId).Name;
            }

            if (this.ModelState.IsValid)
            {
                if (modelIn.Id == null || modelIn.Id == 0)
                {
                    try
                    {
                        modelIn.Id = 0;
                        var subCategory = new SubCategory
                        {
                            Name       = modelIn.Name,
                            CategoryId = modelIn.CategoryId,
                        };
                        this.categoryService.SaveSubCategoryAsync(subCategory, imageName);
                        return(this.RedirectToAction("GlobalCategories", "Category", new { toastr = "New category name has been created successfully." }));
                    }
                    catch
                    {
                        return(this.RedirectToAction("GlobalCategories", "Category", new { toastr = "ERROR: No category translation has been created. Try again." }));
                    }
                }
                else
                {
                    try
                    {
                        this.categoryService.UpdateSubCategory(modelIn, imageName);
                        return(this.RedirectToAction("GlobalCategories", "Category", new { toastr = "The category has been updated successfully" }));
                    }
                    catch
                    {
                        return(this.RedirectToAction("GlobalCategories", "Category", new { toastr = "ERROR: Category hasn't been updated. Try again." }));
                    }
                }
            }

            this.ViewBag.Message = modelIn.Id != 0 ? "Edit Category" : "New Category";
            return(this.View("AddEditGlobalCategories", modelIn));
        }