Changes order parameter to id

This commit is contained in:
Thai Nguyen 2016-10-20 16:41:38 +07:00
parent 1657916676
commit 15ab6c6326
4 changed files with 43 additions and 27 deletions

View File

@ -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<Integer, Course> course : courses.entrySet()) {
if (course.getKey() == id) {
return course.getValue();
}
}
return null;
}
}

View File

@ -11,7 +11,7 @@ import java.util.List;
public class Course {
private int id;
private String name;
private List<Student> students;
private List<Student> 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() {

View File

@ -1,4 +1,4 @@
<Student>
<id>1</id>
<name>Student A</name>
<id>2</id>
<name>Student B</name>
</Student>

View File

@ -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());