From 15ab6c6326927cdb3fc0d3f81cf90335aa4f9f28 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Thu, 20 Oct 2016 16:41:38 +0700 Subject: [PATCH] Changes order parameter to id --- .../cxf/jaxrs/implementation/Baeldung.java | 31 ++++++++++++------- .../cxf/jaxrs/implementation/Course.java | 29 ++++++++++------- .../src/main/resources/conflict_student.xml | 4 +-- .../cxf/jaxrs/implementation/ServiceTest.java | 6 ++-- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java index 39872f4e32..9dd63cf3ac 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java @@ -37,27 +37,36 @@ public class Baeldung { } @GET - @Path("courses/{courseOrder}") - public Course getCourse(@PathParam("courseOrder") int courseOrder) { - return courses.get(courseOrder); + @Path("courses/{courseId}") + public Course getCourse(@PathParam("courseId") int courseId) { + return findById(courseId); } @PUT - @Path("courses/{courseOrder}") - public Response updateCourse(@PathParam("courseOrder") int courseOrder, Course course) { - Course existingCourse = courses.get(courseOrder); + @Path("courses/{courseId}") + public Response updateCourse(@PathParam("courseId") int courseId, Course course) { + Course existingCourse = findById(courseId); if (existingCourse == null) { return Response.status(Response.Status.NOT_FOUND).build(); } if (existingCourse.equals(course)) { - return Response.notModified().build(); + return Response.notModified().build(); } - courses.put(courseOrder, course); + courses.put(courseId, course); return Response.ok().build(); } - @Path("courses/{courseOrder}/students") - public Course pathToStudent(@PathParam("courseOrder") int courseOrder) { - return courses.get(courseOrder); + @Path("courses/{courseId}/students") + public Course pathToStudent(@PathParam("courseId") int courseId) { + return findById(courseId); + } + + private Course findById(int id) { + for (Map.Entry course : courses.entrySet()) { + if (course.getKey() == id) { + return course.getValue(); + } + } + return null; } } \ No newline at end of file diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java index efb7191290..dfbe8eef1d 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java @@ -11,7 +11,7 @@ import java.util.List; public class Course { private int id; private String name; - private List students; + private List students = new ArrayList<>(); public int getId() { return id; @@ -38,16 +38,13 @@ public class Course { } @GET - @Path("{studentOrder}") - public Student getStudent(@PathParam("studentOrder") int studentOrder) { - return students.get(studentOrder); + @Path("{studentId}") + public Student getStudent(@PathParam("studentId") int studentId) { + return findById(studentId); } @POST public Response createStudent(Student student) { - if (students == null) { - students = new ArrayList<>(); - } if (students.contains(student)) { return Response.status(Response.Status.CONFLICT).build(); } @@ -56,14 +53,24 @@ public class Course { } @DELETE - @Path("{studentOrder}") - public Response deleteStudent(@PathParam("studentOrder") int studentOrder) { - if (students == null || studentOrder >= students.size()) { + @Path("{studentId}") + public Response deleteStudent(@PathParam("studentId") int studentId) { + Student student = findById(studentId); + if (student == null) { return Response.status(Response.Status.NOT_FOUND).build(); } - students.remove(studentOrder); + students.remove(student); return Response.ok().build(); } + + private Student findById(int id) { + for (Student student : students) { + if (student.getId() == id) { + return student; + } + } + return null; + } @Override public int hashCode() { diff --git a/apache-cxf/cxf-jaxrs-implementation/src/main/resources/conflict_student.xml b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/conflict_student.xml index ccf97edb2e..7d083dbdc9 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/main/resources/conflict_student.xml +++ b/apache-cxf/cxf-jaxrs-implementation/src/main/resources/conflict_student.xml @@ -1,4 +1,4 @@ - 1 - Student A + 2 + Student B \ No newline at end of file diff --git a/apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java b/apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java index 8d06518df2..b8fc833194 100644 --- a/apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java +++ b/apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java @@ -91,21 +91,21 @@ public class ServiceTest { HttpResponse response = client.execute(httpPost); assertEquals(200, response.getStatusLine().getStatusCode()); - Student student = getStudent(2, 0); + Student student = getStudent(2, 3); assertEquals(3, student.getId()); assertEquals("Student C", student.getName()); } @Test public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException { - HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/2"); + HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/3"); HttpResponse response = client.execute(httpDelete); assertEquals(404, response.getStatusLine().getStatusCode()); } @Test public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException { - HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0"); + HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/1"); HttpResponse response = client.execute(httpDelete); assertEquals(200, response.getStatusLine().getStatusCode());