BAEL-3059: Update Play Framework (#7674)
* Implementation of Introduction to Play Framework in Java * Implementation of Introduction to Play Framework in Java * Implementation of Introduction to Play Framework in Java * Implementation of the tutorial for Introduction to Play Framework. * Updated code for Play Routing to 2.7.3 * Updated code for Play Routing to 2.7.3 * Initial Commit * Added request object to create end point * Finished implementing student-api * Finished implementing student-api * Implementation of the tutorial for Introduction to Play Framework. * Updated test names * Removed Lombok dependency * Corrected isSuccessful spelling and removed the +1 on student ids
This commit is contained in:
committed by
KevinGilmore
parent
0d648321dc
commit
5bce8b22a1
@@ -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