Commit 9ed60f4a authored by Fjen Undso's avatar Fjen Undso
Browse files

Controllers: use play json body check. Respect date and open in sessionchanges

parent a09e441b
package controllers;
import com.fasterxml.jackson.databind.JsonNode;
import models.QuestionAnswer;
import models.Session;
import models.Vote;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;
import com.fasterxml.jackson.databind.JsonNode;
public class QuestionController extends Controller {
@BodyParser.Of(BodyParser.Json.class)
public static Result createAnswer(String sid) {
// TODO: open check
Session session = Session.find.byId(sid);
if (session == null) {
return notFound("session not found");
......@@ -22,10 +22,6 @@ public class QuestionController extends Controller {
}
JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
QuestionAnswer answer = Json.fromJson(json, QuestionAnswer.class);
if (!answer.owner.isEmpty()) {
QuestionAnswer inserted = new QuestionAnswer(session, answer.owner,
......
......@@ -2,13 +2,14 @@ package controllers;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import models.Session;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;
import com.fasterxml.jackson.databind.JsonNode;
public class SessionController extends Controller {
public static Result getSessions() {
......@@ -28,42 +29,37 @@ public class SessionController extends Controller {
.toJson(session));
}
@BodyParser.Of(BodyParser.Json.class)
public static Result createSession() {
JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
Session session = Json.fromJson(json, Session.class);
if (!(session.name.isEmpty() || session.owner.isEmpty())) {
Session inserted = new Session(session.owner, session.name);
inserted.save();
return created(Json.toJson(inserted));
Session sessionSaved = new Session(session.owner, session.name,
session.open, session.date);
sessionSaved.save();
return created(Json.toJson(sessionSaved));
} else {
return badRequest("name or owner missing");
}
}
@BodyParser.Of(BodyParser.Json.class)
public static Result updateSession(String sid) {
JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
Session session = Json.fromJson(json, Session.class);
Session sessionSaved = Session.find.byId(sid);
if (sessionSaved == null) {
return notFound("session not found");
}
if (sessionSaved.owner.equals(session.owner)) {
sessionSaved.name = session.name;
sessionSaved.date = session.date;
sessionSaved.save();
return noContent();
} else {
if (!sessionSaved.owner.equals(session.owner)) {
return forbidden("wrong owner");
}
sessionSaved.name = session.name;
sessionSaved.date = session.date;
sessionSaved.open = session.open;
sessionSaved.save();
return ok(Json.toJson(sessionSaved));
}
public static Result deleteSession(String sid, String owner) {
......
package controllers;
import com.fasterxml.jackson.databind.JsonNode;
import models.Session;
import models.Vote;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
import play.mvc.Result;
import com.fasterxml.jackson.databind.JsonNode;
public class VoteController extends Controller {
@BodyParser.Of(BodyParser.Json.class)
public static Result createVote(String sid) {
Session session = Session.find.byId(sid);
if (session == null) {
......@@ -20,10 +22,6 @@ public class VoteController extends Controller {
}
JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
Vote vote = Json.fromJson(json, Vote.class);
if (!vote.owner.isEmpty()) {
Vote inserted = new Vote(session, vote.owner, vote.type, vote.vote);
......
......@@ -64,6 +64,12 @@ public class Session extends Model {
this.id = RandomStringUtils.randomAlphanumeric(5).toUpperCase();
}
public Session(String owner, String name, Boolean open, Date date) {
this(owner, name);
this.open = open;
this.date = date;
}
public void resetAnswers() {
this.date = new Date();
for (QuestionAnswer a : this.questionAnswers) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment