public IActionResult UpdateProduct([FromBody] ProductRequest productRequest, int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (!productRequest.Foto.Any()) { return(BadRequest("At least 1 image required.")); } var product = _context.Product.FirstOrDefault(x => x.Id == id); product = ProductMapping.UpdateProductMapping(productRequest, product); _context.Product.Update(product); var removedFotoPath = new List <string> (); foreach (var foto in productRequest.Foto) { if (foto.Id == 0) { _context.FotoUpload.Add(new FotoUpload(foto, id)); } else { var existingFoto = _context.FotoUpload.FirstOrDefault(x => x.Id == foto.Id); if (foto.FotoPath == null) { removedFotoPath.Add(existingFoto.FotoPath); _context.FotoUpload.Remove(existingFoto); } } } foreach (var deletedFoto in removedFotoPath) { var webRootPath = _hostingEnvironment.WebRootPath; var request = HttpContext.Request; var uriBuilder = new UriBuilder { Host = request.Host.Host, Scheme = request.Scheme }; if (request.Host.Port.HasValue) { uriBuilder.Port = request.Host.Port.Value; } var urlPath = uriBuilder.ToString(); var path = deletedFoto.Replace(urlPath, webRootPath + @"\\").Replace("/", @"\\"); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } _context.Commit(); if (!productRequest.Attachment.Any()) { return(Ok(new { msg = "Post Updated" })); } var removedAttachmentPath = new List <string> (); foreach (var attachment in productRequest.Attachment) { if (attachment.Id == 0) { _context.AttachmentFile.Add(new AttachmentFile(attachment, id)); } else { var existingAttachment = _context.AttachmentFile.FirstOrDefault(x => x.Id == attachment.Id); if (attachment.FilePath == null) { removedAttachmentPath.Add(existingAttachment.FilePath); _context.AttachmentFile.Remove(existingAttachment); } } } foreach (var deletedAttachment in removedAttachmentPath) { var webRootPath = _hostingEnvironment.WebRootPath; var request = HttpContext.Request; var uriBuilder = new UriBuilder { Host = request.Host.Host, Scheme = request.Scheme }; if (request.Host.Port.HasValue) { uriBuilder.Port = request.Host.Port.Value; } var urlPath = uriBuilder.ToString(); var path = deletedAttachment.Replace(urlPath, webRootPath + @"\\").Replace("/", @"\\"); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } _context.Commit(); if (!productRequest.ProductCertificate.Any()) { return(Ok(new { msg = "Post Updated" })); } var removedCertificatePath = new List <string> (); foreach (var certificate in productRequest.ProductCertificate) { if (certificate.Id == 0) { _context.FotoUpload.Add(new FotoUpload(certificate, id)); } else { var existingCertificate = _context.FotoUpload.FirstOrDefault(x => x.Id == certificate.Id); if (certificate.FotoPath == null) { removedCertificatePath.Add(existingCertificate.FotoPath); _context.FotoUpload.Remove(existingCertificate); } } } foreach (var deletedCertificate in removedCertificatePath) { var webRootPath = _hostingEnvironment.WebRootPath; var request = HttpContext.Request; var uriBuilder = new UriBuilder { Host = request.Host.Host, Scheme = request.Scheme }; if (request.Host.Port.HasValue) { uriBuilder.Port = request.Host.Port.Value; } var urlPath = uriBuilder.ToString(); var path = deletedCertificate.Replace(urlPath, webRootPath + @"\\").Replace("/", @"\\"); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } _context.Commit(); if (!productRequest.ProductClient.Any()) { return(Ok(new { msg = "Post Updated" })); } var removedClient = new List <string> (); foreach (var client in productRequest.ProductClient) { if (client.Id == 0) { _context.FotoUpload.Add(new FotoUpload(client, id)); } else { var existingClient = _context.FotoUpload.FirstOrDefault(x => x.Id == client.Id); if (client.FotoPath == null) { removedClient.Add(existingClient.FotoPath); _context.FotoUpload.Remove(existingClient); } } } foreach (var deletedClient in removedClient) { var webRootPath = _hostingEnvironment.WebRootPath; var request = HttpContext.Request; var uriBuilder = new UriBuilder { Host = request.Host.Host, Scheme = request.Scheme }; if (request.Host.Port.HasValue) { uriBuilder.Port = request.Host.Port.Value; } var urlPath = uriBuilder.ToString(); var path = deletedClient.Replace(urlPath, webRootPath + @"\\").Replace("/", @"\\"); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } _context.Commit(); if (!productRequest.ProductImplementation.Any()) { return(Ok(new { msg = "Post Updated" })); } var removedProductImplementation = new List <string> (); foreach (var implementation in productRequest.ProductImplementation) { if (implementation.Id == 0) { _context.FotoUpload.Add(new FotoUpload(implementation, id)); } else { var existingImplementation = _context.FotoUpload.FirstOrDefault(x => x.Id == implementation.Id); if (implementation.FotoPath == null) { removedProductImplementation.Add(existingImplementation.FotoPath); _context.FotoUpload.Remove(existingImplementation); } } } foreach (var deletedProductImplementation in removedProductImplementation) { var webRootPath = _hostingEnvironment.WebRootPath; var request = HttpContext.Request; var uriBuilder = new UriBuilder { Host = request.Host.Host, Scheme = request.Scheme }; if (request.Host.Port.HasValue) { uriBuilder.Port = request.Host.Port.Value; } var urlPath = uriBuilder.ToString(); var path = deletedProductImplementation.Replace(urlPath, webRootPath + @"\\").Replace("/", @"\\"); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } _context.Commit(); return(Ok(new { msg = "Post Updated" })); }