Refactor Play example
This commit is contained in:
parent
1ecb726d7e
commit
0dc8119722
|
@ -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.
|
||||
*
|
||||
* <p>
|
||||
* The configuration in the <code>routes</code> file means that this method
|
||||
* will be called when the application receives a <code>GET</code> request with
|
||||
* a path of <code>/message</code>.
|
||||
|
@ -50,9 +54,9 @@ public class AsyncController extends Controller {
|
|||
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
actorSystem.scheduler().scheduleOnce(
|
||||
Duration.create(time, timeUnit),
|
||||
() -> future.complete("Hi!"),
|
||||
exec
|
||||
Duration.create(time, timeUnit),
|
||||
() -> future.complete("Hi!"),
|
||||
exec
|
||||
);
|
||||
return future;
|
||||
}
|
||||
|
|
|
@ -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<Student> 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<Student> 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Integer, Student> 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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue