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

Fix admin #10

Merged
merged 2 commits into from
Nov 28, 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
2 changes: 1 addition & 1 deletion flower_shop.sql
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,4 @@ COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.UserDAO;
import com.bakaqc.flower.model.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AdminAccountController extends HttpServlet {




@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String error = request.getParameter("error");
if(error != null){
request.setAttribute("error", error);
}
UserDAO userDAO = new UserDAO();
List<User> users = userDAO.selectAll();
request.setAttribute("users", users);
request.getSession().setAttribute("isActive", 1);
request.getRequestDispatcher("/view/AccountAdmin.jsp").forward(request, response);
}


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

}


}
16 changes: 16 additions & 0 deletions src/main/java/com/bakaqc/flower/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.bakaqc.flower.dao.OrderDAO;
import com.bakaqc.flower.dao.ProductDAO;
import com.bakaqc.flower.dao.UserDAO;
import com.bakaqc.flower.model.Order;
import com.bakaqc.flower.model.User;
import java.util.*;

public class AdminController extends HttpServlet {

Expand All @@ -11,6 +17,16 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
OrderDAO orderDAO = new OrderDAO();
List<Order> orders = orderDAO.selectAll();
UserDAO userDAO = new UserDAO();
List<User> users = userDAO.selectAll();
ProductDAO productDAO = new ProductDAO();
int productNum = productDAO.selectAll().size();
request.setAttribute("numPro", productNum);
request.setAttribute("users", users);
request.setAttribute("orders", orders);
request.getSession().setAttribute("isActive", 0);

RequestDispatcher rd = request.getRequestDispatcher("/view/admin.jsp");
rd.forward(request, response);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@


package com.bakaqc.flower.controller;

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


public class AdminProductController extends HttpServlet {





@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
CategoriesDAO categoriesDAO = new CategoriesDAO();
List<Categories> categorieses = categoriesDAO.selectAll();
ProductDAO productDAO = new ProductDAO();
List<Product> products = productDAO.selectAll();
request.setAttribute("categories", categorieses);
request.setAttribute("products", products);
request.getSession().setAttribute("isActive", 2);
request.getRequestDispatcher("/view/ProductAdmin.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
@@ -0,0 +1,65 @@
package com.bakaqc.flower.controller;

import com.bakaqc.flower.dao.UserDAO;
import com.bakaqc.flower.model.User;
import com.bakaqc.flower.model.option.UserGender;
import com.bakaqc.flower.model.option.UserStatus;
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;


public class CreateAccountController extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter()) {
String fullname = request.getParameter("fullname");
String year = request.getParameter("year");
String email = request.getParameter("email");
String gender = request.getParameter("gender");
String status = request.getParameter("status");
String address = request.getParameter("address");
String password = request.getParameter("password");
String url = request.getHeader("referer");
if (UserDAO.getInstance().selectByEmail(email) != null) {
request.setAttribute("error", "Email existed!");
request.getRequestDispatcher(url).forward(request, response);
} else {
User user = new User();
user.setPassword(password);
user.setAddress(address);
user.setEmail(email);
user.setFullName(fullname);
user.setStatus(UserStatus.create(status));
user.setGender(UserGender.create(gender));
user.setYearOfBirth(Integer.parseInt(year));
UserDAO.getInstance().insert(user);
response.sendRedirect(url);
}
}
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


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




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

import static com.bakaqc.flower.controller.UpdateProductController.DEFAULT_FILENAME;
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.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;


@MultipartConfig(fileSizeThreshold = 1024 * 1024,
maxFileSize = 1024 * 1024 * 10,
maxRequestSize = 1024 * 1024 * 11)
public class CreateProductController extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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");
String price = request.getParameter("price");
String image = uploadFile(request);
String detail = request.getParameter("detail");
product.setName(name);
product.setCategoryId(Integer.parseInt(categoryID));
product.setCategoryID(Integer.parseInt(categoryID));
product.setPrice(Integer.parseInt(price));
product.setDetail(detail);
productDAO.insert(product);
String url = request.getHeader("referer");
response.sendRedirect(url);
}
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}


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




public String uploadFile(HttpServletRequest request) throws IOException, ServletException {
String fileName = "";
int length = getServletContext().getRealPath("/").length();
String uploadPath = new StringBuilder(getServletContext().getRealPath("/")).delete(length - 10, length - 4).toString() + File.separator + "img";
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.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) {
}
return fileName;
}

private String getFileName(Part part) {
for (String content : part.getHeader("content-disposition").split(";")) {
if (content.trim().startsWith("filename")) {
return content.substring(content.indexOf("=") + 2, content.length() - 1);
}
}
return DEFAULT_FILENAME;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.bakaqc.flower.controller;

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;

/**
*
* @author vungo
*/
public class DeleteAccountController extends HttpServlet {

/**
* 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 {
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 DeleteAccountController</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet DeleteAccountController at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

// <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 {
processRequest(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 {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}
Loading