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 #87 from hardingadonis/hotfix_admin
Browse files Browse the repository at this point in the history
Create admin page
  • Loading branch information
yuhtnguyen authored Dec 8, 2023
2 parents fdd6afd + 7061378 commit e5d74a8
Show file tree
Hide file tree
Showing 37 changed files with 3,875 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.model.Product;
import io.hardingadonis.miu.services.Singleton;
import java.io.*;
import java.time.LocalDateTime;
import java.util.Collections;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(name = "AddNewProduct", urlPatterns = {"/admin/new-product"})
public class AddNewProduct 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/add-new-product.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");

String nameProduct = request.getParameter("productName");

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

int Category_ID = Integer.parseInt(request.getParameter("productCategoryID"));

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

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

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

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

long productPrice = Long.parseLong(request.getParameter("productPrice"));

int productAmount = Integer.parseInt(request.getParameter("productAmount"));

Product product = new Product();

product.setName(nameProduct);
product.setBrand(productBrand);
product.setCategoryID(Category_ID);

product.setOrigin(productOrigin);
product.setExpiryDate(productExpiry);
product.setWeight(productWeight);
product.setPreservation(productPreservation);
product.setPrice(productPrice);
product.setAmount(productAmount);
product.setThumbnail("");
product.setImages(Collections.EMPTY_LIST);

Singleton.productDAO.insert(product);
response.sendRedirect(request.getContextPath()+ "/admin/product");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.model.*;
import io.hardingadonis.miu.services.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import org.json.simple.*;

@WebServlet(name = "CategoryAdmin", urlPatterns = {"/admin/category"})
public class CategoryAdmin 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();

Admin admin = (Admin) session.getAttribute("admin");

if (admin == null) {
response.sendRedirect(request.getContextPath() + "/admin/login");
return;
}
request.getRequestDispatcher("/view/admin/category-admin.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");

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

try {
int id = Integer.parseInt(request.getParameter("id"));

Category category = Singleton.categoryDAO.get(id);

if (category != null) {
category.setName(name);

Singleton.categoryDAO.update(category);
}
} catch (NumberFormatException ex) {
Category category = new Category(name);

Singleton.categoryDAO.insert(category);
}

JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", "success");
jsonResponse.put("message", "Order canceled successfully");

response.setContentType("application/json");
response.getWriter().write(jsonResponse.toString());

response.setStatus(HttpServletResponse.SC_OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.model.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet(name = "DashboardAdmin", urlPatterns = {"/admin"})
public class DashboardAdmin 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();

Admin admin = (Admin) session.getAttribute("admin");

if (admin == null) {
response.sendRedirect(request.getContextPath() + "/admin/login");
return;
}

request.getRequestDispatcher("/view/admin/dashboard-admin.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
@@ -1,11 +1,10 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.model.Admin;
import io.hardingadonis.miu.services.Hash;
import io.hardingadonis.miu.services.Singleton;
import io.hardingadonis.miu.model.*;
import io.hardingadonis.miu.services.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

@WebServlet(name = "LoginAdmin", urlPatterns = {"/admin/login"})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.hardingadonis.miu.controller.admin;

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

@WebServlet(name = "LogoutAdmin", urlPatterns = {"/admin/logout"})
public class LogoutAdmin 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(false);

if (session != null) {
session.invalidate();
}

response.sendRedirect(request.getContextPath() + "/admin");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.model.*;
import io.hardingadonis.miu.model.detail.OrderStatus;
import io.hardingadonis.miu.services.Singleton;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import org.json.simple.JSONObject;

@WebServlet(name = "OrderAdmin", urlPatterns = {"/admin/order"})
public class OrderAdmin 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();

Admin admin = (Admin) session.getAttribute("admin");

if (admin == null) {
response.sendRedirect(request.getContextPath() + "/admin/login");
return;
}

request.getRequestDispatcher("/view/admin/order-admin.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");

OrderStatus orderStatus = OrderStatus.create(request.getParameter("status"));

try {
int id = Integer.parseInt(request.getParameter("id"));

Order order = Singleton.orderDAO.get(id);

if (order != null) {
order.setStatus(orderStatus);

Singleton.orderDAO.update(order);
}
} catch (NumberFormatException ex) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

return;
}

JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", "success");
jsonResponse.put("message", "Status updated successfully");

response.setContentType("application/json");
response.getWriter().write(jsonResponse.toString());

response.setStatus(HttpServletResponse.SC_OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.model.*;
import io.hardingadonis.miu.services.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import org.json.simple.*;

@WebServlet(name = "ProductAdmin", urlPatterns = {"/admin/product"})
public class ProductAdmin 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();

Admin admin = (Admin) session.getAttribute("admin");

if (admin == null) {
response.sendRedirect(request.getContextPath() + "/admin/login");
return;
}

request.getRequestDispatcher("/view/admin/product-admin.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Handle form submission here
}

@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
int ID = Integer.parseInt(request.getParameter("id"));

Singleton.productDAO.delete(ID);

JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", "success");
jsonResponse.put("message", "User canceled successfully");

response.setContentType("application/json");
response.getWriter().write(jsonResponse.toString());

response.setStatus(HttpServletResponse.SC_OK);

} catch (NumberFormatException ex) {
System.err.println(ex.getMessage());
}
}
}
53 changes: 53 additions & 0 deletions src/main/java/io/hardingadonis/miu/controller/admin/UserAdmin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.hardingadonis.miu.controller.admin;

import io.hardingadonis.miu.model.*;
import io.hardingadonis.miu.services.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import org.json.simple.*;

@WebServlet(name = "UserAdmin", urlPatterns = {"/admin/user"})
public class UserAdmin 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();

Admin admin = (Admin) session.getAttribute("admin");

if (admin == null) {
response.sendRedirect(request.getContextPath() + "/admin/login");
return;
}

request.getRequestDispatcher("/view/admin/user-admin.jsp").forward(request, response);
}

@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
int ID = Integer.parseInt(request.getParameter("id"));

Singleton.userDAO.delete(ID);

JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", "success");
jsonResponse.put("message", "User canceled successfully");

response.setContentType("application/json");
response.getWriter().write(jsonResponse.toString());

response.setStatus(HttpServletResponse.SC_OK);

} catch (NumberFormatException ex) {
System.err.println(ex.getMessage());
}
}
}
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);

}
Loading

0 comments on commit e5d74a8

Please sign in to comment.