[JAVA-13854] Half list moved (#12598)
* [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>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package model;
|
||||
|
||||
public class Student {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private int age;
|
||||
private int id;
|
||||
|
||||
public Student() {
|
||||
}
|
||||
|
||||
public Student(String firstName, String lastName, int age, int id) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.age = age;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package store;
|
||||
|
||||
import model.Student;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class StudentStore {
|
||||
private Map<Integer, Student> students = new HashMap<>();
|
||||
|
||||
public Optional<Student> addStudent(Student student) {
|
||||
int id = students.size();
|
||||
student.setId(id);
|
||||
students.put(id, student);
|
||||
return Optional.ofNullable(student);
|
||||
}
|
||||
|
||||
public Optional<Student> getStudent(int id) {
|
||||
return Optional.ofNullable(students.get(id));
|
||||
}
|
||||
|
||||
public Set<Student> getAllStudents() {
|
||||
return new HashSet<>(students.values());
|
||||
}
|
||||
|
||||
public Optional<Student> updateStudent(Student student) {
|
||||
int id = student.getId();
|
||||
if (students.containsKey(id)) {
|
||||
students.put(id, student);
|
||||
return Optional.ofNullable(student);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public boolean deleteStudent(int id) {
|
||||
return students.remove(id) != null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import play.libs.Json;
|
||||
|
||||
public class Util {
|
||||
public static ObjectNode createResponse(Object response, boolean ok) {
|
||||
ObjectNode result = Json.newObject();
|
||||
result.put("isSuccessful", ok);
|
||||
if (response instanceof String) {
|
||||
result.put("body", (String) response);
|
||||
} else {
|
||||
result.putPOJO("body", response);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user