Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Merge branch 'dev' into sơn-error-when-add-customer-with-field-null
Browse files Browse the repository at this point in the history
  • Loading branch information
hardingadonis authored Mar 10, 2024
2 parents 7596231 + 5e106aa commit 6e3c6fd
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,20 +21,37 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)

List<Order> 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<Order> orderPaging = Singleton.orderDAO.pagination(offset, LIMIT);

List<Order> orderPaging = Singleton.orderDAO.pagination(offset, LIMIT);
int total = Singleton.orderDAO.totalPages(LIMIT);

request.setAttribute("numOfOrder", count);
request.setAttribute("currentPage", pageNum);
request.setAttribute("totalPage", total);
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
// }

}





}
41 changes: 25 additions & 16 deletions src/main/webapp/view/jsp/management/customer/add-customer.jsp
Original file line number Diff line number Diff line change
@@ -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");
%>
<!DOCTYPE html>
<html data-bs-theme="light" lang="en">

Expand Down Expand Up @@ -44,7 +48,7 @@
<strong>Tên khách hàng</strong>
</label>
<div class="mb-3 form-group">
<input class="form-control" type="text" id="name-customer" placeholder="Nhập tên khách hàng" name="name" >
<input class="form-control" type="text" id="name-customer" placeholder="Nhập tên khách hàng" name="name" value="<%= (error != null && customer != null) ? customer.getName() : ""%>">
<span class="form-message text-danger"></span>
</div>
</div>
Expand All @@ -60,26 +64,31 @@
</div>
</div>
</div>

</div>
<div class="mb-3">
<div class="mb-3">
<label class="form-label" for="address">
<strong>Địa chỉ</strong>
</label>
<div class="mb-3 form-group">
<input class="form-control" type="text" id="address-customer" placeholder="Nhập địa chỉ của khách hàng" name="address" >
<span class="form-message text-danger"></span>
<div class="mb-3">
<label class="form-label" for="address">
<strong>Địa chỉ</strong>
</label>
<input class="form-control" type="text" id="address" placeholder="Nhập địa chỉ của khách hàng" name="address" value="<%= (error != null && customer != null) ? customer.getAddress() : ""%>">
</div>
</div>
<div class="mb-3">
<label class="form-label" for="email">
<strong>Email</strong>
</label>
<div class="mb-3 form-group">
<input class="form-control" type="email" id="email-customer" placeholder="Nhập email khách hàng" name="email" >
<span class="form-message text-danger"></span>
<div class="mb-3">
<label class="form-label" for="email">
<strong>Email</strong>
</label>
<input class="form-control" type="email" id="email" placeholder="Nhập email khách hàng" name="email">
</div>
<%
if(error != null) {
%>
<span class="form-message text-danger"><%=error%></span>
<%
}
%>
</div>
<a class="btn btn-primary btn-sm" href="customer">Quay lại</a>
<button class="btn btn-primary btn-sm" type="submit">Lưu lại</button>
</div>
</form>
Expand Down
22 changes: 20 additions & 2 deletions src/main/webapp/view/jsp/management/order/order.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
<table class="table my-0" id="dataTable">
<thead>
<tr>
<th width = '30%'>Mã đặt hàng</th>
<th width = '25%'>Mã đặt hàng</th>
<th width = '30%'>Tên khách hàng</th>
<th width = '30%'>Tổng tiền</th>
<th width = '20%'>Tổng tiền</th>
<th width = '15%'>Trạng thái</th>
<th width = '10%'>Thao tác</th>
</tr>
</thead>
Expand All @@ -52,6 +53,23 @@
<td><img class="rounded-circle me-2" width="40" height="40" src="<%=request.getContextPath()%>/view/assets/images/icons/order.png">${order.code}</td>
<td>${order.customer.name}</td>
<td>${order.getTotalToString()} ₫</td>
<td style="color:
<c:choose>
<c:when test="${order.status eq 'PENDING'}">gray</c:when>
<c:when test="${order.status eq 'SHIPPING'}">orange</c:when>
<c:when test="${order.status eq 'DONE'}">green</c:when>
<c:when test="${order.status eq 'CANCELLED'}">red</c:when>
<c:otherwise>black</c:otherwise>
</c:choose>;
">
<c:choose>
<c:when test="${order.status eq 'PENDING'}">Đang xử lý</c:when>
<c:when test="${order.status eq 'SHIPPING'}">Đang giao</c:when>
<c:when test="${order.status eq 'DONE'}">Đã xong</c:when>
<c:when test="${order.status eq 'CANCELLED'}">Đã huỷ</c:when>
<c:otherwise>${order.status}</c:otherwise>
</c:choose>
</td>
<td class="text-start">
<a class="btn btn-primary btn-sm" role="button" data-bs-toggle="tooltip" data-bss-tooltip="" style="margin: 2px;" title="Thông tin chi tiết" href="<%=request.getContextPath()%>/order-detail?id=${order.ID}">
<i class="la la-info-circle"></i>
Expand Down
5 changes: 4 additions & 1 deletion src/main/webapp/view/jsp/management/order/update-order.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@
</div>
<div class="mb-3">
<div class="mb-3">
<label class="form-label" for="country"><strong>Ghi chú</strong></label><textarea class="form-control" readonly=""></textarea>
<label class="form-label" for="country">
<strong>Ghi chú</strong>
</label>
<textarea class="form-control" id="order-note" name="note" placeholder="${requestScope.ord.note}"></textarea>
</div>

<button class="btn btn-primary btn-sm" type="button">
Expand Down

0 comments on commit 6e3c6fd

Please sign in to comment.