Skip to content

Commit 15ab6c6

Browse files
committed
Changes order parameter to id
1 parent 1657916 commit 15ab6c6

4 files changed

Lines changed: 43 additions & 27 deletions

File tree

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,36 @@ public class Baeldung {
3737
}
3838

3939
@GET
40-
@Path("courses/{courseOrder}")
41-
public Course getCourse(@PathParam("courseOrder") int courseOrder) {
42-
return courses.get(courseOrder);
40+
@Path("courses/{courseId}")
41+
public Course getCourse(@PathParam("courseId") int courseId) {
42+
return findById(courseId);
4343
}
4444

4545
@PUT
46-
@Path("courses/{courseOrder}")
47-
public Response updateCourse(@PathParam("courseOrder") int courseOrder, Course course) {
48-
Course existingCourse = courses.get(courseOrder);
46+
@Path("courses/{courseId}")
47+
public Response updateCourse(@PathParam("courseId") int courseId, Course course) {
48+
Course existingCourse = findById(courseId);
4949
if (existingCourse == null) {
5050
return Response.status(Response.Status.NOT_FOUND).build();
5151
}
5252
if (existingCourse.equals(course)) {
53-
return Response.notModified().build();
53+
return Response.notModified().build();
5454
}
55-
courses.put(courseOrder, course);
55+
courses.put(courseId, course);
5656
return Response.ok().build();
5757
}
5858

59-
@Path("courses/{courseOrder}/students")
60-
public Course pathToStudent(@PathParam("courseOrder") int courseOrder) {
61-
return courses.get(courseOrder);
59+
@Path("courses/{courseId}/students")
60+
public Course pathToStudent(@PathParam("courseId") int courseId) {
61+
return findById(courseId);
62+
}
63+
64+
private Course findById(int id) {
65+
for (Map.Entry<Integer, Course> course : courses.entrySet()) {
66+
if (course.getKey() == id) {
67+
return course.getValue();
68+
}
69+
}
70+
return null;
6271
}
6372
}

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class Course {
1212
private int id;
1313
private String name;
14-
private List<Student> students;
14+
private List<Student> students = new ArrayList<>();
1515

1616
public int getId() {
1717
return id;
@@ -38,16 +38,13 @@ public void setStudents(List<Student> students) {
3838
}
3939

4040
@GET
41-
@Path("{studentOrder}")
42-
public Student getStudent(@PathParam("studentOrder") int studentOrder) {
43-
return students.get(studentOrder);
41+
@Path("{studentId}")
42+
public Student getStudent(@PathParam("studentId") int studentId) {
43+
return findById(studentId);
4444
}
4545

4646
@POST
4747
public Response createStudent(Student student) {
48-
if (students == null) {
49-
students = new ArrayList<>();
50-
}
5148
if (students.contains(student)) {
5249
return Response.status(Response.Status.CONFLICT).build();
5350
}
@@ -56,14 +53,24 @@ public Response createStudent(Student student) {
5653
}
5754

5855
@DELETE
59-
@Path("{studentOrder}")
60-
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
61-
if (students == null || studentOrder >= students.size()) {
56+
@Path("{studentId}")
57+
public Response deleteStudent(@PathParam("studentId") int studentId) {
58+
Student student = findById(studentId);
59+
if (student == null) {
6260
return Response.status(Response.Status.NOT_FOUND).build();
6361
}
64-
students.remove(studentOrder);
62+
students.remove(student);
6563
return Response.ok().build();
6664
}
65+
66+
private Student findById(int id) {
67+
for (Student student : students) {
68+
if (student.getId() == id) {
69+
return student;
70+
}
71+
}
72+
return null;
73+
}
6774

6875
@Override
6976
public int hashCode() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<Student>
2-
<id>1</id>
3-
<name>Student A</name>
2+
<id>2</id>
3+
<name>Student B</name>
44
</Student>

‎apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,21 @@ public void whenCreateValidStudent_thenReceiveOKResponse() throws IOException {
9191
HttpResponse response = client.execute(httpPost);
9292
assertEquals(200, response.getStatusLine().getStatusCode());
9393

94-
Student student = getStudent(2, 0);
94+
Student student = getStudent(2, 3);
9595
assertEquals(3, student.getId());
9696
assertEquals("Student C", student.getName());
9797
}
9898

9999
@Test
100100
public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException {
101-
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/2");
101+
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/3");
102102
HttpResponse response = client.execute(httpDelete);
103103
assertEquals(404, response.getStatusLine().getStatusCode());
104104
}
105105

106106
@Test
107107
public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException {
108-
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0");
108+
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/1");
109109
HttpResponse response = client.execute(httpDelete);
110110
assertEquals(200, response.getStatusLine().getStatusCode());
111111

0 commit comments

Comments
 (0)