Skip to content

Commit 0dc8119

Browse files
author
Grzegorz Piwowarek
committed
Refactor Play example
1 parent 1ecb726 commit 0dc8119

4 files changed

Lines changed: 80 additions & 68 deletions

File tree

‎play-framework/student-api/app/controllers/AsyncController.java‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package controllers;
22

33
import akka.actor.ActorSystem;
4+
45
import javax.inject.*;
6+
57
import play.*;
68
import play.mvc.*;
9+
710
import java.util.concurrent.Executor;
811
import java.util.concurrent.CompletableFuture;
912
import java.util.concurrent.CompletionStage;
1013
import java.util.concurrent.TimeUnit;
14+
1115
import scala.concurrent.duration.Duration;
1216
import scala.concurrent.ExecutionContextExecutor;
1317

@@ -17,11 +21,11 @@
1721
* asynchronously delay sending a response for 1 second.
1822
*
1923
* @param actorSystem We need the {@link ActorSystem}'s
20-
* {@link Scheduler} to run code after a delay.
21-
* @param exec We need a Java {@link Executor} to apply the result
22-
* of the {@link CompletableFuture} and a Scala
23-
* {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
24-
* An {@link ExecutionContextExecutor} implements both interfaces.
24+
* {@link Scheduler} to run code after a delay.
25+
* @param exec We need a Java {@link Executor} to apply the result
26+
* of the {@link CompletableFuture} and a Scala
27+
* {@link ExecutionContext} so we can use the Akka {@link Scheduler}.
28+
* An {@link ExecutionContextExecutor} implements both interfaces.
2529
*/
2630
@Singleton
2731
public class AsyncController extends Controller {
@@ -31,14 +35,14 @@ public class AsyncController extends Controller {
3135

3236
@Inject
3337
public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor exec) {
34-
this.actorSystem = actorSystem;
35-
this.exec = exec;
38+
this.actorSystem = actorSystem;
39+
this.exec = exec;
3640
}
3741

3842
/**
3943
* An action that returns a plain text message after a delay
4044
* of 1 second.
41-
*
45+
* <p>
4246
* The configuration in the <code>routes</code> file means that this method
4347
* will be called when the application receives a <code>GET</code> request with
4448
* a path of <code>/message</code>.
@@ -50,9 +54,9 @@ public CompletionStage<Result> message() {
5054
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
5155
CompletableFuture<String> future = new CompletableFuture<>();
5256
actorSystem.scheduler().scheduleOnce(
53-
Duration.create(time, timeUnit),
54-
() -> future.complete("Hi!"),
55-
exec
57+
Duration.create(time, timeUnit),
58+
() -> future.complete("Hi!"),
59+
exec
5660
);
5761
return future;
5862
}
Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,63 @@
11
package controllers;
2-
import models.*;
3-
import util.*;
4-
import play.mvc.*;
5-
import play.libs.Json;
6-
import play.libs.Json.*;
7-
import com.fasterxml.jackson.databind.JsonNode;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
84
import com.fasterxml.jackson.databind.ObjectMapper;
9-
import java.util.List;
5+
import models.Student;
6+
import models.StudentStore;
7+
import play.libs.Json;
8+
import play.mvc.Controller;
9+
import play.mvc.Result;
10+
import util.Util;
11+
12+
import java.util.Set;
1013

1114
public class StudentController extends Controller {
1215
public Result create() {
13-
JsonNode json = request().body().asJson();
14-
if(json == null)
15-
return badRequest(Util.createResponse("Expecting Json data",false));
16-
Student student=StudentStore.getInstance().addStudent((Student)Json.fromJson(json,Student.class));
17-
JsonNode jsonObject=Json.toJson(student);
18-
return created(Util.createResponse(jsonObject,true));
16+
JsonNode json = request().body().asJson();
17+
if (json == null) {
18+
return badRequest(Util.createResponse("Expecting Json data", false));
19+
}
20+
Student student = StudentStore.getInstance().addStudent(Json.fromJson(json, Student.class));
21+
JsonNode jsonObject = Json.toJson(student);
22+
return created(Util.createResponse(jsonObject, true));
1923
}
20-
public Result update() {
24+
25+
public Result update() {
2126
JsonNode json = request().body().asJson();
22-
if(json == null)
23-
return badRequest(Util.createResponse("Expecting Json data",false));
24-
Student student=StudentStore.getInstance().updateStudent((Student)Json.fromJson(json,Student.class));
25-
if(student==null){
26-
return notFound(Util.createResponse("Student not found",false));
27-
}
28-
29-
JsonNode jsonObject=Json.toJson(student);
30-
return ok(Util.createResponse(jsonObject,true));
27+
if (json == null) {
28+
return badRequest(Util.createResponse("Expecting Json data", false));
29+
}
30+
Student student = StudentStore.getInstance().updateStudent(Json.fromJson(json, Student.class));
31+
if (student == null) {
32+
return notFound(Util.createResponse("Student not found", false));
33+
}
34+
35+
JsonNode jsonObject = Json.toJson(student);
36+
return ok(Util.createResponse(jsonObject, true));
3137
}
32-
public Result retrieve(int id) {
33-
Student student=StudentStore.getInstance().getStudent(id);
34-
if(student==null){
35-
return notFound(Util.createResponse("Student with id:"+id+" not found",false));
36-
}
37-
JsonNode jsonObjects=Json.toJson(student);
38-
return ok(Util.createResponse(jsonObjects,true));
38+
39+
public Result retrieve(int id) {
40+
if (StudentStore.getInstance().getStudent(id) == null) {
41+
return notFound(Util.createResponse("Student with id:" + id + " not found", false));
42+
}
43+
JsonNode jsonObjects = Json.toJson(StudentStore.getInstance().getStudent(id));
44+
return ok(Util.createResponse(jsonObjects, true));
3945
}
40-
public Result listStudents() {
41-
Set<Student> result=StudentStore.getInstance().getAllStudents();
42-
ObjectMapper mapper = new ObjectMapper();
4346

44-
JsonNode jsonData=mapper.convertValue(result, JsonNode.class);
45-
return ok(Util.createResponse(jsonData,true));
47+
public Result listStudents() {
48+
Set<Student> result = StudentStore.getInstance().getAllStudents();
49+
ObjectMapper mapper = new ObjectMapper();
50+
51+
JsonNode jsonData = mapper.convertValue(result, JsonNode.class);
52+
return ok(Util.createResponse(jsonData, true));
4653

4754
}
48-
public Result delete(int id) {
49-
boolean status=StudentStore.getInstance().deleteStudent(id);
50-
if(!status){
51-
return notFound(Util.createResponse("Student with id:"+id+" not found",false));
52-
}
53-
return ok(Util.createResponse("Student with id:"+id+" deleted",true));
55+
56+
public Result delete(int id) {
57+
if (!StudentStore.getInstance().deleteStudent(id)) {
58+
return notFound(Util.createResponse("Student with id:" + id + " not found", false));
59+
}
60+
return ok(Util.createResponse("Student with id:" + id + " deleted", true));
5461
}
5562

5663
}

‎play-framework/student-api/app/models/StudentStore.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package models;
2-
import java.util.ArrayList;
2+
33
import java.util.HashMap;
44
import java.util.HashSet;
55
import java.util.Map;
@@ -10,13 +10,14 @@ public class StudentStore {
1010
private Map<Integer, Student> students = new HashMap<>();
1111

1212
public static StudentStore getInstance() {
13-
if (instance == null)
13+
if (instance == null) {
1414
instance = new StudentStore();
15+
}
1516
return instance;
1617
}
1718

1819
public Student addStudent(Student student) {
19-
int id = students.size() + 1;
20+
int id = students.size();
2021
student.setId(id);
2122
students.put(id, student);
2223
return student;
@@ -31,7 +32,7 @@ public Set<Student> getAllStudents() {
3132
}
3233

3334
public Student updateStudent(Student student) {
34-
int id=student.getId();
35+
int id = student.getId();
3536
if (students.containsKey(id)) {
3637
students.put(id, student);
3738
return student;
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package util;
2-
import com.fasterxml.jackson.databind.node.ObjectNode;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.node.ObjectNode;
35
import play.libs.Json;
4-
import play.libs.Json.*;
5-
import com.fasterxml.jackson.databind.JsonNode;
66

7-
public class Util{
8-
public static ObjectNode createResponse(Object response,boolean ok){
9-
ObjectNode result = Json.newObject();
10-
result.put("isSuccessfull", ok);
11-
if(response instanceof String)
12-
result.put("body",(String)response);
13-
else result.put("body",(JsonNode)response);
7+
public class Util {
8+
public static ObjectNode createResponse(Object response, boolean ok) {
9+
ObjectNode result = Json.newObject();
10+
result.put("isSuccessfull", ok);
11+
if (response instanceof String)
12+
result.put("body", (String) response);
13+
else result.put("body", (JsonNode) response);
1414

15-
return result;
16-
}
15+
return result;
16+
}
1717
}

0 commit comments

Comments
 (0)