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

Commit

Permalink
Merge pull request #79 from hardingadonis/thuy_create_admin
Browse files Browse the repository at this point in the history
Thuy create admin(hotfixx-admin)
  • Loading branch information
yuhtnguyen authored Dec 7, 2023
2 parents 3466747 + 20c3c95 commit 4e60610
Show file tree
Hide file tree
Showing 23 changed files with 1,145 additions and 160 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.dao.impl.mysql.CategoryDAOMySQLImpl;
import io.hardingadonis.miu.model.Category;
import io.hardingadonis.miu.services.Singleton;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(name = "CategoryManagement", urlPatterns = {"/categorymanagement"})
@MultipartConfig
public class CategoryManagement 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.getRequestDispatcher("/view/admin/category-management.jsp").forward(request, response);

}

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

}

}
112 changes: 82 additions & 30 deletions src/main/java/io/hardingadonis/miu/controller/web/CheckoutServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import io.hardingadonis.miu.model.*;
import io.hardingadonis.miu.model.detail.*;
import io.hardingadonis.miu.services.*;
import io.hardingadonis.miu.services.vnpay.*;
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
Expand Down Expand Up @@ -39,21 +44,24 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");

User user = (User) request.getSession().getAttribute("user");
HttpSession session = request.getSession();

User user = (User) session.getAttribute("user");

String cartCookie = getCartCookie(request);
long totalPrice = getTotalPrice(cartCookie);
String address = request.getParameter("address");
Payment payment = Payment.create(request.getParameter("payment"));

if (payment == Payment.COD) {
Order order = new Order(user.getID(), address, totalPrice, payment, OrderStatus.PROCESSING);
Order order = new Order(user.getID(), address, totalPrice, payment, OrderStatus.PROCESSING);

int id = Singleton.orderDAO.insert(order);
session.setAttribute("order", order);
session.setAttribute("cart_cookie", cartCookie);

moveCartToOrderData(id, cartCookie, response);

response.sendRedirect("purchase-history");
if (payment == Payment.COD) {
response.sendRedirect("checkout-success");
} else {
handleVNPayCheckout(request, response, (int) totalPrice);
}
}

Expand Down Expand Up @@ -81,38 +89,82 @@ private static long getTotalPrice(String cartData) {
Product product = Singleton.productDAO.get(key);
total += product.getPrice() * value;
}
} catch (ParseException ex) {
} catch (org.json.simple.parser.ParseException ex) {
System.err.println(ex.getMessage());
}

return total;
}

private static void moveCartToOrderData(int id, String cartData, HttpServletResponse response) {
try {
JSONObject data = (JSONObject) new JSONParser().parse(cartData);

for (Object keyStr : data.keySet()) {
Object valueStr = data.get(keyStr);
int value = Integer.parseInt(valueStr.toString());
int key = Integer.parseInt(keyStr.toString());

Singleton.orderDataDAO.insert(new OrderData(id, key, value));
updateProductAmoutAferCheckout(key, value);
private static void handleVNPayCheckout(HttpServletRequest request, HttpServletResponse response, int totalPrice)
throws UnsupportedEncodingException, IOException {
String vnp_Version = "2.1.0";
String vnp_Command = "pay";
String vnp_OrderInfo = "Miu Shop - Thanh toán đơn hàng";
String orderType = "other";
String vnp_TxnRef = VNPayConfig.getRandomNumber(8);
String vnp_IpAddr = VNPayConfig.getIpAddress(request);
String vnp_TmnCode = VNPayConfig.vnp_TmnCode;

Map vnp_Params = new HashMap<>();
vnp_Params.put("vnp_Version", vnp_Version);
vnp_Params.put("vnp_Command", vnp_Command);
vnp_Params.put("vnp_TmnCode", vnp_TmnCode);
vnp_Params.put("vnp_Amount", String.valueOf(totalPrice * 100));
vnp_Params.put("vnp_CurrCode", "VND");
vnp_Params.put("vnp_BankCode", "");
vnp_Params.put("vnp_TxnRef", vnp_TxnRef);
vnp_Params.put("vnp_OrderInfo", vnp_OrderInfo);
vnp_Params.put("vnp_OrderType", orderType);
vnp_Params.put("vnp_Locale", "vn");
vnp_Params.put("vnp_ReturnUrl", getDomainWithPortAndContextPath(request) + VNPayConfig.vnp_ReturnUrl);
vnp_Params.put("vnp_IpAddr", vnp_IpAddr);

Calendar cld = Calendar.getInstance(TimeZone.getTimeZone("Etc/GMT+7"));
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String vnp_CreateDate = formatter.format(cld.getTime());

vnp_Params.put("vnp_CreateDate", vnp_CreateDate);
cld.add(Calendar.MINUTE, 15);
String vnp_ExpireDate = formatter.format(cld.getTime());
vnp_Params.put("vnp_ExpireDate", vnp_ExpireDate);

List fieldNames = new ArrayList(vnp_Params.keySet());
Collections.sort(fieldNames);
StringBuilder hashData = new StringBuilder();
StringBuilder query = new StringBuilder();
Iterator itr = fieldNames.iterator();

while (itr.hasNext()) {
String fieldName = (String) itr.next();
String fieldValue = (String) vnp_Params.get(fieldName);

if ((fieldValue != null) && (fieldValue.length() > 0)) {
hashData.append(fieldName);
hashData.append('=');
hashData.append(URLEncoder.encode(fieldValue, StandardCharsets.US_ASCII.toString()));

query.append(URLEncoder.encode(fieldName, StandardCharsets.US_ASCII.toString()));
query.append('=');
query.append(URLEncoder.encode(fieldValue, StandardCharsets.US_ASCII.toString()));

if (itr.hasNext()) {
query.append('&');
hashData.append('&');
}
}

Cookie cookie = new Cookie("cart", "");
cookie.setMaxAge(0);
response.addCookie(cookie);
} catch (ParseException ex) {
System.err.println(ex.getMessage());
}
}

private static void updateProductAmoutAferCheckout(int productID, int amount) {
Product product = Singleton.productDAO.get(productID);
product.setAmount(product.getAmount() - amount);
String queryUrl = query.toString();
String vnp_SecureHash = VNPayConfig.hmacSHA512(VNPayConfig.vnp_HashSecret, hashData.toString());
queryUrl += "&vnp_SecureHash=" + vnp_SecureHash;
String paymentUrl = VNPayConfig.vnp_PayUrl + "?" + queryUrl;

response.sendRedirect(paymentUrl);
}

Singleton.productDAO.update(product);
private static String getDomainWithPortAndContextPath(HttpServletRequest request)
throws MalformedURLException {
return "http://" + new URL(request.getRequestURL().toString()).getHost() + ":" + request.getServerPort() + "/miu/";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.hardingadonis.miu.controller.web;

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

@WebServlet(name = "CheckoutSuccessServlet", urlPatterns = {"/checkout-success"})
public class CheckoutSuccessServlet extends HttpServlet {

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

HttpSession session = request.getSession();

if ((session.getAttribute("user") == null) || (session.getAttribute("order") == null) || (session.getAttribute("cart_cookie") == null)) {
response.sendRedirect("home");
}

request.getRequestDispatcher("/view/web/checkout-success.jsp").forward(request, response);
}
}
17 changes: 0 additions & 17 deletions src/main/java/io/hardingadonis/miu/dao/CartDAO.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/io/hardingadonis/miu/dao/CategoryDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public interface CategoryDAO {
public void delete(int ID);

public int count();

public String getNameCategory(int ID);

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public Category get(int ID) {
return category;
}

public String getNameCategory(int ID) {
try {
Connection conn = Singleton.dbContext.getConnection();

PreparedStatement smt = conn.prepareStatement("SELECT name FROM category WHERE id = ? AND delete_at IS NULL");
smt.setInt(1, ID);

ResultSet rs = smt.executeQuery();

if (rs.next()) {
return rs.getString("name");
}

Singleton.dbContext.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
return null;
}

@Override
public void insert(Category obj) {
try {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/io/hardingadonis/miu/services/Singleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class Singleton {

public static AdminDAO adminDAO;

public static CartDAO cartDAO;

public static CategoryDAO categoryDAO;

public static OrderDAO orderDAO;
Expand All @@ -32,8 +30,6 @@ public class Singleton {

adminDAO = new AdminDAOMySQLImpl();

cartDAO = new CartDAOMySQLImpl();

categoryDAO = new CategoryDAOMySQLImpl();

orderDAO = new OrderDAOMySQLImpl();
Expand Down
Loading

0 comments on commit 4e60610

Please sign in to comment.