test
Форк от lirfrnk/test
1package servlets;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5
6import javax.servlet.RequestDispatcher;
7import javax.servlet.ServletException;
8import javax.servlet.http.HttpServlet;
9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11
12import com.bittercode.constant.ResponseCode;
13import com.bittercode.model.UserRole;
14import com.bittercode.service.BookService;
15import com.bittercode.service.impl.BookServiceImpl;
16import com.bittercode.util.StoreUtil;
17
18public class RemoveBookServlet extends HttpServlet {
19
20BookService bookService = new BookServiceImpl();
21
22public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
23PrintWriter pw = res.getWriter();
24res.setContentType("text/html");
25if (!StoreUtil.isLoggedIn(UserRole.SELLER, req.getSession())) {
26RequestDispatcher rd = req.getRequestDispatcher("SellerLogin.html");
27rd.include(req, res);
28pw.println("<table class=\"tab\"><tr><td>Please Login First to Continue!!</td></tr></table>");
29return;
30}
31
32try {
33String bookId = req.getParameter("bookId");
34RequestDispatcher rd = req.getRequestDispatcher("SellerHome.html");
35rd.include(req, res);
36StoreUtil.setActiveTab(pw, "removebook");
37pw.println("<div class='container'>");
38if (bookId == null || bookId.isBlank()) {
39// render the remove book form;
40showRemoveBookForm(pw);
41return;
42} // else continue
43
44String responseCode = bookService.deleteBookById(bookId.trim());
45if (ResponseCode.SUCCESS.name().equalsIgnoreCase(responseCode)) {
46pw.println("<table class=\"tab my-5\"><tr><td>Book Removed Successfully</td></tr></table>");
47pw.println(
48"<table class=\"tab\"><tr><td><a href=\"removebook\">Remove more Books</a></td></tr></table>");
49
50} else {
51pw.println("<table class=\"tab my-5\"><tr><td>Book Not Available In The Store</td></tr></table>");
52pw.println(
53"<table class=\"tab\"><tr><td><a href=\"removebook\">Remove more Books</a></td></tr></table>");
54}
55pw.println("</div>");
56} catch (Exception e) {
57e.printStackTrace();
58pw.println("<table class=\"tab\"><tr><td>Failed to Remove Books! Try Again</td></tr></table>");
59}
60}
61
62private static void showRemoveBookForm(PrintWriter pw) {
63String form = "<form action=\"removebook\" method=\"post\" class='my-5'>\r\n"
64+ " <table class=\"tab\">\r\n"
65+ " <tr>\r\n"
66+ " <td>\r\n"
67+ " <label for=\"bookCode\">Enter BookId to Remove </label>\r\n"
68+ " <input type=\"text\" name=\"bookId\" placeholder=\"Enter Book Id\" id=\"bookCode\" required>\r\n"
69+ " <input class=\"btn btn-danger my-2\" type=\"submit\" value=\"Remove Book\">\r\n"
70+ " </td>\r\n"
71+ " </tr>\r\n"
72+ "\r\n"
73+ " </table>\r\n"
74+ " </form>";
75pw.println(form);
76}
77
78}
79