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

Task #4034: delete question and its answers

check if session has this question and throw unauthorized exception if
not
parent 198c36a3
Branches
Tags
No related merge requests found
......@@ -113,4 +113,10 @@ public class QuestionController extends AbstractController {
return questions;
}
@RequestMapping(value="/session/{sessionKey}/questions/{questiondId}", method=RequestMethod.DELETE)
@ResponseBody
public void deleteAnswersAndQuestion(@PathVariable String sessionKey, @PathVariable String questionId, HttpServletResponse response) {
questionService.deleteQuestion(sessionKey, questionId);
}
}
......@@ -61,6 +61,7 @@ import de.thm.arsnova.entities.User;
import de.thm.arsnova.entities.VisitedSession;
import de.thm.arsnova.exceptions.ForbiddenException;
import de.thm.arsnova.exceptions.NotFoundException;
import de.thm.arsnova.exceptions.UnauthorizedException;
import de.thm.arsnova.services.IFeedbackService;
import de.thm.arsnova.services.ISessionService;
import de.thm.arsnova.services.IUserService;
......@@ -602,4 +603,33 @@ public class CouchDBDao implements IDatabaseDao {
}
return null;
}
@Override
public void deleteQuestion(String sessionKey, String questionId) {
Session s = this.getSessionFromKeyword(sessionKey);
try {
Document question = this.getDatabase().getDocument(questionId);
if(!question.getString("sessionId").equals(s.get_id())) {
throw new UnauthorizedException();
}
} catch (IOException e) {
logger.error("could not find question {}", questionId);
}
try {
View view = new View("answer/cleanup");
view.setKey(URLEncoder.encode("\"" + questionId + "\"", "UTF-8"));
ViewResults results = this.getDatabase().view(view);
for(Document d : results.getResults()) {
Document answer = this.getDatabase().getDocument(d.getId());
this.getDatabase().deleteDocument(answer);
}
Document question = this.getDatabase().getDocument(questionId);
this.getDatabase().deleteDocument(question);
} catch(Exception e) {
logger.error("Could not delete question and its answers with id {}", questionId);
}
}
}
......@@ -45,4 +45,5 @@ public interface IDatabaseDao {
public LoggedIn registerAsOnlineUser(User u, Session s);
public void updateSessionOwnerActivity(Session session);
public List<String> getQuestionIds(String sessionKey);
public void deleteQuestion(String sessionKey, String questionId);
}
\ No newline at end of file
......@@ -30,4 +30,5 @@ public interface IQuestionService {
public List<Question> getSkillQuestions(String sessionkey);
public int getSkillQuestionCount(String sessionkey);
public List<String> getQuestionIds(String sessionKey);
public void deleteQuestion(String sessionKey, String questionId);
}
\ No newline at end of file
......@@ -74,4 +74,11 @@ public class QuestionService implements IQuestionService {
public List<String> getQuestionIds(String sessionKey) {
return databaseDao.getQuestionIds(sessionKey);
}
@Override
@Authenticated
public void deleteQuestion(String sessionKey, String questionId) {
databaseDao.deleteQuestion(sessionKey, questionId);
}
}
......@@ -169,4 +169,10 @@ public class StubDatabaseDao implements IDatabaseDao {
return null;
}
@Override
public void deleteQuestion(String sessionKey, String questionId) {
// 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