Merge pull request #759 from nguyennamthai/master
Changes order parameter to id
This commit is contained in:
commit
ee34720098
@ -37,27 +37,36 @@ public class Baeldung {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("courses/{courseOrder}")
|
@Path("courses/{courseId}")
|
||||||
public Course getCourse(@PathParam("courseOrder") int courseOrder) {
|
public Course getCourse(@PathParam("courseId") int courseId) {
|
||||||
return courses.get(courseOrder);
|
return findById(courseId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PUT
|
@PUT
|
||||||
@Path("courses/{courseOrder}")
|
@Path("courses/{courseId}")
|
||||||
public Response updateCourse(@PathParam("courseOrder") int courseOrder, Course course) {
|
public Response updateCourse(@PathParam("courseId") int courseId, Course course) {
|
||||||
Course existingCourse = courses.get(courseOrder);
|
Course existingCourse = findById(courseId);
|
||||||
if (existingCourse == null) {
|
if (existingCourse == null) {
|
||||||
return Response.status(Response.Status.NOT_FOUND).build();
|
return Response.status(Response.Status.NOT_FOUND).build();
|
||||||
}
|
}
|
||||||
if (existingCourse.equals(course)) {
|
if (existingCourse.equals(course)) {
|
||||||
return Response.notModified().build();
|
return Response.notModified().build();
|
||||||
}
|
}
|
||||||
courses.put(courseOrder, course);
|
courses.put(courseId, course);
|
||||||
return Response.ok().build();
|
return Response.ok().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Path("courses/{courseOrder}/students")
|
@Path("courses/{courseId}/students")
|
||||||
public Course pathToStudent(@PathParam("courseOrder") int courseOrder) {
|
public Course pathToStudent(@PathParam("courseId") int courseId) {
|
||||||
return courses.get(courseOrder);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,7 +11,7 @@ import java.util.List;
|
|||||||
public class Course {
|
public class Course {
|
||||||
private int id;
|
private int id;
|
||||||
private String name;
|
private String name;
|
||||||
private List<Student> students;
|
private List<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
@ -38,33 +38,42 @@ public class Course {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("{studentOrder}")
|
@Path("{studentId}")
|
||||||
public Student getStudent(@PathParam("studentOrder") int studentOrder) {
|
public Student getStudent(@PathParam("studentId") int studentId) {
|
||||||
return students.get(studentOrder);
|
return findById(studentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
public Response createStudent(Student student) {
|
public Response createStudent(Student student) {
|
||||||
if (students == null) {
|
for (Student element : students) {
|
||||||
students = new ArrayList<>();
|
if (element.getId() == student.getId()) {
|
||||||
}
|
|
||||||
if (students.contains(student)) {
|
|
||||||
return Response.status(Response.Status.CONFLICT).build();
|
return Response.status(Response.Status.CONFLICT).build();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
students.add(student);
|
students.add(student);
|
||||||
return Response.ok(student).build();
|
return Response.ok(student).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
@Path("{studentOrder}")
|
@Path("{studentId}")
|
||||||
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
|
public Response deleteStudent(@PathParam("studentId") int studentId) {
|
||||||
if (students == null || studentOrder >= students.size()) {
|
Student student = findById(studentId);
|
||||||
|
if (student == null) {
|
||||||
return Response.status(Response.Status.NOT_FOUND).build();
|
return Response.status(Response.Status.NOT_FOUND).build();
|
||||||
}
|
}
|
||||||
students.remove(studentOrder);
|
students.remove(student);
|
||||||
return Response.ok().build();
|
return Response.ok().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Student findById(int id) {
|
||||||
|
for (Student student : students) {
|
||||||
|
if (student.getId() == id) {
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return id + name.hashCode();
|
return id + name.hashCode();
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
<Student>
|
<Student>
|
||||||
<id>1</id>
|
<id>2</id>
|
||||||
<name>Student A</name>
|
<name>Student B</name>
|
||||||
</Student>
|
</Student>
|
@ -91,21 +91,21 @@ public class ServiceTest {
|
|||||||
HttpResponse response = client.execute(httpPost);
|
HttpResponse response = client.execute(httpPost);
|
||||||
assertEquals(200, response.getStatusLine().getStatusCode());
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
|
|
||||||
Student student = getStudent(2, 0);
|
Student student = getStudent(2, 3);
|
||||||
assertEquals(3, student.getId());
|
assertEquals(3, student.getId());
|
||||||
assertEquals("Student C", student.getName());
|
assertEquals("Student C", student.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException {
|
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);
|
HttpResponse response = client.execute(httpDelete);
|
||||||
assertEquals(404, response.getStatusLine().getStatusCode());
|
assertEquals(404, response.getStatusLine().getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException {
|
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);
|
HttpResponse response = client.execute(httpDelete);
|
||||||
assertEquals(200, response.getStatusLine().getStatusCode());
|
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user