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

Update paging order #178

Merged
merged 1 commit into from
Mar 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import jakarta.servlet.http.*;
import java.io.*;
import java.util.*;
import java.util.stream.*;

@WebServlet(name = "OrderServlet", urlPatterns = {"/order"})
public class OrderServlet extends HttpServlet {

final static int LIMIT = 10;

@Override
Expand All @@ -22,20 +21,37 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)

List<Order> orders = Singleton.orderDAO.getAll();
int count = Singleton.orderDAO.count();

int pageNum = getInteger(request.getParameter("page"));

String pageNumParam = request.getParameter("page");
int pageNum = 0;

if (pageNumParam == null || pageNumParam.isEmpty()) {
pageNum = 1;
} else {
try {
pageNum = Integer.parseInt(pageNumParam);
} catch (NumberFormatException e) {
response.sendRedirect("./error-404");
return;
}
}

if (pageNum < 0) {
response.sendRedirect("./error-404");
return;
}

int offset = pageNum;

if (offset == -1){
offset = 0;

if (offset == 0) {
pageNum = 1;
} else {
offset = (offset - 1)*LIMIT;
offset = (offset - 1) * LIMIT;
}
List<Order> orderPaging = Singleton.orderDAO.pagination(offset, LIMIT);

List<Order> orderPaging = Singleton.orderDAO.pagination(offset, LIMIT);
int total = Singleton.orderDAO.totalPages(LIMIT);

request.setAttribute("numOfOrder", count);
request.setAttribute("currentPage", pageNum);
request.setAttribute("totalPage", total);
Expand All @@ -50,15 +66,4 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}

private static int getInteger(String parameter){
if (parameter == null) {
return -1;
}
try {
return Integer.parseInt(parameter);
} catch (NumberFormatException e) {
return -1;
}
}
}
Loading