public IActionResult UpdateGallery(int id)
        {
            if (id == 0)
            {
                ViewData["GeneralError"] = Messages.GeneralError;
                return(RedirectToAction("ListGallery", "ManagementPanel"));
            }

            var data = _galleryService.GetById(id);

            if (data.Data == null)
            {
                ViewData["GeneralError"] = Messages.GeneralError;
                return(RedirectToAction("ListGallery", "ManagementPanel"));
            }

            UpdateGalleryPageModel model = new UpdateGalleryPageModel
            {
                Description = data.Data.Description,
                Id          = data.Data.Id
            };



            var selectBox = GetCategoryImageSelectBoxView(data.Data);



            ViewData["AllCategories"] = selectBox;


            return(View(model));
        }
        public IActionResult UpdateGallery(UpdateGalleryPageModel model)
        {
            var data = _galleryService.GetById(model.Id);

            if (!ModelState.IsValid)
            {
                ViewData["GeneralError"] = Messages.GeneralError;
                var selectBox = GetCategoryImageSelectBoxView(data.Data);

                ViewData["AllCategories"] = selectBox;
                return(View(model));
            }

            string oldImage = data.Data.Source;

            data.Data.Description = model.Description;

            if (model.File != null)
            {
                var source = _galleryService.UploadGalleryImage(model.File, ImageSavePaths.GallerySavePath);
                if (source.Success)
                {
                    data.Data.Source = source.Data;
                    var result = _galleryService.Update(data.Data, oldImage);

                    if (result.Success)
                    {
                        bool isSuccess     = true;
                        var  oldCategories = _galleryCategoryService.GetGalleryId(data.Data.Id);

                        foreach (var oldCat in oldCategories.Data)
                        {
                            var res = _galleryCategoryService.Delete(oldCat);
                            isSuccess = res.Success;
                            if (!isSuccess)
                            {
                                break;
                            }
                        }

                        if (!isSuccess)
                        {
                            foreach (var oldCat in oldCategories.Data)
                            {
                                _galleryCategoryService.Add(oldCat);
                            }
                        }
                        else
                        {
                            foreach (var newCat in model.CategoriesId)
                            {
                                GalleryCategory galleryCategory = new GalleryCategory();
                                galleryCategory.GalleryImageId  = model.Id;
                                galleryCategory.CategoryImageId = newCat;
                                _galleryCategoryService.Add(galleryCategory);
                            }
                        }

                        ViewData["GeneralSuccess"] = result.Message;
                        return(RedirectToAction("ListGallery", "ManagementPanel"));
                    }
                }
            }
            else
            {
                var result = _galleryService.Update(data.Data, "");
                if (result.Success)
                {
                    bool isSuccess     = true;
                    var  oldCategories = _galleryCategoryService.GetGalleryId(data.Data.Id);

                    foreach (var oldCat in oldCategories.Data)
                    {
                        var res = _galleryCategoryService.Delete(oldCat);
                        isSuccess = res.Success;
                        if (!isSuccess)
                        {
                            break;
                        }
                    }

                    if (!isSuccess)
                    {
                        foreach (var oldCat in oldCategories.Data)
                        {
                            _galleryCategoryService.Add(oldCat);
                        }
                    }
                    else
                    {
                        foreach (var newCat in model.CategoriesId)
                        {
                            GalleryCategory galleryCategory = new GalleryCategory();
                            galleryCategory.GalleryImageId  = model.Id;
                            galleryCategory.CategoryImageId = newCat;
                            _galleryCategoryService.Add(galleryCategory);
                        }
                    }
                    ViewData["GeneralSuccess"] = result.Message;
                    return(RedirectToAction("ListGallery", "ManagementPanel"));
                }
            }


            ViewData["GeneralError"] = Messages.GeneralError;
            return(RedirectToAction("ListGallery", "ManagementPanel"));
        }