Skip to content
Snippets Groups Projects
Commit d158d112 authored by Christoph Thelen's avatar Christoph Thelen
Browse files

Fixed counting of interposed questions

parent b11aad82
No related merge requests found
...@@ -105,13 +105,13 @@ public class QuestionByAudienceController extends AbstractController { ...@@ -105,13 +105,13 @@ public class QuestionByAudienceController extends AbstractController {
throw new BadRequestException(); throw new BadRequestException();
} }
@RequestMapping(value = "/{questionId}", method = RequestMethod.DELETE) @RequestMapping(value = "/{questionId}", method = RequestMethod.DELETE)
@ResponseBody @ResponseBody
public final void deleteInterposedQuestion( public final void deleteInterposedQuestion(
@PathVariable final String questionId, @PathVariable final String questionId,
final HttpServletResponse response final HttpServletResponse response
) { ) {
questionService.deleteQuestion(questionId); questionService.deleteInterposedQuestion(questionId);
} }
} }
...@@ -760,13 +760,17 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -760,13 +760,17 @@ public class CouchDBDao implements IDatabaseDao {
public final void deleteQuestion(final Question question) { public final void deleteQuestion(final Question question) {
try { try {
this.deleteAnswers(question); this.deleteAnswers(question);
Document q = this.getDatabase().getDocument(question.get_id()); this.deleteDocument(question.get_id());
this.getDatabase().deleteDocument(q);
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("IOException: Could not delete question {}", question.get_id()); LOGGER.error("IOException: Could not delete question {}", question.get_id());
} }
} }
private void deleteDocument(final String documentId) throws IOException {
Document d = this.getDatabase().getDocument(documentId);
this.getDatabase().deleteDocument(d);
}
@Override @Override
public final void deleteAnswers(final Question question) { public final void deleteAnswers(final Question question) {
try { try {
...@@ -775,8 +779,7 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -775,8 +779,7 @@ public class CouchDBDao implements IDatabaseDao {
ViewResults results = this.getDatabase().view(view); ViewResults results = this.getDatabase().view(view);
for (Document d : results.getResults()) { for (Document d : results.getResults()) {
Document answer = this.getDatabase().getDocument(d.getId()); this.deleteDocument(d.getId());
this.getDatabase().deleteDocument(answer);
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("IOException: Could not delete answers for question {}", question.get_id()); LOGGER.error("IOException: Could not delete answers for question {}", question.get_id());
...@@ -1047,7 +1050,10 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -1047,7 +1050,10 @@ public class CouchDBDao implements IDatabaseDao {
return new InterposedReadingCount(); return new InterposedReadingCount();
} }
int read = results.getJSONArray("rows").optJSONObject(0).optInt("value"); int read = results.getJSONArray("rows").optJSONObject(0).optInt("value");
int unread = results.getJSONArray("rows").optJSONObject(1).optInt("value"); int unread = 0;
if (results.getJSONArray("rows").optJSONObject(1) != null) {
unread = results.getJSONArray("rows").optJSONObject(1).optInt("value");
}
return new InterposedReadingCount(read, unread); return new InterposedReadingCount(read, unread);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
LOGGER.error("Error while retrieving interposed question count", e); LOGGER.error("Error while retrieving interposed question count", e);
...@@ -1234,22 +1240,29 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -1234,22 +1240,29 @@ public class CouchDBDao implements IDatabaseDao {
} }
@Override @Override
public InterposedQuestion getInterposedQuestion(String questionId) throws IOException { public InterposedQuestion getInterposedQuestion(String questionId) {
Document document = this.getDatabase().getDocument(questionId); try {
InterposedQuestion question = (InterposedQuestion) JSONObject.toBean( Document document = this.getDatabase().getDocument(questionId);
document.getJSONObject(), InterposedQuestion question = (InterposedQuestion) JSONObject.toBean(document.getJSONObject(),
InterposedQuestion.class InterposedQuestion.class);
); question.setSessionId(getSessionKeyword(question.getSessionId()));
question.setSessionId(getSessionKeyword(question.getSessionId())); return question;
return question; } catch (IOException e) {
LOGGER.error("Could not load interposed question {}", questionId);
}
return null;
} }
@Override @Override
public void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException { public void markInterposedQuestionAsRead(InterposedQuestion question) {
question.setRead(true); try {
Document document = this.getDatabase().getDocument(question.get_id()); question.setRead(true);
document.put("read", question.isRead()); Document document = this.getDatabase().getDocument(question.get_id());
this.getDatabase().saveDocument(document); document.put("read", question.isRead());
this.getDatabase().saveDocument(document);
} catch (IOException e) {
LOGGER.error("Coulg not mark interposed question as read {}", question.get_id());
}
} }
@Override @Override
...@@ -1329,4 +1342,13 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -1329,4 +1342,13 @@ public class CouchDBDao implements IDatabaseDao {
LOGGER.error("Could not delete answer {} because of {}", answerId, e.getMessage()); LOGGER.error("Could not delete answer {} because of {}", answerId, e.getMessage());
} }
} }
@Override
public void deleteInterposedQuestion(InterposedQuestion question) {
try {
this.deleteDocument(question.get_id());
} catch (IOException e) {
LOGGER.error("Could not delete interposed question {} because of {}", question.get_id(), e.getMessage());
}
}
} }
...@@ -109,9 +109,9 @@ public interface IDatabaseDao { ...@@ -109,9 +109,9 @@ public interface IDatabaseDao {
int countQuestions(); int countQuestions();
InterposedQuestion getInterposedQuestion(String questionId) throws IOException; InterposedQuestion getInterposedQuestion(String questionId);
void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException; void markInterposedQuestionAsRead(InterposedQuestion question);
List<Session> getMyVisitedSessions(User user); List<Session> getMyVisitedSessions(User user);
...@@ -126,4 +126,6 @@ public interface IDatabaseDao { ...@@ -126,4 +126,6 @@ public interface IDatabaseDao {
Session getSessionFromId(String sessionId); Session getSessionFromId(String sessionId);
void deleteAnswer(String answerId); void deleteAnswer(String answerId);
void deleteInterposedQuestion(InterposedQuestion question);
} }
...@@ -73,4 +73,6 @@ public interface IQuestionService { ...@@ -73,4 +73,6 @@ public interface IQuestionService {
void deleteAnswer(String questionId, String answerId); void deleteAnswer(String questionId, String answerId);
void deleteInterposedQuestion(String questionId);
} }
...@@ -117,6 +117,21 @@ public class QuestionService implements IQuestionService { ...@@ -117,6 +117,21 @@ public class QuestionService implements IQuestionService {
} }
databaseDao.deleteQuestion(question); databaseDao.deleteQuestion(question);
} }
@Override
@Authenticated
public void deleteInterposedQuestion(String questionId) {
InterposedQuestion question = databaseDao.getInterposedQuestion(questionId);
if (question == null) {
throw new NotFoundException();
}
User user = userService.getCurrentUser();
Session session = databaseDao.getSessionFromKeyword(question.getSessionId());
if (user == null || session == null || !session.isCreator(user)) {
throw new UnauthorizedException();
}
databaseDao.deleteInterposedQuestion(question);
}
@Override @Override
@Authenticated @Authenticated
...@@ -206,18 +221,17 @@ public class QuestionService implements IQuestionService { ...@@ -206,18 +221,17 @@ public class QuestionService implements IQuestionService {
@Override @Override
@Authenticated @Authenticated
public InterposedQuestion readInterposedQuestion(String questionId) { public InterposedQuestion readInterposedQuestion(String questionId) {
try { InterposedQuestion question = databaseDao.getInterposedQuestion(questionId);
InterposedQuestion question = databaseDao.getInterposedQuestion(questionId); if (question == null) {
Session session = this.databaseDao.getSessionFromKeyword(question.getSessionId());
User user = this.userService.getCurrentUser();
if (session.isCreator(user)) {
this.databaseDao.markInterposedQuestionAsRead(question);
}
return question;
} catch (IOException e) {
throw new NotFoundException(); throw new NotFoundException();
} }
Session session = this.databaseDao.getSessionFromKeyword(question.getSessionId());
User user = this.userService.getCurrentUser();
if (session.isCreator(user)) {
this.databaseDao.markInterposedQuestionAsRead(question);
}
return question;
} }
@Override @Override
......
...@@ -338,12 +338,12 @@ public class StubDatabaseDao implements IDatabaseDao { ...@@ -338,12 +338,12 @@ public class StubDatabaseDao implements IDatabaseDao {
} }
@Override @Override
public InterposedQuestion getInterposedQuestion(String questionId) throws IOException { public InterposedQuestion getInterposedQuestion(String questionId) {
return this.interposedQuestion; return this.interposedQuestion;
} }
@Override @Override
public void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException { public void markInterposedQuestionAsRead(InterposedQuestion question) {
this.interposedQuestion.setRead(true); this.interposedQuestion.setRead(true);
} }
...@@ -408,4 +408,9 @@ public class StubDatabaseDao implements IDatabaseDao { ...@@ -408,4 +408,9 @@ public class StubDatabaseDao implements IDatabaseDao {
public void deleteAnswer(String answerId) { public void deleteAnswer(String answerId) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override
public void deleteInterposedQuestion(InterposedQuestion question) {
// TODO Auto-generated method stub
}
} }
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