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

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
dwttlduong committed Dec 7, 2023
1 parent bf8785c commit ec2aca0
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 10 deletions.
43 changes: 43 additions & 0 deletions src/main/java/com/bakaqc/flower/controller/CheckoutController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
*/
package com.bakaqc.flower.controller;

import com.bakaqc.flower.dao.CheckoutDAO;
import com.bakaqc.flower.dao.ProductDAO;
import com.bakaqc.flower.model.Product;
import com.bakaqc.flower.model.User;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
*
* @author Admin
*/
public class CheckoutController extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
CheckoutDAO d = new CheckoutDAO();
HttpSession session = request.getSession();
String pid = request.getParameter("pid");
Product p = ProductDAO.getInstance().getById(pid);
User user = (User) session.getAttribute("user");
d.addOder(user, p);


session.setAttribute("successMessage", "Bạn đã đặt hàng thành công. Cảm ơn và chúc một ngày tốt lành!");

response.sendRedirect("home");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,21 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)

// String success = "Chỉnh sửa thông tin thành công.";
// session.setAttribute("success", success);
String flag = (String) session.getAttribute("flag");//dương thêm

response.sendRedirect("profile");
if (flag != null) {//dương thêm
// Điều hướng đến payment.jsp

String pid = (String) request.getParameter("pid");//dương thêm
// String quantity = request.getParameter("quantity");//dương thêm

response.sendRedirect(request.getContextPath() + "/pay?proId=" + pid);//dương thêm
} else {//dương thêm
response.sendRedirect("profile");
}//dương thêm

// Xóa giá trị của biến từ session bên payment sau khi sử dụng
session.removeAttribute("flag");//dương thêm
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/bakaqc/flower/controller/PaymentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.bakaqc.flower.controller;

import com.bakaqc.flower.dao.ProductDAO;
import com.bakaqc.flower.model.Product;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PaymentController extends HttpServlet {

// protected void processRequest(HttpServletRequest request, HttpServletResponse response)
// throws ServletException, IOException {
// response.setContentType("text/html;charset=UTF-8");
// try (PrintWriter out = response.getWriter()) {
// /* TODO output your page here. You may use following sample code. */
// out.println("<!DOCTYPE html>");
// out.println("<html>");
// out.println("<head>");
// out.println("<title>Servlet PaymentController</title>");
// out.println("</head>");
// out.println("<body>");
// out.println("<h1>Servlet PaymentController at " + request.getContextPath() + "</h1>");
// out.println("</body>");
// out.println("</html>");
// }
// }
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");


String proId = request.getParameter("proId");

Product p = ProductDAO.getInstance().getById(proId);
int total = p.getPrice();
request.setAttribute("total", total);
request.setAttribute("pid", proId);

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

}

// @Override
// protected void doPost(HttpServletRequest request, HttpServletResponse response)
// throws ServletException, IOException {
//
// }
}
50 changes: 50 additions & 0 deletions src/main/java/com/bakaqc/flower/dao/CheckoutDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.bakaqc.flower.dao;

import com.bakaqc.flower.model.Product;
import com.bakaqc.flower.model.User;
import com.bakaqc.flower.service.Convert;
import com.bakaqc.flower.service.JDBC;
import java.sql.*;
import java.time.LocalDateTime;

public class CheckoutDAO {

public void addOder(User user, Product p) {

try {
Connection conn = JDBC.getConnection();



//add oder
PreparedStatement smt1 = conn.prepareStatement("INSERT INTO `order` (`user_id`, `total_price`, `payment`, `status`, `create_at`) VALUES (?, ?, ?, ?, ?)");
smt1.setInt(1, user.getId());
smt1.setInt(2, p.getPrice());
smt1.setString(3, "COD");
smt1.setString(4, "processing");
smt1.setString(5, Convert.convert(LocalDateTime.now()));
smt1.executeUpdate();

// take oder_id
PreparedStatement smt4 = conn.prepareStatement("SELECT id FROM `order` ORDER BY id DESC LIMIT 1");
ResultSet rs = smt4.executeQuery();
rs.next();
int orderId = rs.getInt("id");

//add data_oder
PreparedStatement smt3 = conn.prepareStatement("INSERT INTO `data_order` (`order_id`, `product_id`, `amount`) VALUES (?, ?, ?)");

smt3.setInt(1, orderId);
smt3.setInt(2, p.getId());
smt3.setInt(3, 1);

smt3.executeUpdate();

JDBC.closeConnection(conn);
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}

}

}
16 changes: 16 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<servlet-name>ViewAllController</servlet-name>
<servlet-class>com.bakaqc.flower.controller.ViewAllController</servlet-class>
</servlet>
<servlet>
<servlet-name>PaymentController</servlet-name>
<servlet-class>com.bakaqc.flower.controller.PaymentController</servlet-class>
</servlet>
<servlet>
<servlet-name>ProductDetail</servlet-name>
<servlet-class>com.bakaqc.flower.controller.ProductDetailController</servlet-class>
Expand All @@ -43,6 +47,10 @@
<servlet>
<servlet-name>AdminLoginController</servlet-name>
<servlet-class>com.bakaqc.flower.controller.AdminLoginController</servlet-class>
</servlet>
<servlet>
<servlet-name>CheckoutController</servlet-name>
<servlet-class>com.bakaqc.flower.controller.CheckoutController</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminRegisterController</servlet-name>
Expand Down Expand Up @@ -164,6 +172,10 @@
<servlet-name>AdminAccountController</servlet-name>
<url-pattern>/admin/list-account</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CheckoutController</servlet-name>
<url-pattern>/checkout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AdminProductController</servlet-name>
<url-pattern>/admin/list-product</url-pattern>
Expand All @@ -176,6 +188,10 @@
<servlet-name>DeleteAccountController</servlet-name>
<url-pattern>/admin/deleteUser</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PaymentController</servlet-name>
<url-pattern>/pay</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CreateAccountController</servlet-name>
<url-pattern>/admin/createAccout</url-pattern>
Expand Down
94 changes: 94 additions & 0 deletions src/main/webapp/style/payment.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
body {
background-image: white;
}

.component {
position: relative;
width: 30%;
margin: 5rem auto;
padding: 1rem;

box-shadow: 2px 2px 10px #454545;
background-color: antiquewhite;

text-align: center;
}

.credit-card h1 {
color: rgba(4,99,128,1);
}

.component .credit-card form {
display:flex;
flex-direction: column;
}

.component .credit-card .line {
display: flex;
}

.component .credit-card .line input {
width: 20%; /* on définit une taille plus petite qu'il le faut, le flex-grow fera le reste */
flex: 1;
margin: 0.4rem 0.3rem;
}

input {
border: none;
border-bottom: 1px solid rgba(0,0,0,.12);
margin: 1rem 0;
padding: 4px;
font-size: 1rem;
outline: none;
}

input::-webkit-input-placeholder {
color: #AAAAAA;
}
.checkout-buttons {
display: flex;
justify-content: space-between; /* Các nút sẽ được căn giữa */
}
.valid-button {
border: 0;
padding: 1rem 2rem;
background-color: rgba(4,99,128,0.9);
color: #EFECCA;
font-weight:bold;
margin-top:2rem;
box-shadow: 1px 1px 1px black;
text-decoration: none;
}

.valid-button:hover {
background-color: rgba(4,99,128,1);
box-shadow: none;
text-decoration: none;
}

.total {
position: absolute;
top: 3em;
left: -8em;
z-index: -1;
background: #002F2F;
color: #A7A37E;
width: auto; /* Thay đổi từ 100px thành auto */
transform: rotate(-35deg);
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
padding-right: 2rem;
box-shadow: 1px 1px 5px black;
overflow: hidden; /* Giữ cho nội dung không tràn ra khỏi total */
}

.total p {
font-size: 1.5rem;
margin: 0; /* Loại bỏ margin mặc định của các trình duyệt */
white-space: nowrap; /* Ngăn chữ xuống dòng */
}
.valid-button no-underline {
text-decoration: none;
}
10 changes: 10 additions & 0 deletions src/main/webapp/view/home.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@
<header class="position-relative text-center text-white mb-5 banner">
<img src="./img/banner_home.png" alt="Banner" class="w-100" height="350">
</header>
<%
String successMessage = (String) session.getAttribute("successMessage");
session.removeAttribute("successMessage");
%>


<% if (successMessage != null && !successMessage.isEmpty()) {%>
<h1 style="color: #29E6DF"><%= successMessage%></h1>
<% }%>
<h1>Danh sách sản phẩm</h1>

<h3 class="not-found">${notFound}</h3>
Expand Down
Loading

0 comments on commit ec2aca0

Please sign in to comment.