Пример #1
0
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    Courses course = JSON.getObjectMapper().readValue(req.getReader(), Courses.class);

    // Use a GetUser controller to find the item in the database
    AddingANewCourse controller = new AddingANewCourse();
    controller.addCourse(course);

    // Set status code and content type
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.setContentType("application/json");

    // writing the operation out.
    JSON.getObjectMapper().writeValue(resp.getWriter(), course);
  }
Пример #2
0
  protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String pathInfo = req.getPathInfo();
    if (pathInfo == null || pathInfo.equals("") || pathInfo.equals("/")) {
      // Set status code and content type
      resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
      resp.setContentType("application/json");
      resp.getWriter().write("we delete nothing");
      return;
    }

    // Get the item name
    if (pathInfo.startsWith("/")) {
      pathInfo = pathInfo.substring(1);
    }

    int courseId = Integer.parseInt(pathInfo);

    RemovingACourse deleteUser = new RemovingACourse();
    deleteUser.removingACourse(courseId);

    // send response
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.setContentType("application/json");

    GetCourseById controller = new GetCourseById();

    JSON.getObjectMapper().writeValue(resp.getWriter(), controller.getCourse(courseId));
  }
Пример #3
0
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String pathInfo = req.getPathInfo();
    if (pathInfo == null || pathInfo.equals("") || pathInfo.equals("/")) {
      getAllCourses gt = new getAllCourses();
      List<Courses> cs = gt.getAllcourses();

      // Set status code and content type
      resp.setStatus(HttpServletResponse.SC_OK);
      resp.setContentType("application/json");

      // Return the item in JSON format
      JSON.getObjectMapper().writeValue(resp.getWriter(), cs);

      return;
    }

    // Get the item name
    if (pathInfo.startsWith("/")) {
      pathInfo = pathInfo.substring(1);
    }
    // Use a GettingACourse controller to find the item in the database
    gettingACourse controller = new gettingACourse();
    Courses course = controller.getCourse(pathInfo);

    if (course == null) {
      // No such item, so return a NOT FOUND response
      resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
      resp.setContentType("text/plain");
      resp.getWriter().println("Not a course: " + pathInfo);
      return;
    }
    // Set status code and content type
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.setContentType("application/json");

    // Return the item in JSON format
    JSON.getObjectMapper().writeValue(resp.getWriter(), course);
  }