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; package controllers;
import com.fasterxml.jackson.databind.JsonNode;
import models.QuestionAnswer; import models.QuestionAnswer;
import models.Session; import models.Session;
import models.Vote;
import play.libs.Json; import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller; import play.mvc.Controller;
import play.mvc.Result; import play.mvc.Result;
import com.fasterxml.jackson.databind.JsonNode;
public class QuestionController extends Controller { public class QuestionController extends Controller {
@BodyParser.Of(BodyParser.Json.class)
public static Result createAnswer(String sid) { public static Result createAnswer(String sid) {
// TODO: open check
Session session = Session.find.byId(sid); Session session = Session.find.byId(sid);
if (session == null) { if (session == null) {
return notFound("session not found"); return notFound("session not found");
...@@ -22,10 +22,6 @@ public class QuestionController extends Controller { ...@@ -22,10 +22,6 @@ public class QuestionController extends Controller {
} }
JsonNode json = request().body().asJson(); JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
QuestionAnswer answer = Json.fromJson(json, QuestionAnswer.class); QuestionAnswer answer = Json.fromJson(json, QuestionAnswer.class);
if (!answer.owner.isEmpty()) { if (!answer.owner.isEmpty()) {
QuestionAnswer inserted = new QuestionAnswer(session, answer.owner, QuestionAnswer inserted = new QuestionAnswer(session, answer.owner,
......
...@@ -2,13 +2,14 @@ package controllers; ...@@ -2,13 +2,14 @@ package controllers;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import models.Session; import models.Session;
import play.libs.Json; import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller; import play.mvc.Controller;
import play.mvc.Result; import play.mvc.Result;
import com.fasterxml.jackson.databind.JsonNode;
public class SessionController extends Controller { public class SessionController extends Controller {
public static Result getSessions() { public static Result getSessions() {
...@@ -28,42 +29,37 @@ public class SessionController extends Controller { ...@@ -28,42 +29,37 @@ public class SessionController extends Controller {
.toJson(session)); .toJson(session));
} }
@BodyParser.Of(BodyParser.Json.class)
public static Result createSession() { public static Result createSession() {
JsonNode json = request().body().asJson(); JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
Session session = Json.fromJson(json, Session.class); Session session = Json.fromJson(json, Session.class);
if (!(session.name.isEmpty() || session.owner.isEmpty())) { if (!(session.name.isEmpty() || session.owner.isEmpty())) {
Session inserted = new Session(session.owner, session.name); Session sessionSaved = new Session(session.owner, session.name,
inserted.save(); session.open, session.date);
return created(Json.toJson(inserted)); sessionSaved.save();
return created(Json.toJson(sessionSaved));
} else { } else {
return badRequest("name or owner missing"); return badRequest("name or owner missing");
} }
} }
@BodyParser.Of(BodyParser.Json.class)
public static Result updateSession(String sid) { public static Result updateSession(String sid) {
JsonNode json = request().body().asJson(); JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
Session session = Json.fromJson(json, Session.class); Session session = Json.fromJson(json, Session.class);
Session sessionSaved = Session.find.byId(sid); Session sessionSaved = Session.find.byId(sid);
if (sessionSaved == null) { if (sessionSaved == null) {
return notFound("session not found"); return notFound("session not found");
} }
if (!sessionSaved.owner.equals(session.owner)) {
return forbidden("wrong owner");
}
if (sessionSaved.owner.equals(session.owner)) {
sessionSaved.name = session.name; sessionSaved.name = session.name;
sessionSaved.date = session.date; sessionSaved.date = session.date;
sessionSaved.open = session.open;
sessionSaved.save(); sessionSaved.save();
return noContent(); return ok(Json.toJson(sessionSaved));
} else {
return forbidden("wrong owner");
}
} }
public static Result deleteSession(String sid, String owner) { public static Result deleteSession(String sid, String owner) {
......
package controllers; package controllers;
import com.fasterxml.jackson.databind.JsonNode;
import models.Session; import models.Session;
import models.Vote; import models.Vote;
import play.libs.Json; import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller; import play.mvc.Controller;
import play.mvc.Result; import play.mvc.Result;
import com.fasterxml.jackson.databind.JsonNode;
public class VoteController extends Controller { public class VoteController extends Controller {
@BodyParser.Of(BodyParser.Json.class)
public static Result createVote(String sid) { public static Result createVote(String sid) {
Session session = Session.find.byId(sid); Session session = Session.find.byId(sid);
if (session == null) { if (session == null) {
...@@ -20,10 +22,6 @@ public class VoteController extends Controller { ...@@ -20,10 +22,6 @@ public class VoteController extends Controller {
} }
JsonNode json = request().body().asJson(); JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
Vote vote = Json.fromJson(json, Vote.class); Vote vote = Json.fromJson(json, Vote.class);
if (!vote.owner.isEmpty()) { if (!vote.owner.isEmpty()) {
Vote inserted = new Vote(session, vote.owner, vote.type, vote.vote); Vote inserted = new Vote(session, vote.owner, vote.type, vote.vote);
......
...@@ -64,6 +64,12 @@ public class Session extends Model { ...@@ -64,6 +64,12 @@ public class Session extends Model {
this.id = RandomStringUtils.randomAlphanumeric(5).toUpperCase(); 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() { public void resetAnswers() {
this.date = new Date(); this.date = new Date();
for (QuestionAnswer a : this.questionAnswers) { 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