Commit 2d53bd6a authored by Fjen Undso's avatar Fjen Undso
Browse files

QuestionController: delete old answers of same owner when a new one arrives

parent 514fc22c
package controllers;
import java.util.List;
import models.QuestionAnswer;
import models.Session;
import play.libs.Json;
......@@ -16,7 +18,7 @@ public class QuestionController extends Controller {
/**
* Creates a new {@link QuestionAnswer} for a {@link Session} from the
* request body
* request body and deletes the old answers from the same owner
*
* @param sid
* the ID of a Session
......@@ -35,6 +37,13 @@ public class QuestionController extends Controller {
JsonNode json = request().body().asJson();
QuestionAnswer answer = Json.fromJson(json, QuestionAnswer.class);
if (!answer.owner.isEmpty()) {
// Delete old answer(s)
List<QuestionAnswer> oldAnswers = QuestionAnswer
.findFromOwner(answer.owner);
for (QuestionAnswer qa : oldAnswers) {
qa.delete();
}
QuestionAnswer inserted = new QuestionAnswer(session, answer.owner,
answer.answer);
session.addQuestionAnswer(inserted);
......
......@@ -110,6 +110,7 @@ public class VoteController extends Controller {
sAll.value = v.value;
break;
case REQUEST:
// TODO: consider only last 5minutes?
sRequests.value += v.value.compareTo(0);
break;
default:
......
package models;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
......@@ -52,6 +53,10 @@ public class QuestionAnswer extends Model {
public static Finder<Long, QuestionAnswer> find = new Finder<Long, QuestionAnswer>(
Long.class, QuestionAnswer.class);
public static List<QuestionAnswer> findFromOwner(String owner) {
return QuestionAnswer.find.where().eq("owner", owner).findList();
}
public QuestionAnswer(Session session, String owner, Answer answer) {
this.session = session;
this.owner = owner;
......
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