From 28479940edc2180c634150c353fe450d8fdf6ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90inh=20Qu=E1=BB=91c=20Ch=C6=B0=C6=A1ng?= Date: Wed, 6 Mar 2024 16:26:38 +0700 Subject: [PATCH 1/3] fixed bug 143 & 144 --- .../product/UpdateProductServlet.java | 21 +++++++---- .../js/management/product/add-product.js | 4 +-- .../js/management/product/update-product.js | 35 +++++++++++++++++++ .../jsp/management/product/update-product.jsp | 20 +++++------ 4 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 src/main/webapp/view/assets/js/management/product/update-product.js 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..b6b94d2 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 @@ -73,8 +73,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) Integer idP = Integer.valueOf(pId); String productName = request.getParameter("productName"); 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,12 +88,19 @@ 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); 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..237bea0 --- /dev/null +++ b/src/main/webapp/view/assets/js/management/product/update-product.js @@ -0,0 +1,35 @@ +$(document).ready(function () { + $('#update-product').on('submit', function (e) { + e.preventDefault(); + + var formData = new FormData(this); + + $.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'; + }); + + setTimeout(function () { + window.location.href = 'product'; + }, 1500); + }, + error: function (response) { + + } + }); + }); +}); \ No newline at end of file diff --git a/src/main/webapp/view/jsp/management/product/update-product.jsp b/src/main/webapp/view/jsp/management/product/update-product.jsp index 38d5c5f..bb393a3 100644 --- a/src/main/webapp/view/jsp/management/product/update-product.jsp +++ b/src/main/webapp/view/jsp/management/product/update-product.jsp @@ -1,5 +1,6 @@ <%@ page contentType="text/html" pageEncoding="UTF-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@page import="io.hardingadonis.saledock.utils.Singleton" %> @@ -39,7 +40,7 @@

Cập nhập sản phẩm

-
+
@@ -72,14 +73,10 @@
- - + + + +
@@ -121,7 +118,7 @@ Quay lại
@@ -141,9 +138,12 @@ <%@include file="../../../common/_goback.jsp" %> + + + \ No newline at end of file From 48214f593de93ae985cebc95efc6bfdf70d8c9b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90inh=20Qu=E1=BB=91c=20Ch=C6=B0=C6=A1ng?= Date: Wed, 6 Mar 2024 18:34:24 +0700 Subject: [PATCH 2/3] edit logic for update-product page --- .../management/product/UpdateProductServlet.java | 2 +- .../assets/js/management/product/update-product.js | 6 ++++-- .../view/jsp/management/customer/customer-detail.jsp | 10 +++++----- 3 files changed, 10 insertions(+), 8 deletions(-) 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 b6b94d2..eddf69e 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 @@ -104,7 +104,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) Singleton.productDAO.save(product); - response.sendRedirect("./product"); + response.sendRedirect("./product-detail?id=" + idP); } } 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 index 237bea0..202c177 100644 --- a/src/main/webapp/view/assets/js/management/product/update-product.js +++ b/src/main/webapp/view/assets/js/management/product/update-product.js @@ -3,6 +3,8 @@ $(document).ready(function () { e.preventDefault(); var formData = new FormData(this); + + var productId = formData.get('id'); $.ajax({ type: 'POST', @@ -20,11 +22,11 @@ $(document).ready(function () { if (result.dismiss === Swal.DismissReason.timer) { console.log('I was closed by the timer') } - window.location.href = 'product'; + window.location.href = 'product-detail?id=' + productId; }); setTimeout(function () { - window.location.href = 'product'; + window.location.href = 'product-detail?id=' + productId; }, 1500); }, error: function (response) { 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 @@
- - - + + + From a625894f4d20c3e15d6c9882f75f96bef309ea0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90inh=20Qu=E1=BB=91c=20Ch=C6=B0=C6=A1ng?= Date: Thu, 7 Mar 2024 11:13:22 +0700 Subject: [PATCH 3/3] fix logic for name product input --- .../controller/management/product/UpdateProductServlet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 eddf69e..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,7 +71,7 @@ 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"); String productPrice = request.getParameter("price");