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

Models: cleanup and add more session methods

parent 77413838
package controllers;
import play.mvc.Result;
import java.util.List;
import models.Session;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
public class SessionController extends Controller {
public static Result getSessions(String owner) {
return null;
public static Result getSessions() {
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));
}
public static Result getSession(String sid) {
......
......@@ -16,7 +16,7 @@ import com.avaje.ebean.annotation.EnumValue;
public class QuestionAnswer extends Model {
@Id
public String id;
public Long id;
@ManyToOne
@Constraints.Required
......@@ -45,7 +45,8 @@ public class QuestionAnswer extends Model {
D,
}
public static Finder find = new Finder(Long.class, QuestionAnswer.class);
public static Finder<Long, QuestionAnswer> find = new Finder<Long, QuestionAnswer>(
Long.class, QuestionAnswer.class);
public QuestionAnswer(Session session, String owner, Answer answer) {
this.session = session;
......
......@@ -10,6 +10,8 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.apache.commons.lang3.RandomStringUtils;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
......@@ -18,7 +20,7 @@ import play.db.ebean.Model;
public class Session extends Model {
@Id
@Constraints.Max(6)
@Constraints.MinLength(5)
public String id;
@Constraints.Required
......@@ -43,18 +45,33 @@ public class Session extends Model {
@Column(nullable = true)
public List<QuestionAnswer> questionAnswers = new ArrayList<>();
public static Finder find = new Finder(Long.class, Session.class);
public static Finder<String, Session> find = new Finder<String, Session>(
String.class, Session.class);
public static List<Session> findFromOwner(String owner) {
return Session.find.where().eq("owner", owner).findList();
}
public Session(String owner, String name) {
this.owner = owner;
this.name = name;
this.open = true;
// crappy easy readable random id
this.id = RandomStringUtils.randomAlphanumeric(5).toUpperCase();
}
public void resetAnswers() {
this.date = new Date();
for (QuestionAnswer a : this.questionAnswers) {
a.delete();
}
}
public void addVote(Vote v) {
this.votes.add(v);
}
public void resetAnswers() {
this.date = new Date();
for (QuestionAnswer a: this.questionAnswers) {
a.delete();
}
}
public void addQuestionAnswer(QuestionAnswer qa) {
this.questionAnswers.add(qa);
}
}
......@@ -17,7 +17,7 @@ import com.avaje.ebean.annotation.EnumValue;
public class Vote extends Model {
@Id
public String id;
public Long id;
@ManyToOne
@Constraints.Required
......@@ -49,7 +49,8 @@ public class Vote extends Model {
NOREQUEST,
}
public static Finder find = new Finder(Long.class, Vote.class);
public static Finder<Long, Vote> find = new Finder<Long, Vote>(Long.class,
Vote.class);
public Vote(Session session, String owner, Type type, Integer vote) {
this.session = session;
......
......@@ -3,7 +3,8 @@
# ~~~~
# Session
GET /sessions/byowner/:owner controllers.SessionController.getSessions(owner: String)
GET /sessions controllers.SessionController.getSessions
GET /sessions/byowner/:owner controllers.SessionController.getSessionsByOwner(owner: String)
GET /sessions/:sid controllers.SessionController.getSession(sid: String)
POST /sessions controllers.SessionController.createSession
PUT /sessions/:sid controllers.SessionController.updateSession(sid: String)
......
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