Refactor Play example

This commit is contained in:
Grzegorz Piwowarek 2016-10-04 17:54:59 +02:00
parent 1ecb726d7e
commit 0dc8119722
4 changed files with 82 additions and 70 deletions

View File

@ -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;
@ -38,7 +42,7 @@ public class AsyncController extends Controller {
/**
* 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>.

View File

@ -1,27 +1,33 @@
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.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)
if (json == null) {
return badRequest(Util.createResponse("Expecting Json data", false));
Student student=StudentStore.getInstance().addStudent((Student)Json.fromJson(json,Student.class));
}
Student student = StudentStore.getInstance().addStudent(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)
if (json == null) {
return badRequest(Util.createResponse("Expecting Json data", false));
Student student=StudentStore.getInstance().updateStudent((Student)Json.fromJson(json,Student.class));
}
Student student = StudentStore.getInstance().updateStudent(Json.fromJson(json, Student.class));
if (student == null) {
return notFound(Util.createResponse("Student not found", false));
}
@ -29,14 +35,15 @@ public class StudentController extends Controller {
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){
if (StudentStore.getInstance().getStudent(id) == null) {
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));
}
public Result listStudents() {
Set<Student> result = StudentStore.getInstance().getAllStudents();
ObjectMapper mapper = new ObjectMapper();
@ -45,9 +52,9 @@ public class StudentController extends Controller {
return ok(Util.createResponse(jsonData, true));
}
public Result delete(int id) {
boolean status=StudentStore.getInstance().deleteStudent(id);
if(!status){
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));

View File

@ -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;

View File

@ -1,8 +1,8 @@
package util;
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) {