Skip to content

Commit

Permalink
Merge pull request #6 from MEITREX/content_mutated
Browse files Browse the repository at this point in the history
Publish assessment content mutated event
  • Loading branch information
ZombieAlienRobot authored Oct 18, 2024
2 parents 22ff2cd + ca4f468 commit 0984e77
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ repositories {
}

dependencies {
implementation 'de.unistuttgart.iste.meitrex:meitrex-common:1.2'
implementation 'de.unistuttgart.iste.meitrex:meitrex-common:1.2.4'
implementation 'com.google.code.findbugs:jsr305:3.0.2' // removes a gradle warning about an unknown annotation
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-graphql'
Expand All @@ -127,7 +127,7 @@ dependencies {
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'de.unistuttgart.iste.meitrex:meitrex-common-test:1.2'
testImplementation 'de.unistuttgart.iste.meitrex:meitrex-common-test:1.2.4'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework:spring-webflux'
testImplementation 'org.springframework.graphql:spring-graphql-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ public FlashcardSet createFlashcardSet(@Argument final UUID courseId,
return flashcardService.createFlashcardSet(courseId, assessmentId, input);
}

@MutationMapping
public UUID deleteFlashcardSet(@Argument(name = "input") final UUID id) {
return flashcardService.deleteFlashcardSet(id);
}

@MutationMapping
public FlashcardLearnedFeedback logFlashcardLearned(@Argument("input") final LogFlashcardLearnedInput input,
@ContextValue final LoggedInUser currentUser) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.unistuttgart.iste.meitrex.flashcard_service.service;

import de.unistuttgart.iste.meitrex.common.event.AssessmentContentMutatedEvent;
import de.unistuttgart.iste.meitrex.common.event.AssessmentType;
import de.unistuttgart.iste.meitrex.common.event.ContentChangeEvent;
import de.unistuttgart.iste.meitrex.common.event.CrudOperation;
import de.unistuttgart.iste.meitrex.common.exception.IncompleteEventMessageException;
Expand All @@ -8,6 +10,7 @@

import de.unistuttgart.iste.meitrex.flashcard_service.persistence.entity.FlashcardEntity;
import de.unistuttgart.iste.meitrex.flashcard_service.persistence.entity.FlashcardSetEntity;
import de.unistuttgart.iste.meitrex.flashcard_service.persistence.entity.FlashcardSideEntity;
import de.unistuttgart.iste.meitrex.flashcard_service.persistence.mapper.FlashcardMapper;
import de.unistuttgart.iste.meitrex.flashcard_service.persistence.repository.FlashcardRepository;
import de.unistuttgart.iste.meitrex.flashcard_service.persistence.repository.FlashcardSetRepository;
Expand All @@ -19,9 +22,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -49,6 +50,8 @@ public Flashcard createFlashcard(final UUID assessmentId, final CreateFlashcardI

set.getFlashcards().add(flashcard);

publishAssessmentContentMutatedEvent(set);

return flashcardMapper.entityToDto(flashcard);
}

Expand All @@ -64,6 +67,8 @@ public Flashcard updateFlashcard(final UpdateFlashcardInput input) {

updatedFlashcard = flashcardRepository.save(updatedFlashcard);

publishAssessmentContentMutatedEvent(updatedFlashcard.getParentSet());

return flashcardMapper.entityToDto(updatedFlashcard);
}

Expand All @@ -74,6 +79,9 @@ public UUID deleteFlashcard(final UUID assessmentId, final UUID flashcardId) {
}
flashcardSetRepository.save(set);
publishItemChangeEvent(flashcardId);

publishAssessmentContentMutatedEvent(set);

return flashcardId;
}

Expand All @@ -84,14 +92,10 @@ public FlashcardSet createFlashcardSet(final UUID courseId, final UUID assessmen
mappedEntity.setAssessmentId(assessmentId);
mappedEntity.setCourseId(courseId);
final FlashcardSetEntity flashcardSetEntity = flashcardSetRepository.save(mappedEntity);
return flashcardMapper.flashcardSetEntityToDto(flashcardSetEntity);
}

public UUID deleteFlashcardSet(final UUID uuid) {
requireFlashcardSetExisting(uuid);
publishDeletedFlashcardSet(uuid);
flashcardSetRepository.deleteById(uuid);
return uuid;
publishAssessmentContentMutatedEvent(flashcardSetEntity);

return flashcardMapper.flashcardSetEntityToDto(flashcardSetEntity);
}

public FlashcardSetEntity requireFlashcardSetExisting(final UUID uuid) {
Expand Down Expand Up @@ -178,6 +182,19 @@ private void publishItemChangeEvent(final UUID itemId) {

}

/**
* Helper method to raise an AssessmentContentMutatedEvent dapr event for the specified flashcard set.
* @param flashcardSetEntity The flashcard set for which to raise the event for.
*/
private void publishAssessmentContentMutatedEvent(final FlashcardSetEntity flashcardSetEntity) {
topicPublisher.notifyAssessmentContentMutated(new AssessmentContentMutatedEvent(
flashcardSetEntity.getCourseId(),
flashcardSetEntity.getAssessmentId(),
AssessmentType.FLASHCARDS,
generateTaskInformation(flashcardSetEntity)
));
}

/**
* for each flashcard of the deleted flashcard set publish a itemchanged event
*
Expand All @@ -192,4 +209,41 @@ private void publishDeletedFlashcardSet(UUID flashcardSetId) {
}
}
}

/**
* Helper method to generate TaskInformation objects for a given flashcard set.
* @param flashcardSet The flashcard set for which to generate the task information.
* @return List containing the task information.
*/
private List<AssessmentContentMutatedEvent.TaskInformation> generateTaskInformation(
final FlashcardSetEntity flashcardSet) {
final List<AssessmentContentMutatedEvent.TaskInformation> results =
new ArrayList<>(flashcardSet.getFlashcards().size());

for(final FlashcardEntity flashcard : flashcardSet.getFlashcards()) {
final StringBuilder sb = new StringBuilder();

sb.append("Flashcard\n\n");
// sort the flashcard sides such that questions appear before answers
List<FlashcardSideEntity> sortedSides = flashcard.getSides().stream()
.sorted(Comparator.comparing(x -> x.isQuestion() ? 0 : 1))
.toList();
for(FlashcardSideEntity side : sortedSides) {
if(side.isQuestion())
sb.append("Question Side:\n");
else if(side.isAnswer())
sb.append("Answer Side:\n");

sb.append(side.getLabel());
sb.append(": ");
sb.append(side.getText());
sb.append("\n\n");
}
results.add(new AssessmentContentMutatedEvent.TaskInformation(
flashcard.getItemId(),
sb.toString().trim()));
}

return results;
}
}
7 changes: 0 additions & 7 deletions src/main/resources/graphql/service/mutation.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ type Mutation {
"""
_internal_noauth_createFlashcardSet(courseId: UUID!, assessmentId: UUID!, input: CreateFlashcardSetInput!): FlashcardSet!

"""
Deletes a flashcard set. Throws an error if the flashcard set does not exist.
The contained flashcards are deleted as well.
"""
deleteFlashcardSet(input: UUID!): UUID!
@deprecated(reason: "Only for development, will be removed in production. Use deleteAssessment in contents service instead.")

"""
Modify a flashcard set.
🔒 The user must be an admin the course the flashcard set is in to perform this action.
Expand Down

0 comments on commit 0984e77

Please sign in to comment.