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;
|
||||||
|
|
||||||
|
@ -38,7 +42,7 @@ public class AsyncController extends Controller {
|
||||||
/**
|
/**
|
||||||
* 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>.
|
||||||
|
|
|
@ -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();
|
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().addStudent((Student)Json.fromJson(json,Student.class));
|
|
||||||
JsonNode jsonObject=Json.toJson(student);
|
|
||||||
return created(Util.createResponse(jsonObject,true));
|
|
||||||
}
|
}
|
||||||
|
Student student = StudentStore.getInstance().addStudent(Json.fromJson(json, Student.class));
|
||||||
|
JsonNode jsonObject = Json.toJson(student);
|
||||||
|
return created(Util.createResponse(jsonObject, true));
|
||||||
|
}
|
||||||
|
|
||||||
public Result update() {
|
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().updateStudent(Json.fromJson(json, Student.class));
|
||||||
return notFound(Util.createResponse("Student not found",false));
|
if (student == null) {
|
||||||
|
return notFound(Util.createResponse("Student not found", false));
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonNode jsonObject=Json.toJson(student);
|
JsonNode jsonObject = Json.toJson(student);
|
||||||
return ok(Util.createResponse(jsonObject,true));
|
return ok(Util.createResponse(jsonObject, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result retrieve(int id) {
|
public Result retrieve(int id) {
|
||||||
Student student=StudentStore.getInstance().getStudent(id);
|
if (StudentStore.getInstance().getStudent(id) == null) {
|
||||||
if(student==null){
|
return notFound(Util.createResponse("Student with id:" + id + " not found", false));
|
||||||
return notFound(Util.createResponse("Student with id:"+id+" not found",false));
|
|
||||||
}
|
}
|
||||||
JsonNode jsonObjects=Json.toJson(student);
|
JsonNode jsonObjects = Json.toJson(StudentStore.getInstance().getStudent(id));
|
||||||
return ok(Util.createResponse(jsonObjects,true));
|
return ok(Util.createResponse(jsonObjects, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result listStudents() {
|
public Result listStudents() {
|
||||||
Set<Student> result=StudentStore.getInstance().getAllStudents();
|
Set<Student> result = StudentStore.getInstance().getAllStudents();
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
JsonNode jsonData=mapper.convertValue(result, JsonNode.class);
|
JsonNode jsonData = mapper.convertValue(result, JsonNode.class);
|
||||||
return ok(Util.createResponse(jsonData,true));
|
return ok(Util.createResponse(jsonData, true));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Result delete(int id) {
|
public Result delete(int id) {
|
||||||
boolean status=StudentStore.getInstance().deleteStudent(id);
|
if (!StudentStore.getInstance().deleteStudent(id)) {
|
||||||
if(!status){
|
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,16 +1,16 @@
|
||||||
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