Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update voucher for admin and staff #84

Merged
merged 6 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions init/full.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ create table Food (
food_status bit not null,
food_rate tinyint not null,
discount_percent tinyint not null,
food_img_url varchar(255) null,
food_img_url varchar(255) not null,
food_type_id tinyint not null foreign key references FoodType(food_type_id)
);

Expand Down Expand Up @@ -52,7 +52,11 @@ go
create table Voucher (
voucher_id tinyint identity(1,1) not null primary key,
voucher_name nvarchar(200) not null,
voucher_discount_percent tinyint not null
voucher_code char(16) not null,
voucher_discount_percent tinyint not null,
voucher_quantity tinyint not null,
voucher_status bit not null,
voucher_date datetime not null
);

go
Expand Down Expand Up @@ -378,12 +382,10 @@ insert into OrderStatus (order_status) values (N'Đã giao');
insert into OrderStatus (order_status) values (N'Đã hủy');

-- Voucher
insert into Voucher (voucher_name , voucher_discount_percent) values ( N'Quốc tế phụ nữ', 30);
insert into Voucher (voucher_name , voucher_discount_percent) values ( N'Khách hàng may mắn', 20);
insert into Voucher (voucher_name , voucher_discount_percent) values (N'Quà tặng Noel', 40);
insert into Voucher (voucher_name , voucher_discount_percent) values ( N'Người đặc biệt', 50);


insert into Voucher (voucher_name, voucher_code, voucher_discount_percent, voucher_quantity, voucher_status, voucher_date) values ( N'Quốc tế phụ nữ', 'ADASD2FD23123DBE', 30, 100, 0,'20231019 00:00:01 AM' );
insert into Voucher (voucher_name, voucher_code, voucher_discount_percent, voucher_quantity, voucher_status, voucher_date) values ( N'Khách hàng may mắn', 'ADEF38BDYOQM875V', 20, 10, 0,'20230808 00:00:01 AM');
insert into Voucher (voucher_name, voucher_code, voucher_discount_percent, voucher_quantity, voucher_status, voucher_date) values (N'Quà tặng Noel', 'DUEMAHWOPUNH62GH', 40, 50, 1,'20231215 00:00:01 AM' );
insert into Voucher (voucher_name, voucher_code, voucher_discount_percent, voucher_quantity, voucher_status, voucher_date) values ( N'Người đặc biệt', 'DJWOA975N4B92BH6', 50, 5, 1,'20231108 00:00:01 AM' );

-- Cart, CartItem, Order test data
insert into Cart (customer_id) values (1);
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/Controllers/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
Expand Down Expand Up @@ -196,6 +199,7 @@ private void doPostUpdateUser(HttpServletRequest request, HttpServletResponse re

int result = accountDAO.update(account);


if (result == 1) {
response.sendRedirect("/admin");
return;
Expand All @@ -209,10 +213,16 @@ private void doPostAddVoucher(HttpServletRequest request, HttpServletResponse re
throws ServletException, IOException {

String voucherName = (String) request.getParameter("txtvoucher_name");
byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtAddVoucher_discount_percent"));

String voucherCode = (String) request.getParameter("txtvoucher_code");
Byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent"));
Byte voucher_quantity = Byte.parseByte(request.getParameter("txtvoucher_quantity"));
Byte voucher_status = Byte.parseByte(request.getParameter("txtvoucher_status"));
String datetimelocal = request.getParameter("txtvoucher_time");

Timestamp datetime = Timestamp.valueOf(datetimelocal.replace("T"," ")+":00");

VoucherDAO voucherDAO = new VoucherDAO();
Voucher voucher = new Voucher(voucherName, voucher_discount_percent);
Voucher voucher = new Voucher(voucherName, voucherCode, voucher_discount_percent, voucher_quantity, voucher_status, datetime);

int result = voucherDAO.add(voucher);

Expand All @@ -227,12 +237,19 @@ private void doPostAddVoucher(HttpServletRequest request, HttpServletResponse re

private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
byte voucherID = Byte.parseByte(request.getParameter("txtvoucher_id"));
Byte voucherID = Byte.parseByte(request.getParameter("txtvoucher_id"));
String voucherName = (String) request.getParameter("txtvoucher_name");
byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent"));

String voucherCode = (String) request.getParameter("txtvoucher_code");
Byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent"));
Byte voucher_quantity = Byte.parseByte(request.getParameter("txtvoucher_quantity"));
Byte voucher_status = Byte.parseByte(request.getParameter("txtvoucher_status"));

String datetimelocal = request.getParameter("txtvoucher_time");

Timestamp datetime = Timestamp.valueOf(datetimelocal.replace("T"," ")+":00");

VoucherDAO voucherDAO = new VoucherDAO();
Voucher voucher = new Voucher(voucherName, voucher_discount_percent);
Voucher voucher = new Voucher(voucherName, voucherCode, voucher_discount_percent, voucher_quantity, voucher_status, datetime);
voucher.setVoucherID(voucherID);

int result = voucherDAO.update(voucher);
Expand Down
74 changes: 48 additions & 26 deletions src/main/java/Controllers/PromotionManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* 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 Controllers;

import DAOs.AccountDAO;
Expand All @@ -19,34 +18,38 @@
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

public class PromotionManagerController extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
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 PromotionManagerController</title>");
out.println("<title>Servlet PromotionManagerController</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet PromotionManagerController at " + request.getContextPath () + "</h1>");
out.println("<h1>Servlet PromotionManagerController at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
}

private void doGetVoucher(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getRequestURI();
Expand All @@ -59,14 +62,22 @@ private void doGetVoucher(HttpServletRequest request, HttpServletResponse respon
}
response.sendRedirect("/promotionManager");
}

private void doPostAddVoucher(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String voucherName = (String) request.getParameter("txtvoucher_name");
byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtAddVoucher_discount_percent"));
String voucherCode = (String) request.getParameter("txtvoucher_code");
Byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent"));
Byte voucher_quantity = Byte.parseByte(request.getParameter("txtvoucher_quantity"));
Byte voucher_status = Byte.parseByte(request.getParameter("txtvoucher_status"));
// Lấy thời gian hiện tại
LocalDateTime currentTime = LocalDateTime.now();
// Chuyển đổi thời gian hiện tại thành Timestamp
Timestamp datetime = Timestamp.valueOf(currentTime);

VoucherDAO voucherDAO = new VoucherDAO();
Voucher voucher = new Voucher(voucherName, voucher_discount_percent);
Voucher voucher = new Voucher(voucherName, voucherCode, voucher_discount_percent, voucher_quantity, voucher_status, datetime);

int result = voucherDAO.add(voucher);

Expand All @@ -78,15 +89,23 @@ private void doPostAddVoucher(HttpServletRequest request, HttpServletResponse re
return;
}
}
private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse response)

private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
byte voucherID = Byte.parseByte(request.getParameter("txtvoucher_id"));
byte voucherID = Byte.parseByte(request.getParameter("txtvoucher_id"));
String voucherName = (String) request.getParameter("txtvoucher_name");
byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent"));
String voucherCode = (String) request.getParameter("txtvoucher_code");
Byte voucher_discount_percent = Byte.parseByte(request.getParameter("txtvoucher_discount_percent"));
Byte voucher_quantity = Byte.parseByte(request.getParameter("txtvoucher_quantity"));
Byte voucher_status = Byte.parseByte(request.getParameter("txtvoucher_status"));

// Lấy thời gian hiện tại
LocalDateTime currentTime = LocalDateTime.now();
// Chuyển đổi thời gian hiện tại thành Timestamp
Timestamp datetime = Timestamp.valueOf(currentTime);

VoucherDAO voucherDAO = new VoucherDAO();
Voucher voucher = new Voucher(voucherName, voucher_discount_percent);
Voucher voucher = new Voucher(voucherName, voucherCode, voucher_discount_percent, voucher_quantity, voucher_status, datetime);
voucher.setVoucherID(voucherID);

int result = voucherDAO.update(voucher);
Expand All @@ -101,19 +120,20 @@ private void doPostUpdateVoucher(HttpServletRequest request, HttpServletResponse
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws ServletException, IOException {
String path = request.getRequestURI();
if (path.endsWith("/promotionManager")) {

VoucherDAO voucherDAO = new VoucherDAO();
List<Voucher> voucherList = voucherDAO.getAllList();

Expand All @@ -127,20 +147,21 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
// response.setContentType("text/css");
request.getRequestDispatcher("/promotionManager.jsp").forward(request, response);
}
}
}

/**
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
throws ServletException, IOException {
if (request.getParameter("btnSubmit") != null) {
switch (request.getParameter("btnSubmit")) {
switch (request.getParameter("btnSubmit")) {
case "SubmitAddVoucher":
doPostAddVoucher(request, response);
break;
Expand All @@ -153,8 +174,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}
}

/**
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
Expand Down
33 changes: 23 additions & 10 deletions src/main/java/DAOs/VoucherDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Voucher getVoucher(byte id) {
ps.setByte(1, id);
rs = ps.executeQuery();
if (rs.next()) {
voucher = new Voucher(rs.getByte("voucher_id"), rs.getString("voucher_name"), rs.getByte("voucher_discount_percent"));
voucher = new Voucher(rs.getByte("voucher_id"), rs.getString("voucher_name"), rs.getString("voucher_code"),rs.getByte("voucher_discount_percent"),rs.getByte("voucher_quantity"), rs.getByte("voucher_status"), rs.getTimestamp("voucher_date"));
}
} catch (SQLException ex) {
Logger.getLogger(VoucherDAO.class.getName()).log(Level.SEVERE, null, ex);
Expand All @@ -58,8 +58,13 @@ public List<Voucher> getAllList() {
while (voucherRS.next()) {
Voucher voucher = new Voucher(
voucherRS.getByte("voucher_id"),
voucherRS.getString("voucher_name"),
voucherRS.getByte("voucher_discount_percent"));
voucherRS.getString("voucher_name"),
voucherRS.getString("voucher_code"),
voucherRS.getByte("voucher_discount_percent"),
voucherRS.getByte("voucher_quantity"),
voucherRS.getByte("voucher_status"),
voucherRS.getTimestamp("voucher_date")
);
voucherList.add(voucher);
}
} catch (SQLException ex) {
Expand All @@ -69,12 +74,16 @@ public List<Voucher> getAllList() {
}

public int add(Voucher voucher) {
String sql = "insert into Voucher values (?,?)";
String sql = "insert into Voucher values (?,?,?,?,?,?)";
int result = 0;
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, voucher.getName());
ps.setInt(2, voucher.getVoucher_discount_percent());
ps.setString(1, voucher.getVoucher_name());
ps.setString(2, voucher.getVoucher_code());
ps.setByte(3, voucher.getVoucher_discount_percent());
ps.setByte(4, voucher.getVoucher_quantity());
ps.setByte(5, voucher.getVoucher_status());
ps.setTimestamp(6, voucher.getVoucher_date());
result = ps.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(VoucherDAO.class.getName()).log(Level.SEVERE, null, ex);
Expand All @@ -96,13 +105,17 @@ public int delete(byte id) {
}

public int update(Voucher voucher) {
String sql = "update Voucher set voucher_name = ?, voucher_discount_percent = ? where voucher_id = ?";
String sql = "update Voucher set voucher_name = ?, voucher_code = ?,voucher_discount_percent = ?, voucher_quantity = ?, voucher_status = ?, voucher_date = ? where voucher_id = ?";
int result = 0;
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, voucher.getName());
ps.setByte(2, voucher.getVoucher_discount_percent());
ps.setByte(3, voucher.getVoucherID());
ps.setString(1, voucher.getVoucher_name());
ps.setString(2, voucher.getVoucher_code());
ps.setByte(3, voucher.getVoucher_discount_percent());
ps.setByte(4, voucher.getVoucher_quantity());
ps.setByte(5, voucher.getVoucher_status());
ps.setTimestamp(6, voucher.getVoucher_date());
ps.setByte(7, voucher.getVoucherID());
result = ps.executeUpdate();
} catch (SQLException ex) {
Logger.getLogger(VoucherDAO.class.getName()).log(Level.SEVERE, null, ex);
Expand Down
Loading