Exemple #1
0
        public IActionResult OnPostDelete()
        {
            if (User.IsInRole("Admin"))
            {
                var product = _productRepository.GetProduct(Id);
                product.IsDeleted = true;
                var uploads = _uploadRepository.GetProductUploads(Id);
                foreach (var item in uploads)
                {
                    var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", Id.ToString(), item.FileName);
                    System.IO.File.Delete(path);
                    item.IsDeleted = true;
                    _uploadRepository.EditUpload(item);
                }

                var thumbnail = _uploadRepository.GetProductThumbnail(Id);
                var pathi     = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", Id.ToString(), thumbnail.FileName);
                System.IO.File.Delete(pathi);
                thumbnail.IsDeleted = true;
                _uploadRepository.EditUpload(thumbnail);
                _uploadRepository.SaveChanges();
                return(RedirectToPage("/Products/Index", new { toastr = "Product deleted successfully." }));
            }

            return(RedirectToPage("/Products/Index"));
        }
        public IActionResult OnPostAddUpload(string imageBytes)
        {
            try
            {
                var upload = new Upload();
                upload.IsCarousel      = true;
                upload.IsDeleted       = false;
                upload.IsThumbnail     = false;
                upload.CreatedOnDate   = DateTime.Now;
                upload.CreatedByUserId = _userManager.GetUserId(User);
                _uploadRepository.AddUpload(upload);
                _uploadRepository.SaveChanges();

                var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "carousels");

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                var fileName = "Banner" + upload.Id + ".png";

                imageBytes = imageBytes.Remove(0, 22);
                byte[] image = Convert.FromBase64String(imageBytes);

                var imagePath = System.IO.Path.Combine(path, fileName);
                System.IO.File.WriteAllBytes(imagePath, image);

                upload.Path     = "~/uploads/carousels/" + fileName;
                upload.FileName = fileName;

                _uploadRepository.EditUpload(upload);
                _uploadRepository.SaveChanges();

                return(RedirectToPage("/Panel/Carousel"));
            }
            catch (Exception ex)
            {
                return(RedirectToPage("/Panel/Carousel"));
            }
        }
Exemple #3
0
        public void AddUpload(string imageBytes, int productId, bool isEditing)
        {
            try
            {
                var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", productId.ToString());
                if (isEditing)
                {
                    var currentThumbnail = _uploadRepository.GetProductThumbnail(productId);
                    if (currentThumbnail != null)
                    {
                        currentThumbnail.IsDeleted = true;
                        _uploadRepository.EditUpload(currentThumbnail);
                        _uploadRepository.SaveChanges();
                        System.IO.File.Delete(System.IO.Path.Combine(path, "thumbnail.png"));
                    }
                }

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                imageBytes = imageBytes.Remove(0, 22);
                byte[] image     = Convert.FromBase64String(imageBytes);
                var    imagePath = System.IO.Path.Combine(path, "thumbnail.png");
                System.IO.File.WriteAllBytes(imagePath, image);
                var userId = _userManager.GetUserId(User);
                var upload = new Upload()
                {
                    Path            = "~/uploads/products/" + productId + "/thumbnail.png",
                    FileName        = "thumbnail.png",
                    ProductId       = productId,
                    CreatedOnDate   = DateTime.Now,
                    CreatedByUserId = userId,
                    IsThumbnail     = true,
                    IsCarousel      = false,
                    IsDeleted       = false
                };
                _uploadRepository.AddUpload(upload);
                _uploadRepository.SaveChanges();
            }
            catch (Exception ex)
            {
            }
        }
        public IActionResult DeleteProductUpload(int productId, int uploadId)
        {
            try
            {
                var upload = _uploadRepository.GetUpload(uploadId);
                upload.IsDeleted = true;
                _uploadRepository.EditUpload(upload);
                _uploadRepository.SaveChanges();

                var path = System.IO.Path.Combine(_env.WebRootPath, "uploads", "products", productId.ToString(), upload.FileName);
                System.IO.File.Delete(path);

                return(Ok(true));
            }
            catch (Exception)
            {
                return(Ok(false));
            }
        }