public async Task <IActionResult> Delete([FromForm] ProductDeleteVM vm)
        {
            if (ModelState.IsValid)
            {
                var deleteResult = await _productService.DeleteProduct(vm.Id);

                if (deleteResult)
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }

            return(RedirectToAction("details", "product", new { slug = vm.Slug }));
        }
        public IActionResult Delete([FromBody] ProductDeleteVM model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var prod = _context.Products.SingleOrDefault(p => p.Id == model.Id);

            if (prod != null)
            {
                _context.Products.Remove(prod);
                _context.SaveChanges();
            }
            return(BadRequest());
        }
示例#3
0
        public IActionResult Delete([FromBody] ProductDeleteVM model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var fullProduct = _context.Products.SingleOrDefault(p => p.Id == model.Id);

            if (fullProduct != null)
            {
                //видаляємо фото(якщо не за замовчуванням)
                if (fullProduct.PhotoName != kNamePhotoDefault && fullProduct.PhotoName != null)
                {
                    string imageNamePath = Path.Combine(dirPathSave, fullProduct.PhotoName);
                    System.IO.File.Delete(imageNamePath);
                }
                //видаляємо продукт
                _context.Products.Remove(fullProduct);
                _context.SaveChanges();
            }

            return(Ok(fullProduct.Id));
        }