Refactor Play example
This commit is contained in:
parent
1ecb726d7e
commit
0dc8119722
|
@ -1,13 +1,17 @@
|
||||||
package controllers;
|
package controllers;
|
||||||
|
|
||||||
import akka.actor.ActorSystem;
|
import akka.actor.ActorSystem;
|
||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
import play.*;
|
import play.*;
|
||||||
import play.mvc.*;
|
import play.mvc.*;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionStage;
|
import java.util.concurrent.CompletionStage;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import scala.concurrent.duration.Duration;
|
import scala.concurrent.duration.Duration;
|
||||||
import scala.concurrent.ExecutionContextExecutor;
|
import scala.concurrent.ExecutionContextExecutor;
|
||||||
|
|
||||||
|
@ -17,11 +21,11 @@ import scala.concurrent.ExecutionContextExecutor;
|
||||||
* asynchronously delay sending a response for 1 second.
|
* asynchronously delay sending a response for 1 second.
|
||||||
*
|
*
|
||||||
* @param actorSystem We need the {@link ActorSystem}'s
|
* @param actorSystem We need the {@link ActorSystem}'s
|
||||||
* {@link Scheduler} to run code after a delay.
|
* {@link Scheduler} to run code after a delay.
|
||||||
* @param exec We need a Java {@link Executor} to apply the result
|
* @param exec We need a Java {@link Executor} to apply the result
|
||||||
* of the {@link CompletableFuture} and a Scala
|
* of the {@link CompletableFuture} and a Scala
|
||||||
* {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
|
* {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
|
||||||
* An {@link ExecutionContextExecutor} implements both interfaces.
|
* An {@link ExecutionContextExecutor} implements both interfaces.
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
public class AsyncController extends Controller {
|
public class AsyncController extends Controller {
|
||||||
|
@ -31,14 +35,14 @@ public class AsyncController extends Controller {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor exec) {
|
public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor exec) {
|
||||||
this.actorSystem = actorSystem;
|
this.actorSystem = actorSystem;
|
||||||
this.exec = exec;
|
this.exec = exec;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An action that returns a plain text message after a delay
|
* An action that returns a plain text message after a delay
|
||||||
* of 1 second.
|
* of 1 second.
|
||||||
*
|
* <p>
|
||||||
* The configuration in the <code>routes</code> file means that this method
|
* 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
|
* will be called when the application receives a <code>GET</code> request with
|
||||||
* a path of <code>/message</code>.
|
* a path of <code>/message</code>.
|
||||||
|
@ -50,9 +54,9 @@ public class AsyncController extends Controller {
|
||||||
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
|
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
|
||||||
CompletableFuture<String> future = new CompletableFuture<>();
|
CompletableFuture<String> future = new CompletableFuture<>();
|
||||||
actorSystem.scheduler().scheduleOnce(
|
actorSystem.scheduler().scheduleOnce(
|
||||||
Duration.create(time, timeUnit),
|
Duration.create(time, timeUnit),
|
||||||
() -> future.complete("Hi!"),
|
() -> future.complete("Hi!"),
|
||||||
exec
|
exec
|
||||||
);
|
);
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,56 +1,63 @@
|
||||||
package controllers;
|
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 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 class StudentController extends Controller {
|
||||||
public Result create() {
|
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();
|
JsonNode json = request().body().asJson();
|
||||||
if(json == null)
|
if (json == null) {
|
||||||
return badRequest(Util.createResponse("Expecting Json data",false));
|
return badRequest(Util.createResponse("Expecting Json data", false));
|
||||||
Student student=StudentStore.getInstance().updateStudent((Student)Json.fromJson(json,Student.class));
|
}
|
||||||
if(student==null){
|
Student student = StudentStore.getInstance().addStudent(Json.fromJson(json, Student.class));
|
||||||
return notFound(Util.createResponse("Student not found",false));
|
JsonNode jsonObject = Json.toJson(student);
|
||||||
}
|
return created(Util.createResponse(jsonObject, true));
|
||||||
|
|
||||||
JsonNode jsonObject=Json.toJson(student);
|
|
||||||
return ok(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);
|
public Result update() {
|
||||||
return ok(Util.createResponse(jsonData,true));
|
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);
|
public Result delete(int id) {
|
||||||
if(!status){
|
if (!StudentStore.getInstance().deleteStudent(id)) {
|
||||||
return notFound(Util.createResponse("Student with id:"+id+" not found",false));
|
return notFound(Util.createResponse("Student with id:" + id + " not found", false));
|
||||||
}
|
}
|
||||||
return ok(Util.createResponse("Student with id:"+id+" deleted",true));
|
return ok(Util.createResponse("Student with id:" + id + " deleted", true));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
package models;
|
package models;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -10,13 +10,14 @@ public class StudentStore {
|
||||||
private Map<Integer, Student> students = new HashMap<>();
|
private Map<Integer, Student> students = new HashMap<>();
|
||||||
|
|
||||||
public static StudentStore getInstance() {
|
public static StudentStore getInstance() {
|
||||||
if (instance == null)
|
if (instance == null) {
|
||||||
instance = new StudentStore();
|
instance = new StudentStore();
|
||||||
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Student addStudent(Student student) {
|
public Student addStudent(Student student) {
|
||||||
int id = students.size() + 1;
|
int id = students.size();
|
||||||
student.setId(id);
|
student.setId(id);
|
||||||
students.put(id, student);
|
students.put(id, student);
|
||||||
return student;
|
return student;
|
||||||
|
@ -31,7 +32,7 @@ public class StudentStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Student updateStudent(Student student) {
|
public Student updateStudent(Student student) {
|
||||||
int id=student.getId();
|
int id = student.getId();
|
||||||
if (students.containsKey(id)) {
|
if (students.containsKey(id)) {
|
||||||
students.put(id, student);
|
students.put(id, student);
|
||||||
return student;
|
return student;
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
package util;
|
package util;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import play.libs.Json;
|
import play.libs.Json;
|
||||||
import play.libs.Json.*;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
|
|
||||||
public class Util{
|
public class Util {
|
||||||
public static ObjectNode createResponse(Object response,boolean ok){
|
public static ObjectNode createResponse(Object response, boolean ok) {
|
||||||
ObjectNode result = Json.newObject();
|
ObjectNode result = Json.newObject();
|
||||||
result.put("isSuccessfull", ok);
|
result.put("isSuccessfull", ok);
|
||||||
if(response instanceof String)
|
if (response instanceof String)
|
||||||
result.put("body",(String)response);
|
result.put("body", (String) response);
|
||||||
else result.put("body",(JsonNode)response);
|
else result.put("body", (JsonNode) response);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue