Commit f17340b4 authored by Daniel Gerhardt's avatar Daniel Gerhardt

Fix persistance related bugs for contents and answers

* Fix delayed answer creation
* Fix deletion of all contents of a group
* Fix deletion of all answers of a content
* Fix answer count for groups
* Initialize new answers with round 1
parent b5566915
Pipeline #30064 passed with stages
in 1 minute and 28 seconds
......@@ -529,6 +529,9 @@ public class ContentController extends PaginationController {
final HttpServletResponse response) {
final de.thm.arsnova.model.Content content = contentService.get(contentId);
final de.thm.arsnova.model.Answer answerV3 = fromV2Migrator.migrate(answer, content);
if (answerV3.getContentId() == null) {
answerV3.setContentId(contentId);
}
if (!contentId.equals(answerV3.getContentId())) {
throw new BadRequestException("Mismatching content IDs.");
}
......
......@@ -52,7 +52,7 @@ public class Answer extends Entity {
private Content.Format format;
@Positive
private int round;
private int round = 1;
private Map<String, Map<String, ?>> extensions;
......
......@@ -64,12 +64,12 @@ public class CouchDbAnswerRepository extends CouchDbCrudRepository<Answer>
@Override
public Iterable<Answer> findStubsByContentId(final String contentId) {
return createEntityStubs(db.queryView(createQuery("by_contentid").key(contentId)));
return createEntityStubs(db.queryView(createQuery("by_contentid").reduce(false).key(contentId)));
}
@Override
public Iterable<Answer> findStubsByContentIds(final Collection<String> contentIds) {
return createEntityStubs(db.queryView(createQuery("by_contentid").keys(contentIds)));
return createEntityStubs(db.queryView(createQuery("by_contentid").reduce(false).keys(contentIds)));
}
@Override
......@@ -152,7 +152,9 @@ public class CouchDbAnswerRepository extends CouchDbCrudRepository<Answer>
.group(true)
.keys(contentIds));
return result.isEmpty() ? 0 : result.getRows().get(0).getValueAsInt();
return result.isEmpty() ? 0 : result.getRows().stream()
.map(ViewResult.Row::getValueAsInt)
.reduce(0, (a, b) -> a + b);
}
@Override
......
......@@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import org.ektorp.ComplexKey;
import org.ektorp.CouchDbConnector;
import org.ektorp.ViewResult;
......@@ -82,9 +83,9 @@ public class CouchDbContentRepository extends CouchDbCrudRepository<Content> imp
@Override
public Iterable<Content> findStubsByIds(final Set<String> ids) {
return createEntityStubs(db.queryView(createQuery("by_id")
return super.createEntityStubs(db.queryView(createQuery("by_id")
.keys(ids)
.reduce(false)));
.reduce(false)), (a, b) -> {});
}
@Override
......
......@@ -122,7 +122,9 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen
content.resetState();
/* FIXME: cancel timer */
contentService.update(content);
delete(answerRepository.findStubsByContentId(content.getId()));
final Iterable<Answer> answers = answerRepository.findStubsByContentId(content.getId());
answers.forEach(a -> a.setRoomId(content.getRoomId()));
delete(answers);
}
@Override
......@@ -323,7 +325,9 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen
@Override
@PreAuthorize("isAuthenticated() && hasPermission(#answer, 'create')")
public Answer create(final Answer answer) {
this.answerQueue.offer(answer);
prepareCreate(answer);
answerQueue.offer(answer);
finalizeCreate(answer);
return answer;
}
......@@ -438,6 +442,7 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen
@Secured({"ROLE_USER", "RUN_AS_SYSTEM"})
public void handleContentDeletion(final BeforeDeletionEvent<Content> event) {
final Iterable<Answer> answers = answerRepository.findStubsByContentId(event.getEntity().getId());
answers.forEach(a -> a.setRoomId(event.getEntity().getRoomId()));
delete(answers);
}
}
......@@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -240,12 +241,13 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem
@PreAuthorize("isAuthenticated()")
private void deleteByRoomAndGroupName(final Room room, final String groupName) {
final Iterable<Content> contents;
if ("all".equals(groupName)) {
delete(contentRepository.findStubsByRoomId(room.getId()));
} else {
final Set<String> ids = contentGroupService.getByRoomIdAndName(room.getId(), groupName).getContentIds();
delete(contentRepository.findStubsByIds(ids));
final Iterable<Content> contents = contentRepository.findStubsByIds(ids);
contents.forEach(c -> c.setRoomId(room.getId()));
delete(contents);
}
}
......
Markdown is supported
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