Commit 03be86b6 authored by Fjen Undso's avatar Fjen Undso
Browse files

add json checks and reduce depth in controllermethods

parent 09151924
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 models.Vote;
...@@ -11,20 +13,24 @@ public class QuestionController extends Controller { ...@@ -11,20 +13,24 @@ public class QuestionController extends Controller {
public static Result createAnswer(String sid) { public static Result createAnswer(String sid) {
Session session = Session.find.byId(sid); Session session = Session.find.byId(sid);
if (session != null) { if (session == null) {
QuestionAnswer answer = Json.fromJson(request().body().asJson(), return notFound("session not found");
QuestionAnswer.class); }
JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
QuestionAnswer answer = Json.fromJson(json, QuestionAnswer.class);
if (!answer.owner.isEmpty()) { if (!answer.owner.isEmpty()) {
QuestionAnswer inserted = new QuestionAnswer(session, QuestionAnswer inserted = new QuestionAnswer(session, answer.owner,
answer.owner, answer.answer); answer.answer);
session.addQuestionAnswer(inserted); session.addQuestionAnswer(inserted);
session.save(); session.save();
return created(Json.toJson(inserted)); return created(Json.toJson(inserted));
} else { } else {
return badRequest("owner missing"); return badRequest("owner missing");
} }
} else {
return notFound("session not found");
}
} }
} }
...@@ -2,6 +2,8 @@ package controllers; ...@@ -2,6 +2,8 @@ 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.Controller; import play.mvc.Controller;
...@@ -27,8 +29,12 @@ public class SessionController extends Controller { ...@@ -27,8 +29,12 @@ public class SessionController extends Controller {
} }
public static Result createSession() { public static Result createSession() {
Session session = Json.fromJson(request().body().asJson(), JsonNode json = request().body().asJson();
Session.class); if (json == null) {
return badRequest("Expecting Json data");
}
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 inserted = new Session(session.owner, session.name);
inserted.save(); inserted.save();
...@@ -39,10 +45,17 @@ public class SessionController extends Controller { ...@@ -39,10 +45,17 @@ public class SessionController extends Controller {
} }
public static Result updateSession(String sid) { public static Result updateSession(String sid) {
Session session = Json.fromJson(request().body().asJson(), JsonNode json = request().body().asJson();
Session.class); if (json == null) {
return badRequest("Expecting Json data");
}
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");
}
if (sessionSaved.owner == session.owner) { if (sessionSaved.owner == session.owner) {
sessionSaved.name = session.name; sessionSaved.name = session.name;
sessionSaved.date = session.date; sessionSaved.date = session.date;
...@@ -51,46 +64,46 @@ public class SessionController extends Controller { ...@@ -51,46 +64,46 @@ public class SessionController extends Controller {
} else { } else {
return forbidden("wrong owner"); return forbidden("wrong owner");
} }
} else {
return notFound("session not found");
}
} }
public static Result deleteSession(String sid, String owner) { public static Result deleteSession(String sid, String owner) {
Session session = Session.find.byId(sid); Session session = Session.find.byId(sid);
if (session != null) { if (session == null) {
return notFound("session not found");
}
if (session.owner == owner) { if (session.owner == owner) {
session.delete(); session.delete();
return noContent(); return noContent();
} else { } else {
return forbidden("wrong owner"); return forbidden("wrong owner");
} }
} else {
return notFound("session not found");
}
} }
public static Result getVotes(String sid) { public static Result getVotes(String sid) {
Session session = Session.find.byId(sid); Session session = Session.find.byId(sid);
if (session != null) { if (session == null) {
return ok(Json.toJson(session.votes));
} else {
return notFound("session not found"); return notFound("session not found");
} else {
return ok(Json.toJson(session.votes));
} }
} }
public static Result getAnswers(String sid) { public static Result getAnswers(String sid) {
Session session = Session.find.byId(sid); Session session = Session.find.byId(sid);
if (session != null) { if (session == null) {
return ok(Json.toJson(session.questionAnswers));
} else {
return notFound("session not found"); return notFound("session not found");
} else {
return ok(Json.toJson(session.questionAnswers));
} }
} }
public static Result resetAnswers(String sid, String owner) { public static Result resetAnswers(String sid, String owner) {
Session session = Session.find.byId(sid); Session session = Session.find.byId(sid);
if (session != null) { if (session == null) {
return notFound("session not found");
}
if (session.owner == owner) { if (session.owner == owner) {
session.resetAnswers(); session.resetAnswers();
session.save(); session.save();
...@@ -98,8 +111,5 @@ public class SessionController extends Controller { ...@@ -98,8 +111,5 @@ public class SessionController extends Controller {
} else { } else {
return forbidden("wrong owner"); return forbidden("wrong owner");
} }
} else {
return notFound("session not found");
}
} }
} }
\ No newline at end of file
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;
...@@ -10,19 +12,23 @@ public class VoteController extends Controller { ...@@ -10,19 +12,23 @@ public class VoteController extends Controller {
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) {
Vote vote = Json.fromJson(request().body().asJson(), Vote.class); return notFound("session not found");
}
JsonNode json = request().body().asJson();
if (json == null) {
return badRequest("Expecting Json data");
}
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 inserted = new Vote(session, vote.owner, vote.type, vote.vote);
vote.vote);
session.addVote(inserted); session.addVote(inserted);
session.save(); session.save();
return created(Json.toJson(inserted)); return created(Json.toJson(inserted));
} else { } else {
return badRequest("owner missing"); return badRequest("owner missing");
} }
} else {
return notFound("session not found");
}
} }
} }
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