Move routing-in-play -> play-framework
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* This controller contains an action that demonstrates how to write
|
||||
* simple asynchronous code in a controller. It uses a timer to
|
||||
* 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.
|
||||
*/
|
||||
@Singleton
|
||||
public class AsyncController extends Controller {
|
||||
|
||||
private final ActorSystem actorSystem;
|
||||
private final ExecutionContextExecutor exec;
|
||||
|
||||
@Inject
|
||||
public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor 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>.
|
||||
*/
|
||||
public CompletionStage<Result> message() {
|
||||
return getFutureMessage(1, TimeUnit.SECONDS).thenApplyAsync(Results::ok, exec);
|
||||
}
|
||||
|
||||
private CompletionStage<String> getFutureMessage(long time, TimeUnit timeUnit) {
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
actorSystem.scheduler().scheduleOnce(
|
||||
Duration.create(time, timeUnit),
|
||||
() -> future.complete("Hi!"),
|
||||
exec
|
||||
);
|
||||
return future;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package controllers;
|
||||
|
||||
import javax.inject.*;
|
||||
import play.*;
|
||||
import play.mvc.*;
|
||||
|
||||
import services.Counter;
|
||||
|
||||
/**
|
||||
* This controller demonstrates how to use dependency injection to
|
||||
* bind a component into a controller class. The class contains an
|
||||
* action that shows an incrementing count to users. The {@link Counter}
|
||||
* object is injected by the Guice dependency injection system.
|
||||
*/
|
||||
@Singleton
|
||||
public class CountController extends Controller {
|
||||
|
||||
private final Counter counter;
|
||||
|
||||
@Inject
|
||||
public CountController(Counter counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
/**
|
||||
* An action that responds with the {@link Counter}'s current
|
||||
* count. The result is plain text. This action is mapped to
|
||||
* <code>GET</code> requests with a path of <code>/count</code>
|
||||
* requests by an entry in the <code>routes</code> config file.
|
||||
*/
|
||||
public Result count() {
|
||||
return ok(Integer.toString(counter.nextCount()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package controllers;
|
||||
|
||||
import play.mvc.*;
|
||||
|
||||
import views.html.*;
|
||||
|
||||
/**
|
||||
* This controller contains an action to handle HTTP requests
|
||||
* to the application's home page.
|
||||
*/
|
||||
public class HomeController extends Controller {
|
||||
|
||||
/**
|
||||
* An action that renders an HTML page with a welcome message.
|
||||
* 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>/</code>.
|
||||
*/
|
||||
public Result index() {
|
||||
return ok(index.render("Your new application is ready."));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package controllers;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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(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(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) {
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user