Commit 6eb37cd1 authored by Fjen Undso's avatar Fjen Undso
Browse files

add VoteStats. incomplete aggregation

parent e3e278c5
package controllers;
import java.util.ArrayList;
import java.util.List;
import models.Session;
import models.Vote;
import models.VoteStats;
import play.libs.Json;
import play.mvc.BodyParser;
import play.mvc.Controller;
......@@ -34,7 +38,7 @@ public class VoteController extends Controller {
JsonNode json = request().body().asJson();
Vote vote = Json.fromJson(json, Vote.class);
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.value);
session.addVote(inserted);
session.save();
return created(Json.toJson(inserted)); // 201
......@@ -61,4 +65,53 @@ public class VoteController extends Controller {
return ok(Json.toJson(session.votes)); // 200
}
}
/**
* Gets aggregated statistics of {@link Vote}s for a {@link Session} as
* {@link VoteStats}
*
* @param sid
* the ID of a Session
* @return list of VoteStats
*/
public static Result getVoteStats(String sid) {
Session session = Session.find.byId(sid);
if (session == null) {
return notFound("session not found"); // 404
} else {
VoteStats sAll = new VoteStats(VoteStats.Type.ALL, 0);
VoteStats sSpeed = new VoteStats(VoteStats.Type.SPEED, 0);
VoteStats sUnderstandability = new VoteStats(
VoteStats.Type.UNDERSTANDABILITY, 0);
VoteStats sRequests = new VoteStats(VoteStats.Type.REQUEST, 0);
List<VoteStats> vsList = new ArrayList<VoteStats>();
vsList.add(sAll);
vsList.add(sSpeed);
vsList.add(sUnderstandability);
vsList.add(sRequests);
// generate statistics
for (Vote v : session.votes) {
switch (v.type) {
case SPEED:
// TODO: aggregate
sSpeed.value = v.value;
sAll.value = v.value;
break;
case UNDERSTANDABILITY:
// TODO: aggregate
sUnderstandability.value = v.value;
sAll.value = v.value;
break;
case REQUEST:
sRequests.value += v.value.compareTo(0);
break;
default:
break;
}
}
return ok(Json.toJson(vsList)); // 200
}
}
}
......@@ -34,7 +34,7 @@ public class Vote extends Model {
public Date date = new Date();
@Column(nullable = true)
public Integer vote;
public Integer value;
@Constraints.Required
public Type type;
......@@ -48,19 +48,16 @@ public class Vote extends Model {
@EnumValue("R")
REQUEST,
@EnumValue("N")
NOREQUEST,
}
public static Finder<Long, Vote> find = new Finder<Long, Vote>(Long.class,
Vote.class);
public Vote(Session session, String owner, Type type, Integer vote) {
public Vote(Session session, String owner, Type type, Integer value) {
this.session = session;
this.owner = owner;
this.type = type;
this.vote = vote;
this.value = value;
}
}
package models;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import com.avaje.ebean.annotation.EnumValue;
@Entity
public class VoteStats extends Model {
private static final long serialVersionUID = 8660259739658830119L;
@Id
public Long id;
@Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:ss")
public Date date = new Date();
@Column(nullable = true)
public Integer value;
@Constraints.Required
public Type type;
public enum Type {
@EnumValue("A")
ALL,
@EnumValue("S")
SPEED,
@EnumValue("U")
UNDERSTANDABILITY,
@EnumValue("R")
REQUEST,
}
public static Finder<Long, VoteStats> find = new Finder<Long, VoteStats>(Long.class,
VoteStats.class);
public VoteStats(Type type, Integer value) {
this.type = type;
this.value = value;
}
}
......@@ -18,4 +18,4 @@ POST /sessions/:sid/resetanswers/:owner controllers.QuestionController.reset
# Vote
GET /sessions/:sid/votes controllers.VoteController.getVotes(sid: String)
POST /sessions/:sid/votes controllers.VoteController.createVote(sid: String)
# TODO: aggregierte daten abfragen
\ No newline at end of file
GET /sessions/:sid/votestats controllers.VoteController.getVoteStats(sid: String)
\ 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