Skip to content
Snippets Groups Projects
Commit c37a8d17 authored by Julian Hochstetter's avatar Julian Hochstetter
Browse files

Task #4041: countAnswers

parent 8391fea8
No related merge requests found
......@@ -152,4 +152,31 @@ public class QuestionController extends AbstractController {
}
return answer;
}
/**
* returns a list of {@link Answer}s encoded as a JSON document for a given
* question id. In this case only {@link Answer} <tt>questionId</tt>,
* <tt>answerText</tt>, <tt>answerSubject</tt> and <tt>answerCount</tt>
* properties are set
*
* @param sessionKey
* Session Keyword to which the question belongs to
* @param questionId
* CouchDB Question ID for which the given answers should be
* retrieved
* @return List<{@link Answer}> or {@link NotFoundException}
* @throws NotFoundException
* if wrong session, wrong question or no answers was given
* @throws ForbiddenException
* if not logged in
*/
@RequestMapping(value="/session/{sessionKey}/question/{questionId}/answers", method=RequestMethod.GET)
@ResponseBody
public List<Answer> getAnswers(@PathVariable String sessionKey, @PathVariable String questionId, HttpServletResponse response) {
List<Answer> answers = questionService.getAnswers(sessionKey, questionId);
if(answers == null || answers.isEmpty()) {
throw new NotFoundException();
}
return answers;
}
}
......@@ -717,7 +717,40 @@ public class CouchDBDao implements IDatabaseDao {
return (Answer) JSONObject.toBean(results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"),
Answer.class);
} catch (UnsupportedEncodingException e) {
logger.error("Error while retrieving unansweredquestions", e);
logger.error("Error while retrieving answer for user {} and question {}, {}", new Object[]{user, questionId, e});
}
return null;
}
@Override
public List<Answer> getAnswers(String sessionKey, String questionId) {
Session s = this.getSessionFromKeyword(sessionKey);
if(s == null) {
throw new NotFoundException();
}
try {
View view = new View("skill_question/count_answers");
view.setStartKey("[" + URLEncoder.encode("\"" + questionId + "\"", "UTF-8") + "]");
view.setEndKey("[" + URLEncoder.encode("\"" + questionId + "\",{}", "UTF-8") + "]");
view.setGroup(true);
ViewResults results = this.getDatabase().view(view);
if(results.getResults().isEmpty()) {
throw new NotFoundException();
}
List<Answer> answers = new ArrayList<Answer>();
for(Document d : results.getResults()) {
Answer a = new Answer();
a.setAnswerCount(d.getInt("value"));
a.setQuestionId(d.getJSONObject().getJSONArray("key").getString(0));
a.setAnswerText(d.getJSONObject().getJSONArray("key").getString(1));
a.setAnswerSubject(d.getJSONObject().getJSONArray("key").getString(2));
answers.add(a);
}
return answers;
} catch (UnsupportedEncodingException e) {
logger.error("Error while retrieving answers", e);
}
return null;
......
......@@ -67,4 +67,6 @@ public interface IDatabaseDao {
public Answer getMyAnswer(String sessionKey, String questionId);
public List<Answer> getAnswers(String sessionKey, String questionId);
}
\ No newline at end of file
package de.thm.arsnova.entities;
import java.util.ArrayList;
import java.util.List;
/*
"type":"skill_question_answer",
"sessionId":"61d33ea2ec73acefbba898c3510325c9",
"questionId":"61d33ea2ec73acefbba898c351040280",
"answerText":"2500 $",
"user":"jhtr80"
}}
*/
public class Answer {
private String _id;
......@@ -20,7 +9,9 @@ public class Answer {
private String sessionId;
private String questionId;
private String answerText;
private String answerSubject;
private String user;
private int answerCount;
public Answer() {
this.type = "skill_question_answer";
......@@ -74,6 +65,14 @@ public class Answer {
this.answerText = answerText;
}
public String getAnswerSubject() {
return answerSubject;
}
public void setAnswerSubject(String answerSubject) {
this.answerSubject = answerSubject;
}
public String getUser() {
return user;
}
......@@ -82,11 +81,21 @@ public class Answer {
this.user = user;
}
public int getAnswerCount() {
return answerCount;
}
public void setAnswerCount(int answerCount) {
this.answerCount = answerCount;
}
@Override
public String toString() {
return "Answer type:'" + type + "'" +
", session: " + sessionId +
", question: " + questionId +
", subject: " + answerSubject +
", answerCount: " + answerCount +
", answer: " + answerText +
", user: " + user;
}
......
......@@ -40,4 +40,6 @@ public interface IQuestionService {
public List<String> getUnAnsweredQuestions(String sessionKey);
public Answer getMyAnswer(String sessionKey, String questionId);
public List<Answer> getAnswers(String sessionKey, String questionId);
}
\ No newline at end of file
......@@ -100,4 +100,10 @@ public class QuestionService implements IQuestionService {
public Answer getMyAnswer(String sessionKey, String questionId) {
return databaseDao.getMyAnswer(sessionKey, questionId);
}
@Override
@Authenticated
public List<Answer> getAnswers(String sessionKey, String questionId) {
return databaseDao.getAnswers(sessionKey, questionId);
}
}
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