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

Commit

Permalink
Merge pull request #140 from hardingadonis/dev
Browse files Browse the repository at this point in the history
Sale Dock - Release v0.7.0
  • Loading branch information
hardingadonis authored Mar 3, 2024
2 parents 806fa7f + 91038e2 commit ae0a33f
Show file tree
Hide file tree
Showing 38 changed files with 1,502 additions and 200 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ server/*.war
# IntelliJ IDEA
.idea/

.history/
.history/

*.xlsx
2 changes: 1 addition & 1 deletion get_version.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

major_version=0
minor_version=6
minor_version=7
path_version=0

echo "$major_version.$minor_version.$path_version"
17 changes: 14 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.hardingadonis</groupId>
<artifactId>saledock</artifactId>
<version>0.6.0</version>
<version>0.7.0</version>
<packaging>war</packaging>

<name>Sale Dock - v${project.version}</name>
Expand All @@ -16,7 +17,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jakartaee>10.0.0</jakartaee>
</properties>

<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
Expand Down Expand Up @@ -85,6 +86,16 @@
<artifactId>commons-fileupload</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

package io.hardingadonis.saledock.controller.error;

import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.*;
import java.io.*;

@WebServlet(name = "ErrorPageServlet", urlPatterns = {"/error-404"})
public class ErrorPageServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getRequestDispatcher("/view/jsp/error/404.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,30 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

List<Customer> customers = Singleton.customerDAO.getAll();
Integer customerCount = Singleton.customerDAO.count();
request.setAttribute("customerCount", customerCount);
int pageNumber = 1;
int pageSize = 10;

// Get the requested page number
String pageStr = request.getParameter("page");
if (pageStr != null) {
pageNumber = Integer.parseInt(pageStr);
}

List<Customer> customers = Singleton.customerDAO.pagination((pageNumber - 1) * pageSize, pageSize);

int totalPages = Singleton.customerDAO.totalPages(pageSize);

request.setAttribute("pageSize", pageSize);

request.setAttribute("customerCount", Singleton.customerDAO.count());
request.setAttribute("customers", customers);
request.setAttribute("currentPage", pageNumber);
request.setAttribute("totalPages", totalPages);
request.setAttribute("pageSize", pageSize);
request.setAttribute("page", "customer");


request.getRequestDispatcher("/view/jsp/management/customer/customer.jsp").forward(request, response);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,70 @@
package io.hardingadonis.saledock.controller.management.customer;

import io.hardingadonis.saledock.model.Customer;
import io.hardingadonis.saledock.utils.Singleton;
import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.*;

import java.io.*;
import java.util.Optional;

@WebServlet(name = "UpdateCustomerServlet", urlPatterns = {"/update-customer"})
public class UpdateCustomerServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

request.setAttribute("page", "customer");

String id = request.getParameter("id");
if (id == null) {
response.sendError(404);
return;
}

Integer id_customer = Integer.valueOf(id);
Optional<Customer> customer = Singleton.customerDAO.getByID(id_customer);

if (customer.isPresent()) {
var cus = customer.get();

request.setAttribute("cus", cus);
request.getRequestDispatcher("/view/jsp/management/customer/update-customer.jsp").forward(request, response);
} else {
response.sendRedirect(request.getContextPath() + "/customer");
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

System.out.println("hhhh");
String name = request.getParameter("nameCus");
String address = request.getParameter("addressCus");

String id = request.getParameter("id");
if (id == null) {
response.sendError(404);
return;
}


Integer id_customer = Integer.valueOf(id);
Customer customer = Singleton.customerDAO.getByID(id_customer).get();
if (!name.isEmpty()) {
customer.setName(name);
}
if (!address.isEmpty()) {
customer.setAddress(address);
}
Singleton.customerDAO.save(customer);
response.sendRedirect(request.getContextPath() + "/customer-detail?id="+id);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.hardingadonis.saledock.controller.management.order;

import com.mysql.cj.util.StringUtils;
import io.hardingadonis.saledock.model.*;
import io.hardingadonis.saledock.utils.*;
import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.*;
import java.io.*;
import java.util.*;

@WebServlet(name = "AddOrderServlet", urlPatterns = {"/add-order"})
public class AddOrderServlet extends HttpServlet {
Expand All @@ -16,50 +18,152 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

List<Customer> customers = Singleton.customerDAO.getAll();
List<Product> products = Singleton.productDAO.getAll();

String action = request.getParameter("action");
String customerIdParam = request.getParameter("customerId");

if (customerIdParam != null && !customerIdParam.isEmpty()) {
try {
Integer customerId = Integer.parseInt(customerIdParam);
request.setAttribute("customerId", customerId);
} catch (NumberFormatException e) {
e.printStackTrace();
response.sendRedirect("./error-404");
return;
}
}

if (action != null) {
switch (action) {
case "delete":
deleteProductFromOrder(request, response);
break;
case "goBack":
SessionUtil.getInstance().removeValue(request, "productMap");
response.sendRedirect("./order");
return;
default:
response.sendRedirect("./add-order");
}
}

Map<Integer, Integer> productMap = getProductMap(request);

double totalCost = calculateTotalCost(productMap, Singleton.productDAO.getAll());

request.setAttribute("customers", customers);
request.setAttribute("products", products);
request.setAttribute("productMap", productMap);
request.setAttribute("totalCost", totalCost);

request.setAttribute("page", "order");

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/view/jsp/management/order/add-order.jsp");
requestDispatcher.forward(request, response);
request.getRequestDispatcher("/view/jsp/management/order/add-order.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String customerID = request.getParameter("customerID");
String productID = request.getParameter("productID");
String quantity = request.getParameter("quantity");

if (customerID == null || productID == null || quantity == null) {
response.sendError(404, "Please provide Customer ID, Product ID and Quantity");
String customerIdParam = request.getParameter("customerId");
if (customerIdParam == null || customerIdParam.isEmpty()) {
response.sendRedirect("./add-order?message=customerNotExist");
return;
}
try {
Integer customerId = Integer.parseInt(customerIdParam);
String note = request.getParameter("note");
Employee employee = (Employee) SessionUtil.getInstance().getValue(request, "employee");

Optional<Customer> customer = Singleton.customerDAO.getByID(customerId);

if (customer.isPresent()) {
Order order = new Order();

Integer id_customer = Integer.valueOf(customerID);
Integer id_product = Integer.valueOf(productID);
order.setCustomer(customer.get());
order.setEmployee(employee);
order.setNote(note);

Customer customer = Singleton.customerDAO.getByID(id_customer).orElse(null);
Product product = Singleton.productDAO.getByID(id_product).orElse(null);
HttpSession session = request.getSession();
Employee employee = (Employee) session.getAttribute("employee");
Map<Integer, Integer> productMap = getProductMap(request);

// Check if an employee is logged in
if (employee == null) {
response.sendRedirect(request.getContextPath() + "/login?message=notLoggedIn");
if (productMap.isEmpty()) {
response.sendRedirect("./add-order?customerId=" + customerId + "&message=emptyProduct");
return;
} else {
for (Integer productId : productMap.keySet()) {
Optional<Product> product = Singleton.productDAO.getByID(productId);

if (product.isPresent()) {
order.addProduct(product.get(), productMap.get(productId));
} else {
response.sendRedirect("./add-order?customerId=" + customerId + "&message=productNotExist");
return;
}
}
}
Order saveOrder = Singleton.orderDAO.save(order);
SendEmailUtil.sendOrderMessage(customer.get().getEmail(), "Slae Dock - Đặt hàng thành công", saveOrder);
SessionUtil.getInstance().removeValue(request, "productMap");
response.sendRedirect("./order?message=orderSuccess");
} else {
response.sendRedirect("./add-order?message=customerNotExist");
}
} catch (Exception e) {
e.printStackTrace();
response.sendRedirect("./error-404");
return;
}

if (customer == null || product == null) {
response.sendError(404, "Please provide correct Customer ID and Product ID");
return;
}

private void deleteProductFromOrder(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String productIdParam = request.getParameter("productId");

if (productIdParam != null) {
try {
int productId = Integer.parseInt(productIdParam);

Map<Integer, Integer> productMap = getProductMap(request);

productMap.remove(productId);

double totalCost = calculateTotalCost(productMap, Singleton.productDAO.getAll());
request.setAttribute("totalCost", totalCost);

SessionUtil.getInstance().putValue(request, "productMap", productMap);

} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}

Order order = new Order();
order.setCustomer(customer);
order.setEmployee(employee);
private Map<Integer, Integer> getProductMap(HttpServletRequest request) {
Map<Integer, Integer> productMap = (Map<Integer, Integer>) SessionUtil.getInstance().getValue(request, "productMap");
if (productMap == null) {
productMap = new HashMap<>();
SessionUtil.getInstance().putValue(request, "productMap", productMap);
}
return productMap;
}

order.addProduct(product, Integer.valueOf(quantity));
private double calculateTotalCost(Map<Integer, Integer> productMap, List<Product> allProducts) {
double totalCost = 0;

for (Map.Entry<Integer, Integer> entry : productMap.entrySet()) {
int productId = entry.getKey();
int quantity = entry.getValue();

for (Product product : allProducts) {
if (product.getID() == productId) {
totalCost += product.getPrice() * quantity;
break;
}
}
}

Singleton.orderDAO.save(order);
response.sendRedirect(request.getContextPath() + "/order");
return totalCost;
}
}
Loading

0 comments on commit ae0a33f

Please sign in to comment.