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

Commit

Permalink
admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Anhvnqe170052 committed Dec 7, 2023
1 parent 69b2cb6 commit 0d2e876
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.bakaqc.flower.dao.ProductDAO;
import com.bakaqc.flower.model.Product;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
Expand All @@ -19,12 +22,14 @@
maxRequestSize = 1024 * 1024 * 11)
public class CreateProductController extends HttpServlet {

private final Logger LOGGER = Logger.getLogger(this.getClass().getName());
private final String UPLOAD_FILE_PATH = "/img/product_img";

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
ProductDAO productDAO = new ProductDAO();
Product product = new Product();
String name = request.getParameter("name");
String categoryID = request.getParameter("category");
Expand All @@ -37,7 +42,7 @@ protected void processRequest(HttpServletRequest request, HttpServletResponse re
product.setPrice(Integer.parseInt(price));
product.setDetail(detail);
product.setBanners(image);
productDAO.insert(product);
ProductDAO.getInstance().insert(product);
String url = request.getHeader("referer");
response.sendRedirect(url);
}
Expand All @@ -60,31 +65,38 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}

public String uploadFile(HttpServletRequest request) throws IOException, ServletException {
String fileName = "";
int length = getServletContext().getRealPath("/").length();
String uploadPath = new StringBuilder(getServletContext().getRealPath("/")) + File.separator + "img";
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
// Get the file part from the request
Part filePart = request.getPart("image");
LOGGER.info("filePart: " + filePart.toString());

// Get the filename
String fileName = getFileName(filePart);
LOGGER.info(fileName);

// Specify the directory to save the uploaded file
String savePath = "/img/product_img"; // Change this to your desired directory

// Create the save directory if it doesn't exist
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
try {
for (Part part : request.getParts()) {
String temp = getFileName(part);
if (!temp.equals(DEFAULT_FILENAME) && !temp.trim().isEmpty()) {
fileName = temp;
part.write(uploadPath + File.separator + fileName);
break;
}
}
} catch (FileNotFoundException fne) {

// Save the file to the specified directory
File file = new File(savePath, fileName);
try (InputStream input = filePart.getInputStream()) {
Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

LOGGER.info(fileName);
return fileName;
}

private String getFileName(Part part) {
for (String content : part.getHeader("content-disposition").split(";")) {
private String getFileName(final Part part) {
final String partHeader = part.getHeader("content-disposition");
for (String content : partHeader.split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(content.indexOf("=") + 2, content.length() - 1);
return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
}
}
return DEFAULT_FILENAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}

}



Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.bakaqc.flower.controller;

import com.bakaqc.flower.dao.OrderDAO;
import com.bakaqc.flower.model.Order;
import com.bakaqc.flower.model.option.OrderStatus;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateOrderController extends HttpServlet {

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

}

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

String id = request.getParameter("id");
String status = request.getParameter("status");
Order order = new Order();
order.setId(Integer.parseInt(id));
order.setStatus(OrderStatus.create(status));
OrderDAO.getInstance().update(order);

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

response.sendRedirect(url);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/bakaqc/flower/service/JDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class JDBC {

private static String DBURL = "jdbc:mysql://localhost:3306/flower_shop";
private static String DBURL = "jdbc:mysql://localhost:3333/flower_shop";
private static String USERNAME = "root";
private static String PASSWORD = "";

Expand Down
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 @@ -108,6 +108,14 @@
<servlet-name>AdminOrderController</servlet-name>
<servlet-class>com.bakaqc.flower.controller.AdminOrderController</servlet-class>
</servlet>
<servlet>
<servlet-name>UpdateOrderController</servlet-name>
<servlet-class>com.bakaqc.flower.controller.UpdateOrderController</servlet-class>
</servlet>
<servlet>
<servlet-name>UpdateUserController</servlet-name>
<servlet-class>com.bakaqc.flower.controller.UpdateAccountController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomeController</servlet-name>
<url-pattern>/home</url-pattern>
Expand Down Expand Up @@ -208,6 +216,14 @@
<servlet-name>AdminOrderController</servlet-name>
<url-pattern>/admin/list-order</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateOrderController</servlet-name>
<url-pattern>/admin/updateOrder</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UpdateUserController</servlet-name>
<url-pattern>/updateAccount</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
15
Expand Down
2 changes: 2 additions & 0 deletions src/main/webapp/style/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ pre code {
padding-left: 15px;
margin-right: auto;
margin-left: auto;
display: flex;

}

@media (min-width: 576px) {
Expand Down
Loading

0 comments on commit 0d2e876

Please sign in to comment.