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

Task #4036: getAnswerByUserAndSession

parent b1f6d301
No related merge requests found
......@@ -230,5 +230,14 @@ public class QuestionController extends AbstractController {
return questionService.getFreetextAnswers(sessionKey, questionId);
}
@RequestMapping(value = "/session/{sessionKey}/myanswers", method = RequestMethod.GET)
@ResponseBody
public final List<Answer> getMyAnswers(
@PathVariable final String sessionKey,
final HttpServletResponse response
) {
return questionService.getMytAnswers(sessionKey);
}
}
......@@ -904,6 +904,42 @@ public class CouchDBDao implements IDatabaseDao {
LOGGER.error("Error while retrieving freetext answers", e);
}
return null;
}
@Override
public List<Answer> getMyAnswers(String sessionKey) {
Session s = this.getSessionFromKeyword(sessionKey);
if (s == null) {
throw new NotFoundException();
}
User user = userService.getCurrentUser();
if(user == null) {
throw new UnauthorizedException();
}
try {
View view = new View("answer/by_user_and_session");
view.setKey("[" + URLEncoder.encode("\"" + user.getUsername() + "\",\"" + s.get_id() + "\"", "UTF-8") + "]");
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 = (Answer) JSONObject.toBean(d.getJSONObject().getJSONObject("value"), Answer.class);
a.set_id(d.getId());
a.setSessionId(s.get_id());
answers.add(a);
}
return answers;
} catch (UnsupportedEncodingException e) {
LOGGER.error("Error while retrieving user answers", e);
}
return null;
}
}
......@@ -74,4 +74,6 @@ public interface IDatabaseDao {
List<Answer> getFreetextAnswers(String sessionKey, String questionId);
int getActiveUsers(long since);
List<Answer> getMyAnswers(String sessionKey);
}
......@@ -46,4 +46,6 @@ public interface IQuestionService {
int getAnswerCount(String sessionKey, String questionId);
List<Answer> getFreetextAnswers(String sessionKey, String questionId);
List<Answer> getMytAnswers(String sessionKey);
}
......@@ -118,4 +118,10 @@ public class QuestionService implements IQuestionService {
public List<Answer> getFreetextAnswers(String sessionKey, String questionId) {
return databaseDao.getFreetextAnswers(sessionKey, questionId);
}
@Override
@Authenticated
public List<Answer> getMytAnswers(String sessionKey) {
return databaseDao.getMyAnswers(sessionKey);
}
}
......@@ -239,5 +239,11 @@ public class StubDatabaseDao implements IDatabaseDao {
public int getActiveUsers(long since) {
return stubUsers.size();
}
@Override
public List<Answer> getMyAnswers(String sessionKey) {
// TODO Auto-generated method stub
return null;
}
}
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