public async Task <IActionResult> Update(long id, IList <IFormFile> logo, IList <IFormFile> background)
        {
            var itemBlock = Context.ItemBlocks.SingleOrDefault(w => w.Id == id);

            if (itemBlock == null)
            {
                return(List());
            }

            var uploadedLogo       = logo.ImageUpload(HostingEnv.WebRootPath, false);
            var uploadedBackground = background.ImageUpload(HostingEnv.WebRootPath, false);
            var removeImages       = new List <string>();

            if (uploadedLogo.Any())
            {
                if (!string.IsNullOrEmpty(itemBlock.LogoUrl))
                {
                    removeImages.Add($"{HostingEnv.WebRootPath}{itemBlock.LogoUrl}");
                }
                itemBlock.LogoUrl = uploadedLogo.First().Location;
            }
            if (uploadedBackground.Any())
            {
                if (!string.IsNullOrEmpty(itemBlock.BackgroundUrl))
                {
                    removeImages.Add($"{HostingEnv.WebRootPath}{itemBlock.BackgroundUrl}");
                }
                itemBlock.BackgroundUrl = uploadedBackground.First().Location;
            }
            Context.ItemBlocks.Update(itemBlock);
            await Context.SaveChangesAsync();

            ImageManipulation.Remove(removeImages.ToArray());
            return(List());
        }
        public async Task <IActionResult> Remove(long id)
        {
            var itemBlock = Context.ItemBlocks.SingleOrDefault(w => w.Id == id);

            if (itemBlock == null)
            {
                return(List());
            }
            var removeFiles = new List <string>();

            if (!string.IsNullOrEmpty(itemBlock.LogoUrl))
            {
                removeFiles.Add($"{HostingEnv.WebRootPath}{itemBlock.LogoUrl}");
            }
            if (!string.IsNullOrEmpty(itemBlock.BackgroundUrl))
            {
                removeFiles.Add($"{HostingEnv.WebRootPath}{itemBlock.BackgroundUrl}");
            }

            Context.ItemBlocks.RemoveRange(itemBlock);
            await Context.SaveChangesAsync();

            ImageManipulation.Remove(removeFiles.ToArray());

            return(List());
        }
Esempio n. 3
0
        public async Task <IActionResult> Edit(long id, Item item, IList <IFormFile> imageUpload)
        {
            if (id != item.Id || !ItemExists(item.Id))
            {
                return(RedirectToAction("Index"));
            }
            if (item.UomId <= 0)
            {
                ModelState.Remove("UomId");
                ModelState.AddModelError("UomId", "Unit of Measure is Required!");
            }
            if (item.CategoryId <= 0)
            {
                ModelState.Remove("CategoryId");
                ModelState.AddModelError("CategoryId", "Category is Required!");
            }
            if (item.SubCategoryId <= 0)
            {
                ModelState.Remove("SubCategoryId");
                ModelState.AddModelError("SubCategoryId", "Sub Category is Required!");
            }
            if (ModelState.IsValid)
            {
                var imageLocations = new List <string>();
                var images         = imageUpload.ImageUpload(HostingEnv.WebRootPath).Select(s => new ImageLocation
                {
                    ItemId       = item.Id,
                    FileName     = s.Name,
                    FileLocation = s.Location
                }).ToList();

                if (images.Any())
                {
                    Context.ImageLocations.Where(w => w.ItemId == item.Id).ToList().ForEach(e =>
                    {
                        imageLocations.Add($"{HostingEnv.WebRootPath}{e.FileLocation}");
                        Context.ImageLocations.Remove(e);
                    });
                    Context.ImageLocations.AddRange(images);
                }

                using (var transaction = Context.Database.BeginTransaction())
                {
                    try
                    {
                        if (item.ItemHistories == null)
                        {
                            item.ItemHistories = new List <ItemHistory>();
                        }
                        var changes = Context.Item.AsNoTracking().First(f => f.Id == item.Id)
                                      .GetChangesFrom(item);
                        item.ItemHistories.Add(new ItemHistory
                        {
                            Action       = string.IsNullOrEmpty(changes) ? "Update Item" : changes,
                            ActionDate   = DateTime.Now,
                            ActionUserId = UserManager.GetUserId(User)
                        });
                        Context.Update(item);
                        await Context.SaveChangesAsync();

                        transaction.Commit();
                        ImageManipulation.Remove(imageLocations.ToArray());
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(item));
        }