public async Task <ActionResult <Product> > DeleteProduct(int id) { //TODO Delete image from folder to var product = await _context.products.FindAsync(id); if (product == null) { return(NotFound()); } string webRootPath = hostingEnvironment.WebRootPath; FileHttp.HttpDeleteFile(product.PicPath, webRootPath); _context.products.Remove(product); await _context.SaveChangesAsync(); //return product; return(NoContent()); }
public async Task <IActionResult> PutProduct(int id) { //no you gonna path it in form data // find and get the product by id var product = await _context.products.FindAsync(id); // checked if it exixt if (id != int.Parse(Request.Form["id"])) { return(NotFound()); } if (product == null) { return(NotFound()); } // old image path string picPath = product.PicPath; // check if user update image if (Request.Form.Files.Count > 0) { string webRootPath = hostingEnvironment.WebRootPath; //delete previos image FileHttp.HttpDeleteFile(product.PicPath, webRootPath); //add new image var file = Request.Form.Files[0]; picPath = FileHttp.HttpUploadFile(file, webRootPath); // Edit image path picPath = "Upload/" + picPath; } product.Name = Request.Form["name"]; product.Price = int.Parse(Request.Form["price"]); product.PicPath = picPath; _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(product.Id)) { return(NotFound()); } else { throw; } } return(NoContent()); }