Exemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            picturesPath    = serverFilesManager.GetEnvFullPath(configuration.GetValue <string>("UploadPaths:Pictures"));
            attachmentsPath = serverFilesManager.GetEnvFullPath(configuration.GetValue <string>("UploadPaths:Attachments"));

            GetAvailableParentsAsSelectListItem();
            if (SelectedParentId != -1)
            {
                Product.ParentId = SelectedParentId;
            }
            else
            {
                Product.ParentId = null;
            }

            var uploadedPicturesNames = await serverFilesManager.UploadFilesToServer(PicturesUpload, picturesPath);

            var uploadedAttachmentsNames = await serverFilesManager.UploadFilesToServer(AttachmentsUpload, attachmentsPath);

            AddPicturesToProduct(uploadedPicturesNames);
            AddAttachmentsToProduct(uploadedAttachmentsNames);

            var deletedPictures    = productsData.RemovePictures(PicturesIdsToDelete).Select(p => p.Name);
            var deletedAttachments = productsData.RemoveAttachments(AttachmentsIdsToDelete).Select(p => p.Name);

            serverFilesManager.DeleteFilesFromServer(deletedPictures, picturesPath);
            serverFilesManager.DeleteFilesFromServer(deletedAttachments, attachmentsPath);

            if (ModelState.IsValid)
            {
                if (Product.Id > 0)
                {
                    productsData.Update(Product);
                    productsData.Commit();
                    TempData["Message"] = "Product updated";
                    return(RedirectToPage("Details", new { productId = Product.Id }));
                }
                else
                {
                    productsData.Add(Product);
                    productsData.Commit();
                    TempData["Message"] = "Product added";
                    return(RedirectToPage("List"));
                }
            }
            else
            {
                return(Page());
            }
        }
        public IActionResult OnPost(int productId)
        {
            picturesPath    = serverFilesManager.GetEnvFullPath(configuration.GetValue <string>("UploadPaths:Pictures"));
            attachmentsPath = serverFilesManager.GetEnvFullPath(configuration.GetValue <string>("UploadPaths:Attachments"));

            Product = productsData.GetById(productId);
            var childs = productsData.GetChilds(productId);

            if (childs.Count() > 0)
            {
                foreach (var child in childs)
                {
                    child.ParentId = null;
                    child.Parent   = null;
                }
            }

            var picturesIdsToDelete    = Product.Pictures.Select(p => p.Id).ToList();
            var attachmentsIdsToDelete = Product.Attachments.Select(a => a.Id).ToList();

            var deletedPictures    = productsData.RemovePictures(picturesIdsToDelete).Select(p => p.Name);
            var deletedAttachments = productsData.RemoveAttachments(attachmentsIdsToDelete).Select(p => p.Name);

            serverFilesManager.DeleteFilesFromServer(deletedPictures, picturesPath);
            serverFilesManager.DeleteFilesFromServer(deletedAttachments, attachmentsPath);

            productsData.Remove(productId);
            productsData.Commit();

            return(RedirectToPage("./List"));
        }