diff --git a/src/main/java/io/hardingadonis/saledock/controller/management/product/UpdateProductServlet.java b/src/main/java/io/hardingadonis/saledock/controller/management/product/UpdateProductServlet.java index 6d5978e..cf954ea 100644 --- a/src/main/java/io/hardingadonis/saledock/controller/management/product/UpdateProductServlet.java +++ b/src/main/java/io/hardingadonis/saledock/controller/management/product/UpdateProductServlet.java @@ -71,10 +71,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) return; } Integer idP = Integer.valueOf(pId); - String productName = request.getParameter("productName"); + String productName = request.getParameter("productName").trim(); String productDescription = request.getParameter("productDescription"); - double price = Double.parseDouble(request.getParameter("price")); - + String productPrice = request.getParameter("price"); + Part part = request.getPart("imageUpload"); String imgURL = null; if (part.getSize() > 0) { @@ -88,16 +88,23 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) part.write(realPath + "/" + fileName); } - Product product = Singleton.productDAO.getByID(idP).get(); - product.setPrice(price); - product.setName(productName); - product.setImageURL(imgURL); - product.setDescription(productDescription); + if (!productName.isEmpty()) { + product.setName(productName); + } + if (!productPrice.isEmpty()) { + product.setPrice(Double.parseDouble(productPrice)); + } + if (imgURL != null) { + product.setImageURL(imgURL); + } + if (!productDescription.isEmpty()) { + product.setDescription(productDescription); + } Singleton.productDAO.save(product); - response.sendRedirect("./product"); + response.sendRedirect("./product-detail?id=" + idP); } } diff --git a/src/main/webapp/view/assets/js/management/product/add-product.js b/src/main/webapp/view/assets/js/management/product/add-product.js index 96106b7..4f8cb4e 100644 --- a/src/main/webapp/view/assets/js/management/product/add-product.js +++ b/src/main/webapp/view/assets/js/management/product/add-product.js @@ -15,7 +15,7 @@ $(document).ready(function () { title: 'Thành công!', text: 'Thêm sản phẩm thành công', icon: 'success', - timer: 3000 + timer: 1500 }).then((result) => { if (result.dismiss === Swal.DismissReason.timer) { console.log('I was closed by the timer') @@ -25,7 +25,7 @@ $(document).ready(function () { setTimeout(function () { window.location.href = 'product'; - }, 3000); + }, 1500); }, error: function (response) { diff --git a/src/main/webapp/view/assets/js/management/product/update-product.js b/src/main/webapp/view/assets/js/management/product/update-product.js new file mode 100644 index 0000000..202c177 --- /dev/null +++ b/src/main/webapp/view/assets/js/management/product/update-product.js @@ -0,0 +1,37 @@ +$(document).ready(function () { + $('#update-product').on('submit', function (e) { + e.preventDefault(); + + var formData = new FormData(this); + + var productId = formData.get('id'); + + $.ajax({ + type: 'POST', + url: $(this).attr('action'), + data: formData, + processData: false, + contentType: false, + success: function (response) { + Swal.fire({ + title: 'Thành công!', + text: 'Cập nhật sản phẩm thành công', + icon: 'success', + timer: 1500 + }).then((result) => { + if (result.dismiss === Swal.DismissReason.timer) { + console.log('I was closed by the timer') + } + window.location.href = 'product-detail?id=' + productId; + }); + + setTimeout(function () { + window.location.href = 'product-detail?id=' + productId; + }, 1500); + }, + error: function (response) { + + } + }); + }); +}); \ No newline at end of file diff --git a/src/main/webapp/view/jsp/management/customer/customer-detail.jsp b/src/main/webapp/view/jsp/management/customer/customer-detail.jsp index 3b0615b..c0b9acf 100644 --- a/src/main/webapp/view/jsp/management/customer/customer-detail.jsp +++ b/src/main/webapp/view/jsp/management/customer/customer-detail.jsp @@ -62,11 +62,11 @@
Cập nhập sản phẩm