| [1eb7a55] | 1 | package controller;
|
|---|
| 2 |
|
|---|
| 3 | import service.ProfessorSubjectService;
|
|---|
| 4 | import service.impl.ProfessorSubjectServiceImpl;
|
|---|
| 5 |
|
|---|
| 6 | import javax.servlet.ServletException;
|
|---|
| 7 | import javax.servlet.annotation.WebServlet;
|
|---|
| 8 | import javax.servlet.http.HttpServlet;
|
|---|
| 9 | import javax.servlet.http.HttpServletRequest;
|
|---|
| 10 | import javax.servlet.http.HttpServletResponse;
|
|---|
| 11 | import java.io.IOException;
|
|---|
| 12 | import java.sql.SQLException;
|
|---|
| 13 |
|
|---|
| 14 | @WebServlet("/professor-subject/*")
|
|---|
| 15 | public class ProfessorSubjectServlet extends HttpServlet {
|
|---|
| 16 |
|
|---|
| 17 | private final ProfessorSubjectService service = new ProfessorSubjectServiceImpl();
|
|---|
| 18 |
|
|---|
| 19 | @Override
|
|---|
| 20 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|---|
| 21 | String action = request.getPathInfo();
|
|---|
| 22 |
|
|---|
| 23 | try {
|
|---|
| 24 | if ("/assign".equals(action)) {
|
|---|
| 25 | assign(request, response);
|
|---|
| 26 | } else if ("/remove".equals(action)) {
|
|---|
| 27 | remove(request, response);
|
|---|
| 28 | }
|
|---|
| 29 | } catch (SQLException e) {
|
|---|
| 30 | throw new ServletException(e);
|
|---|
| 31 | }
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | private void assign(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
|
|---|
| 35 | Long professorId = Long.parseLong(request.getParameter("professor_id"));
|
|---|
| 36 | Long subjectId = Long.parseLong(request.getParameter("subject_id"));
|
|---|
| 37 |
|
|---|
| 38 | service.assignProfessorToSubject(professorId, subjectId);
|
|---|
| 39 | response.sendRedirect(request.getContextPath() + "/professor/edit?id=" + professorId);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | private void remove(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
|
|---|
| 43 | Long professorId = Long.parseLong(request.getParameter("professor_id"));
|
|---|
| 44 | Long subjectId = Long.parseLong(request.getParameter("subject_id"));
|
|---|
| 45 |
|
|---|
| 46 | service.removeProfessorFromSubject(professorId, subjectId);
|
|---|
| 47 | response.sendRedirect(request.getContextPath() + "/professor/edit?id=" + professorId);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | } |
|---|