* [JAVA-13854] added parent module * [JAVA-13854] moved apache-tapestry(submodule) to web-modules(parent) * [JAVA-13854] moved bootique(submodule) to web-modules(parent) * [JAVA-13854] moved dropwizard(submodule) to web-modules(parent) * [JAVA-13854] moved blade(submodule) to web-modules(parent) * [JAVA-13854] moved java-lite(submodule) to web-modules(parent) * [JAVA-13854] moved jooby(submodule) to web-modules(parent) * [JAVA-13854] moved linkrest(submodule) to web-modules(parent) * [JAVA-13854] moved ninja(submodule) to web-modules(parent) * [JAVA-13854] moved ratpack(submodule) to web-modules(parent) * [JAVA-13854] moved resteasy(submodule) to web-modules(parent) * [JAVA-13854] moved restx(submodule) to web-modules(parent) * [JAVA-13854] moved spark-java(submodule) to web-modules(parent) * [JAVA-13854] moved vraptor(submodule) to web-modules(parent) * [JAVA-13854] delete modules that were moved * [JAVA-13854] * [JAVA-13854] * [JAVA-13854] delete ninja submodule + moved raml(submodule) to web-modules(parent) * [JAVA-13854] moved gwt(submodule) to web-modules(parent) * [JAVA-13854] moved jakarta-ee(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets(submodule) to web-modules(parent) * [JAVA-13854] moved javax-servlets-2(submodule) to web-modules(parent) * [JAVA-13854] moved jee-7(submodule) to web-modules(parent) * [JAVA-13854] moved play-framework(not a module) to web-modules * [JAVA-13854] fix failing test * [JAVA-13854] moved struts-2(submodule) to web-modules(parent) * [JAVA-13854] moved wicket(submodule) to web-modules(parent) * [JAVA-13854] deleted modules that were moved to web-modules * JAVA-13854 Removed moved modules from the main pom.xml Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com> Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
92 lines
3.6 KiB
Java
92 lines
3.6 KiB
Java
package controllers;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import model.Student;
|
|
import play.libs.Json;
|
|
import play.libs.concurrent.HttpExecutionContext;
|
|
import play.mvc.Controller;
|
|
import play.mvc.Http;
|
|
import play.mvc.Result;
|
|
import store.StudentStore;
|
|
import utils.Util;
|
|
|
|
import javax.inject.Inject;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.concurrent.CompletionStage;
|
|
|
|
import static java.util.concurrent.CompletableFuture.supplyAsync;
|
|
|
|
public class StudentController extends Controller {
|
|
private HttpExecutionContext ec;
|
|
private StudentStore studentStore;
|
|
|
|
@Inject
|
|
public StudentController(HttpExecutionContext ec, StudentStore studentStore) {
|
|
this.studentStore = studentStore;
|
|
this.ec = ec;
|
|
}
|
|
|
|
public CompletionStage<Result> create(Http.Request request) {
|
|
JsonNode json = request.body().asJson();
|
|
return supplyAsync(() -> {
|
|
if (json == null) {
|
|
return badRequest(Util.createResponse("Expecting Json data", false));
|
|
}
|
|
|
|
Optional<Student> studentOptional = studentStore.addStudent(Json.fromJson(json, Student.class));
|
|
return studentOptional.map(student -> {
|
|
JsonNode jsonObject = Json.toJson(student);
|
|
return created(Util.createResponse(jsonObject, true));
|
|
}).orElse(internalServerError(Util.createResponse("Could not create data.", false)));
|
|
}, ec.current());
|
|
}
|
|
|
|
public CompletionStage<Result> listStudents() {
|
|
return supplyAsync(() -> {
|
|
Set<Student> result = studentStore.getAllStudents();
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
JsonNode jsonData = mapper.convertValue(result, JsonNode.class);
|
|
return ok(Util.createResponse(jsonData, true));
|
|
}, ec.current());
|
|
}
|
|
|
|
public CompletionStage<Result> retrieve(int id) {
|
|
return supplyAsync(() -> {
|
|
final Optional<Student> studentOptional = studentStore.getStudent(id);
|
|
return studentOptional.map(student -> {
|
|
JsonNode jsonObjects = Json.toJson(student);
|
|
return ok(Util.createResponse(jsonObjects, true));
|
|
}).orElse(notFound(Util.createResponse("Student with id:" + id + " not found", false)));
|
|
}, ec.current());
|
|
}
|
|
|
|
public CompletionStage<Result> update(Http.Request request) {
|
|
JsonNode json = request.body().asJson();
|
|
return supplyAsync(() -> {
|
|
if (json == null) {
|
|
return badRequest(Util.createResponse("Expecting Json data", false));
|
|
}
|
|
Optional<Student> studentOptional = studentStore.updateStudent(Json.fromJson(json, Student.class));
|
|
return studentOptional.map(student -> {
|
|
if (student == null) {
|
|
return notFound(Util.createResponse("Student not found", false));
|
|
}
|
|
JsonNode jsonObject = Json.toJson(student);
|
|
return ok(Util.createResponse(jsonObject, true));
|
|
}).orElse(internalServerError(Util.createResponse("Could not create data.", false)));
|
|
}, ec.current());
|
|
}
|
|
|
|
public CompletionStage<Result> delete(int id) {
|
|
return supplyAsync(() -> {
|
|
boolean status = studentStore.deleteStudent(id);
|
|
if (!status) {
|
|
return notFound(Util.createResponse("Student with id:" + id + " not found", false));
|
|
}
|
|
return ok(Util.createResponse("Student with id:" + id + " deleted", true));
|
|
}, ec.current());
|
|
}
|
|
}
|