Exemplo n.º 1
0
        public void RemoveProduct(int productId)
        {
            Product prodDelete = _productRepository.GetAll()
                                 .Include(p => p.ProductImages)
                                 .SingleOrDefault(p => p.Id == productId);

            if (prodDelete != null)
            {
                string path = Environment.CurrentDirectory +
                              ConfigurationManager.AppSettings["ImageStore"].ToString();
                foreach (var item in prodDelete.ProductImages.ToList()) // удаляем фотки из папки
                {
                    string imageOrigin = path + "o_" + item.Name;
                    File.Delete(imageOrigin);
                    string imageSmall = path + "s_" + item.Name;
                    File.Delete(imageSmall);
                    var prodImage = _productImageRepository
                                    .GetAll(prodDelete.Id)
                                    .SingleOrDefault(pi => pi.Id == item.Id);
                    if (prodImage != null)
                    {
                        _productImageRepository.Remove(prodImage);
                        _productImageRepository.SaveChanges();
                    }
                }
                //_productImageRepository.Remove();
                //_productImageRepository.SaveChanges();
                //_productRepository.Remove(prodDelete);
                //_productRepository.SaveChanges();
            }
            ;
        }
        public async Task <DefaultResult> Handle(DeleteProductImageCommand command, CancellationToken cancellationToken)
        {
            var result = new DefaultResult();

            var productImage = await _productImageRepository.FindAsync(x => x.Id == command.Id);

            if (productImage == null)
            {
                Notifications.Handle("Imagem de produto não encontrado");
                return(null);
            }

            _productImageRepository.Remove(productImage);

            if (!await CommitAsync())
            {
                return(result);
            }

            return(result);
        }
 public void DeleteProductImage(int id)
 {
     _productImageRepository.Remove(id);
 }