Commit 19573385 authored by Fjen Undso's avatar Fjen Undso
Browse files

SessionController: add method implementations

not tested. can be buggy or wrong
parent 06a50233
......@@ -3,7 +3,6 @@ package controllers;
import java.util.List;
import models.Session;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
......@@ -11,40 +10,96 @@ import play.mvc.Result;
public class SessionController extends Controller {
public static Result getSessions() {
List<Session> sessions = Session.find.all();
return ok(Json.toJson(sessions));
List<Session> sessions = Session.find.all();
return ok(Json.toJson(sessions));
}
public static Result getSessionsByOwner(String owner) {
List<Session> sessions = Session.findFromOwner(owner);
return ok(Json.toJson(sessions));
return sessions.isEmpty() ? notFound("no sessions") : ok(Json
.toJson(sessions));
}
public static Result getSession(String sid) {
return null;
Session session = Session.find.byId(sid);
return session == null ? notFound("session not found") : ok(Json
.toJson(session));
}
public static Result createSession() {
return null;
Session session = Json.fromJson(request().body().asJson(),
Session.class);
if (!(session.name.isEmpty() || session.owner.isEmpty())) {
Session inserted = new Session(session.owner, session.name);
inserted.save();
return created(Json.toJson(inserted));
} else {
return badRequest("name or owner missing");
}
}
public static Result updateSession(String sid) {
return null;
Session session = Json.fromJson(request().body().asJson(),
Session.class);
Session sessionSaved = Session.find.byId(sid);
if (sessionSaved != null) {
if (sessionSaved.owner == session.owner) {
sessionSaved.name = session.name;
sessionSaved.date = session.date;
sessionSaved.save();
return ok();
} else {
return forbidden("wrong owner");
}
} else {
return notFound("session not found");
}
}
public static Result deleteSession(String sid, String owner) {
return null;
Session session = Session.find.byId(sid);
if (session != null) {
if (session.owner == owner) {
session.delete();
return ok();
} else {
return forbidden("wrong owner");
}
} else {
return notFound("session not found");
}
}
public static Result getVotes(String sid) {
return null;
Session session = Session.find.byId(sid);
if (session != null) {
return ok(Json.toJson(session.votes));
} else {
return notFound("session not found");
}
}
public static Result getAnswers(String sid) {
return null;
Session session = Session.find.byId(sid);
if (session != null) {
return ok(Json.toJson(session.questionAnswers));
} else {
return notFound("session not found");
}
}
public static Result resetAnswers(String sid, String owner) {
return null;
Session session = Session.find.byId(sid);
if (session != null) {
if (session.owner == owner) {
session.resetAnswers();
session.save();
return ok();
} else {
return forbidden("wrong owner");
}
} else {
return notFound("session not found");
}
}
}
\ No newline at end of file
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