diff --git a/src/main/java/io/hardingadonis/saledock/controller/management/customer/AddCustomerServlet.java b/src/main/java/io/hardingadonis/saledock/controller/management/customer/AddCustomerServlet.java index ec1def9..609db90 100644 --- a/src/main/java/io/hardingadonis/saledock/controller/management/customer/AddCustomerServlet.java +++ b/src/main/java/io/hardingadonis/saledock/controller/management/customer/AddCustomerServlet.java @@ -6,6 +6,7 @@ import jakarta.servlet.*; import jakarta.servlet.annotation.*; import jakarta.servlet.http.*; +import java.sql.SQLException; @WebServlet(name = "AddCustomerServlet", urlPatterns = {"/add-customer"}) public class AddCustomerServlet extends HttpServlet { @@ -30,18 +31,35 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) String address = request.getParameter("address"); String email = request.getParameter("email"); - if (name.length() > 0 && code.length() > 0 && address.length() > 0 && email.length() > 0) { + try { + if (name.length() > 0 && code.length() > 0 && address.length() > 0 && email.length() > 0) { + Customer customer = new Customer(); + customer.setName(name); + customer.setCode(code); + customer.setAddress(address); + customer.setEmail(email); + Singleton.customerDAO.save(customer); + + response.sendRedirect(request.getContextPath() + "/customer"); + return; + } + } catch (Exception e) { Customer customer = new Customer(); customer.setName(name); - customer.setCode(code); customer.setAddress(address); - customer.setEmail(email); - Singleton.customerDAO.save(customer); + StringBuilder message = new StringBuilder("Tạo khách hàng không thành công: "); + if (e.getMessage().contains(code)) { + message.append("Không được trùng mã khách hàng."); + } + if (e.getMessage().contains(email)) { + message.append("Không được trùng email."); + } + request.setAttribute("error", message.toString()); + request.setAttribute("appendCustomer", customer); + RequestDispatcher requestDispatcher = request.getRequestDispatcher("/view/jsp/management/customer/add-customer.jsp"); + requestDispatcher.forward(request, response); - response.sendRedirect(request.getContextPath() + "/customer"); - return; } - response.sendRedirect(request.getContextPath() + "/add-customer"); } } diff --git a/src/main/java/io/hardingadonis/saledock/controller/management/customer/UpdateCustomerServlet.java b/src/main/java/io/hardingadonis/saledock/controller/management/customer/UpdateCustomerServlet.java index e1b4e9c..5a6ebef 100644 --- a/src/main/java/io/hardingadonis/saledock/controller/management/customer/UpdateCustomerServlet.java +++ b/src/main/java/io/hardingadonis/saledock/controller/management/customer/UpdateCustomerServlet.java @@ -58,11 +58,11 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) Integer id_customer = Integer.valueOf(id); Customer customer = Singleton.customerDAO.getByID(id_customer).get(); - if (!name.isEmpty()) { - customer.setName(name); + if (!name.trim().isEmpty()) { + customer.setName(name.trim()); } - if (!address.isEmpty()) { - customer.setAddress(address); + if (!address.trim().isEmpty()) { + customer.setAddress(address.trim()); } Singleton.customerDAO.save(customer); response.sendRedirect(request.getContextPath() + "/customer-detail?id="+id); diff --git a/src/main/java/io/hardingadonis/saledock/controller/management/order/OrderServlet.java b/src/main/java/io/hardingadonis/saledock/controller/management/order/OrderServlet.java index 30d45de..c5440ba 100644 --- a/src/main/java/io/hardingadonis/saledock/controller/management/order/OrderServlet.java +++ b/src/main/java/io/hardingadonis/saledock/controller/management/order/OrderServlet.java @@ -7,11 +7,10 @@ import jakarta.servlet.http.*; import java.io.*; import java.util.*; -import java.util.stream.*; @WebServlet(name = "OrderServlet", urlPatterns = {"/order"}) public class OrderServlet extends HttpServlet { - + final static int LIMIT = 10; @Override @@ -22,20 +21,37 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) List orders = Singleton.orderDAO.getAll(); int count = Singleton.orderDAO.count(); - - int pageNum = getInteger(request.getParameter("page")); + + String pageNumParam = request.getParameter("page"); + int pageNum = 0; + + if (pageNumParam == null || pageNumParam.isEmpty()) { + pageNum = 1; + } else { + try { + pageNum = Integer.parseInt(pageNumParam); + } catch (NumberFormatException e) { + response.sendRedirect("./error-404"); + return; + } + } + + if (pageNum < 0) { + response.sendRedirect("./error-404"); + return; + } + int offset = pageNum; - - if (offset == -1){ - offset = 0; + + if (offset == 0) { pageNum = 1; } else { - offset = (offset - 1)*LIMIT; + offset = (offset - 1) * LIMIT; } - - List orderPaging = Singleton.orderDAO.pagination(offset, LIMIT); + + List orderPaging = Singleton.orderDAO.pagination(offset, LIMIT); int total = Singleton.orderDAO.totalPages(LIMIT); - + request.setAttribute("numOfOrder", count); request.setAttribute("currentPage", pageNum); request.setAttribute("totalPage", total); @@ -50,15 +66,4 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } - - private static int getInteger(String parameter){ - if (parameter == null) { - return -1; - } - try { - return Integer.parseInt(parameter); - } catch (NumberFormatException e) { - return -1; - } - } } diff --git a/src/main/java/io/hardingadonis/saledock/controller/management/order/UpdateOrderServlet.java b/src/main/java/io/hardingadonis/saledock/controller/management/order/UpdateOrderServlet.java index 893ff6f..e4b1521 100644 --- a/src/main/java/io/hardingadonis/saledock/controller/management/order/UpdateOrderServlet.java +++ b/src/main/java/io/hardingadonis/saledock/controller/management/order/UpdateOrderServlet.java @@ -10,6 +10,7 @@ @WebServlet(name = "UpdateOrderServlet", urlPatterns = {"/update-order"}) public class UpdateOrderServlet extends HttpServlet { + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -43,26 +44,27 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) String ID = request.getParameter("id"); Order order = Singleton.orderDAO.getByID(Integer.valueOf(ID)).orElse(null); - + String status = request.getParameter("status"); - - - if (status != null && !status.equals(order.getStatus()) ) { - - order.setStatus(Order.Status.valueOf(status)); - Singleton.orderDAO.save(order); + String note = request.getParameter("note"); - response.sendRedirect("./order"); - return; - } - else { - response.sendRedirect("./order"); - return; + if (status != null && !status.equals(order.getStatus())) { + + order.setStatus(Order.Status.valueOf(status)); + + } + if(!note.trim().isEmpty()){ + order.setNote(note.trim()); } + Singleton.orderDAO.save(order); + + response.sendRedirect(request.getContextPath() + "/order-detail?id=" + ID); +// else { +// response.sendRedirect(request.getContextPath() + "/order-detail?id=" + ID); +// return; +// } + } - - - - + } diff --git a/src/main/webapp/view/jsp/management/customer/add-customer.jsp b/src/main/webapp/view/jsp/management/customer/add-customer.jsp index a3439ab..fa1c6be 100644 --- a/src/main/webapp/view/jsp/management/customer/add-customer.jsp +++ b/src/main/webapp/view/jsp/management/customer/add-customer.jsp @@ -1,7 +1,11 @@ <%@ page contentType="text/html" pageEncoding="UTF-8" %> <%@page import="io.hardingadonis.saledock.utils.Singleton" %> - +<%@page import="io.hardingadonis.saledock.model.Customer" %> +<% + String error = (String)request.getAttribute("error"); + Customer customer = (Customer)request.getAttribute("appendCustomer"); +%> @@ -44,7 +48,7 @@ Tên khách hàng
- + ">
@@ -60,26 +64,31 @@ +
- -
- - +
+ + ">
-
-
- -
- - +
+ +
+ <% + if(error != null) { + %> + <%=error%> + <% + } + %>
+ Quay lại
diff --git a/src/main/webapp/view/jsp/management/order/order.jsp b/src/main/webapp/view/jsp/management/order/order.jsp index 868c46f..305dec1 100644 --- a/src/main/webapp/view/jsp/management/order/order.jsp +++ b/src/main/webapp/view/jsp/management/order/order.jsp @@ -40,9 +40,10 @@ - + - + + @@ -52,6 +53,23 @@ +
Mã đặt hàngMã đặt hàng Tên khách hàngTổng tiềnTổng tiềnTrạng thái Thao tác
${order.code} ${order.customer.name} ${order.getTotalToString()} ₫gray + orange + green + red + black + ; + "> + + Đang xử lý + Đang giao + Đã xong + Đã huỷ + ${order.status} + + diff --git a/src/main/webapp/view/jsp/management/order/update-order.jsp b/src/main/webapp/view/jsp/management/order/update-order.jsp index d937ac6..c9c5384 100644 --- a/src/main/webapp/view/jsp/management/order/update-order.jsp +++ b/src/main/webapp/view/jsp/management/order/update-order.jsp @@ -169,7 +169,10 @@
- + +