diff --git a/play-framework/student-api/app/controllers/AsyncController.java b/play-framework/student-api/app/controllers/AsyncController.java
index 33cd112837..92c84bb755 100644
--- a/play-framework/student-api/app/controllers/AsyncController.java
+++ b/play-framework/student-api/app/controllers/AsyncController.java
@@ -1,13 +1,17 @@
package controllers;
import akka.actor.ActorSystem;
+
import javax.inject.*;
+
import play.*;
import play.mvc.*;
+
import java.util.concurrent.Executor;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
+
import scala.concurrent.duration.Duration;
import scala.concurrent.ExecutionContextExecutor;
@@ -17,11 +21,11 @@ import scala.concurrent.ExecutionContextExecutor;
* asynchronously delay sending a response for 1 second.
*
* @param actorSystem We need the {@link ActorSystem}'s
- * {@link Scheduler} to run code after a delay.
- * @param exec We need a Java {@link Executor} to apply the result
- * of the {@link CompletableFuture} and a Scala
- * {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
- * An {@link ExecutionContextExecutor} implements both interfaces.
+ * {@link Scheduler} to run code after a delay.
+ * @param exec We need a Java {@link Executor} to apply the result
+ * of the {@link CompletableFuture} and a Scala
+ * {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
+ * An {@link ExecutionContextExecutor} implements both interfaces.
*/
@Singleton
public class AsyncController extends Controller {
@@ -31,14 +35,14 @@ public class AsyncController extends Controller {
@Inject
public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor exec) {
- this.actorSystem = actorSystem;
- this.exec = exec;
+ this.actorSystem = actorSystem;
+ this.exec = exec;
}
/**
* An action that returns a plain text message after a delay
* of 1 second.
- *
+ *
* The configuration in the routes file means that this method
* will be called when the application receives a GET request with
* a path of /message.
@@ -50,9 +54,9 @@ public class AsyncController extends Controller {
private CompletionStage getFutureMessage(long time, TimeUnit timeUnit) {
CompletableFuture future = new CompletableFuture<>();
actorSystem.scheduler().scheduleOnce(
- Duration.create(time, timeUnit),
- () -> future.complete("Hi!"),
- exec
+ Duration.create(time, timeUnit),
+ () -> future.complete("Hi!"),
+ exec
);
return future;
}
diff --git a/play-framework/student-api/app/controllers/StudentController.java b/play-framework/student-api/app/controllers/StudentController.java
index 08318733b5..8b759b9598 100644
--- a/play-framework/student-api/app/controllers/StudentController.java
+++ b/play-framework/student-api/app/controllers/StudentController.java
@@ -1,56 +1,63 @@
package controllers;
-import models.*;
-import util.*;
-import play.mvc.*;
-import play.libs.Json;
-import play.libs.Json.*;
-import com.fasterxml.jackson.databind.JsonNode;
+
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-import java.util.List;
+import models.Student;
+import models.StudentStore;
+import play.libs.Json;
+import play.mvc.Controller;
+import play.mvc.Result;
+import util.Util;
+
+import java.util.Set;
public class StudentController extends Controller {
public Result create() {
- JsonNode json = request().body().asJson();
- if(json == null)
- return badRequest(Util.createResponse("Expecting Json data",false));
- Student student=StudentStore.getInstance().addStudent((Student)Json.fromJson(json,Student.class));
- JsonNode jsonObject=Json.toJson(student);
- return created(Util.createResponse(jsonObject,true));
- }
- public Result update() {
JsonNode json = request().body().asJson();
- if(json == null)
- return badRequest(Util.createResponse("Expecting Json data",false));
- Student student=StudentStore.getInstance().updateStudent((Student)Json.fromJson(json,Student.class));
- if(student==null){
- return notFound(Util.createResponse("Student not found",false));
- }
-
- JsonNode jsonObject=Json.toJson(student);
- return ok(Util.createResponse(jsonObject,true));
+ if (json == null) {
+ return badRequest(Util.createResponse("Expecting Json data", false));
+ }
+ Student student = StudentStore.getInstance().addStudent(Json.fromJson(json, Student.class));
+ JsonNode jsonObject = Json.toJson(student);
+ return created(Util.createResponse(jsonObject, true));
}
- public Result retrieve(int id) {
- Student student=StudentStore.getInstance().getStudent(id);
- if(student==null){
- return notFound(Util.createResponse("Student with id:"+id+" not found",false));
- }
- JsonNode jsonObjects=Json.toJson(student);
- return ok(Util.createResponse(jsonObjects,true));
- }
- public Result listStudents() {
- Set result=StudentStore.getInstance().getAllStudents();
- ObjectMapper mapper = new ObjectMapper();
- JsonNode jsonData=mapper.convertValue(result, JsonNode.class);
- return ok(Util.createResponse(jsonData,true));
+ public Result update() {
+ JsonNode json = request().body().asJson();
+ if (json == null) {
+ return badRequest(Util.createResponse("Expecting Json data", false));
+ }
+ Student student = StudentStore.getInstance().updateStudent(Json.fromJson(json, Student.class));
+ if (student == null) {
+ return notFound(Util.createResponse("Student not found", false));
+ }
+
+ JsonNode jsonObject = Json.toJson(student);
+ return ok(Util.createResponse(jsonObject, true));
+ }
+
+ public Result retrieve(int id) {
+ if (StudentStore.getInstance().getStudent(id) == null) {
+ return notFound(Util.createResponse("Student with id:" + id + " not found", false));
+ }
+ JsonNode jsonObjects = Json.toJson(StudentStore.getInstance().getStudent(id));
+ return ok(Util.createResponse(jsonObjects, true));
+ }
+
+ public Result listStudents() {
+ Set result = StudentStore.getInstance().getAllStudents();
+ ObjectMapper mapper = new ObjectMapper();
+
+ JsonNode jsonData = mapper.convertValue(result, JsonNode.class);
+ return ok(Util.createResponse(jsonData, true));
}
- public Result delete(int id) {
- boolean status=StudentStore.getInstance().deleteStudent(id);
- if(!status){
- return notFound(Util.createResponse("Student with id:"+id+" not found",false));
- }
- return ok(Util.createResponse("Student with id:"+id+" deleted",true));
+
+ public Result delete(int id) {
+ if (!StudentStore.getInstance().deleteStudent(id)) {
+ return notFound(Util.createResponse("Student with id:" + id + " not found", false));
+ }
+ return ok(Util.createResponse("Student with id:" + id + " deleted", true));
}
}
diff --git a/play-framework/student-api/app/models/StudentStore.java b/play-framework/student-api/app/models/StudentStore.java
index 1ebc14ffab..add6a5dbd6 100644
--- a/play-framework/student-api/app/models/StudentStore.java
+++ b/play-framework/student-api/app/models/StudentStore.java
@@ -1,5 +1,5 @@
package models;
-import java.util.ArrayList;
+
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -10,13 +10,14 @@ public class StudentStore {
private Map students = new HashMap<>();
public static StudentStore getInstance() {
- if (instance == null)
+ if (instance == null) {
instance = new StudentStore();
+ }
return instance;
}
public Student addStudent(Student student) {
- int id = students.size() + 1;
+ int id = students.size();
student.setId(id);
students.put(id, student);
return student;
@@ -31,7 +32,7 @@ public class StudentStore {
}
public Student updateStudent(Student student) {
- int id=student.getId();
+ int id = student.getId();
if (students.containsKey(id)) {
students.put(id, student);
return student;
diff --git a/play-framework/student-api/app/util/Util.java b/play-framework/student-api/app/util/Util.java
index 3718b50677..a853a4cb43 100644
--- a/play-framework/student-api/app/util/Util.java
+++ b/play-framework/student-api/app/util/Util.java
@@ -1,17 +1,17 @@
package util;
-import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
-import play.libs.Json.*;
-import com.fasterxml.jackson.databind.JsonNode;
-public class Util{
- public static ObjectNode createResponse(Object response,boolean ok){
- ObjectNode result = Json.newObject();
- result.put("isSuccessfull", ok);
- if(response instanceof String)
- result.put("body",(String)response);
- else result.put("body",(JsonNode)response);
+public class Util {
+ public static ObjectNode createResponse(Object response, boolean ok) {
+ ObjectNode result = Json.newObject();
+ result.put("isSuccessfull", ok);
+ if (response instanceof String)
+ result.put("body", (String) response);
+ else result.put("body", (JsonNode) response);
- return result;
- }
+ return result;
+ }
}
\ No newline at end of file