|
1 | 1 | package controllers; |
2 | | -import models.*; |
3 | | -import util.*; |
4 | | -import play.mvc.*; |
5 | | -import play.libs.Json; |
6 | | -import play.libs.Json.*; |
7 | | -import com.fasterxml.jackson.databind.JsonNode; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
8 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
9 | | -import java.util.List; |
| 5 | +import models.Student; |
| 6 | +import models.StudentStore; |
| 7 | +import play.libs.Json; |
| 8 | +import play.mvc.Controller; |
| 9 | +import play.mvc.Result; |
| 10 | +import util.Util; |
| 11 | + |
| 12 | +import java.util.Set; |
10 | 13 |
|
11 | 14 | public class StudentController extends Controller { |
12 | 15 | public Result create() { |
13 | | - JsonNode json = request().body().asJson(); |
14 | | - if(json == null) |
15 | | - return badRequest(Util.createResponse("Expecting Json data",false)); |
16 | | - Student student=StudentStore.getInstance().addStudent((Student)Json.fromJson(json,Student.class)); |
17 | | - JsonNode jsonObject=Json.toJson(student); |
18 | | - return created(Util.createResponse(jsonObject,true)); |
| 16 | + JsonNode json = request().body().asJson(); |
| 17 | + if (json == null) { |
| 18 | + return badRequest(Util.createResponse("Expecting Json data", false)); |
| 19 | + } |
| 20 | + Student student = StudentStore.getInstance().addStudent(Json.fromJson(json, Student.class)); |
| 21 | + JsonNode jsonObject = Json.toJson(student); |
| 22 | + return created(Util.createResponse(jsonObject, true)); |
19 | 23 | } |
20 | | - public Result update() { |
| 24 | + |
| 25 | + public Result update() { |
21 | 26 | JsonNode json = request().body().asJson(); |
22 | | - if(json == null) |
23 | | - return badRequest(Util.createResponse("Expecting Json data",false)); |
24 | | - Student student=StudentStore.getInstance().updateStudent((Student)Json.fromJson(json,Student.class)); |
25 | | - if(student==null){ |
26 | | - return notFound(Util.createResponse("Student not found",false)); |
27 | | - } |
28 | | - |
29 | | - JsonNode jsonObject=Json.toJson(student); |
30 | | - return ok(Util.createResponse(jsonObject,true)); |
| 27 | + if (json == null) { |
| 28 | + return badRequest(Util.createResponse("Expecting Json data", false)); |
| 29 | + } |
| 30 | + Student student = StudentStore.getInstance().updateStudent(Json.fromJson(json, Student.class)); |
| 31 | + if (student == null) { |
| 32 | + return notFound(Util.createResponse("Student not found", false)); |
| 33 | + } |
| 34 | + |
| 35 | + JsonNode jsonObject = Json.toJson(student); |
| 36 | + return ok(Util.createResponse(jsonObject, true)); |
31 | 37 | } |
32 | | - public Result retrieve(int id) { |
33 | | - Student student=StudentStore.getInstance().getStudent(id); |
34 | | - if(student==null){ |
35 | | - return notFound(Util.createResponse("Student with id:"+id+" not found",false)); |
36 | | - } |
37 | | - JsonNode jsonObjects=Json.toJson(student); |
38 | | - return ok(Util.createResponse(jsonObjects,true)); |
| 38 | + |
| 39 | + public Result retrieve(int id) { |
| 40 | + if (StudentStore.getInstance().getStudent(id) == null) { |
| 41 | + return notFound(Util.createResponse("Student with id:" + id + " not found", false)); |
| 42 | + } |
| 43 | + JsonNode jsonObjects = Json.toJson(StudentStore.getInstance().getStudent(id)); |
| 44 | + return ok(Util.createResponse(jsonObjects, true)); |
39 | 45 | } |
40 | | - public Result listStudents() { |
41 | | - Set<Student> result=StudentStore.getInstance().getAllStudents(); |
42 | | - ObjectMapper mapper = new ObjectMapper(); |
43 | 46 |
|
44 | | - JsonNode jsonData=mapper.convertValue(result, JsonNode.class); |
45 | | - return ok(Util.createResponse(jsonData,true)); |
| 47 | + public Result listStudents() { |
| 48 | + Set<Student> result = StudentStore.getInstance().getAllStudents(); |
| 49 | + ObjectMapper mapper = new ObjectMapper(); |
| 50 | + |
| 51 | + JsonNode jsonData = mapper.convertValue(result, JsonNode.class); |
| 52 | + return ok(Util.createResponse(jsonData, true)); |
46 | 53 |
|
47 | 54 | } |
48 | | - public Result delete(int id) { |
49 | | - boolean status=StudentStore.getInstance().deleteStudent(id); |
50 | | - if(!status){ |
51 | | - return notFound(Util.createResponse("Student with id:"+id+" not found",false)); |
52 | | - } |
53 | | - return ok(Util.createResponse("Student with id:"+id+" deleted",true)); |
| 55 | + |
| 56 | + public Result delete(int id) { |
| 57 | + if (!StudentStore.getInstance().deleteStudent(id)) { |
| 58 | + return notFound(Util.createResponse("Student with id:" + id + " not found", false)); |
| 59 | + } |
| 60 | + return ok(Util.createResponse("Student with id:" + id + " deleted", true)); |
54 | 61 | } |
55 | 62 |
|
56 | 63 | } |
0 commit comments