Skip to content

Commit

Permalink
Adapted FlashcardService
Browse files Browse the repository at this point in the history
  • Loading branch information
TessaKeller committed May 28, 2024
1 parent 01b7c85 commit b9bcaab
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:experimental
FROM gradle:7.6.2-jdk17 AS build
FROM gradle:8-jdk21 AS build
WORKDIR /workspace/app

COPY . /workspace/app
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.+'
id 'org.springframework.boot' version '3.2.+'
id 'io.spring.dependency-management' version '1.+'
id "io.github.kobylynskyi.graphql.codegen" version "5.+"
id "org.sonarqube" version "4.+"
Expand Down
2 changes: 1 addition & 1 deletion components/pubsub.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: gits
name: meitrex
spec:
type: pubsub.redis
version: v1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SubscriptionController {

private final FlashcardService flashcardService;

@Topic(name = "content-changed", pubsubName = "gits")
@Topic(name = "content-changed", pubsubName = "meitrex")
@PostMapping(path = "/flashcard-service/content-changed-pubsub")
public Mono<Void> updateAssociation(@RequestBody CloudEvent<ContentChangeEvent> cloudEvent) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@Entity(name = "FlashcardProgressData")
@Data
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class FlashcardProgressDataEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ public List<Flashcard> getFlashcardsByIds(final List<UUID> ids) {
* @return list of course ids, in the same order as the flashcard ids.
*/
public List<UUID> getCourseIdsForFlashcardIds(final List<UUID> flashcardIds) {
System.out.println("Recieved"+flashcardIds);
return flashcardRepository.findAllById(flashcardIds).stream()
.map(FlashcardEntity::getParentSet)
.map(FlashcardSetEntity::getCourseId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,34 +214,31 @@ private void publishFlashcardSetLearned(final UUID userId, final UUID flashcardS

if (dataLogEntities.size() < flashcardSetEntity.getFlashcards().size()) {
// not all flashcards have been learned yet

return;
}

List<Response>responses=new ArrayList<>();
for(FlashcardProgressDataLogEntity log:dataLogEntities){
FlashcardProgressDataEntity progressDataEntity = log.getFlashcardProgressData();
UUID flashcardID = progressDataEntity.getPrimaryKey().getFlashcardID();
Response response=new Response(flashcardID,log.getSuccess()?1:0);
responses.add(response);
}
final List<FlashcardProgressDataLog> dataLogs = dataLogEntities
.stream()
.map(this::mapLogEntityToDto)
.toList();

final int total = dataLogs.size();
final int correct = dataLogs.stream().mapToInt(log -> log.getSuccess() ? 1 : 0).sum();

final float correctness = (float) correct / total;

flashcardSetEntity.setLastLearned(OffsetDateTime.now());
flashcardSetRepository.save(flashcardSetEntity);
float answer=0;
if(successful){
answer=1F;
}
Response response=Response.builder()
.response(answer)
.itemId(itemId)
.build();

publishUserProgressEvent(userId, flashcardSetId, correctness,response);
publishUserProgressEvent(userId, flashcardSetId, correctness,responses);
}

private void publishUserProgressEvent(final UUID userId, final UUID assessmentId, final float correctness, Response response) {
private void publishUserProgressEvent(final UUID userId, final UUID assessmentId, final float correctness, List<Response> responses) {
topicPublisher.notifyUserWorkedOnContent(
ContentProgressedEvent.builder()
.contentId(assessmentId)
Expand All @@ -250,7 +247,7 @@ private void publishUserProgressEvent(final UUID userId, final UUID assessmentId
.success(true)
.timeToComplete(null)
.correctness(correctness)
.responses(List.of(response))
.responses(responses)
.build()
);
}
Expand Down
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 @@ -51,13 +51,6 @@ type FlashcardSetMutation {
"""
_internal_noauth_updateFlashcard(input: UpdateFlashcardInput!): Flashcard!

"""
Updates a flashcard. Throws an error if the flashcard does not exist.
⚠️ This mutation is only accessible internally in the system and allows the caller to update Flashcards without
any permissions check and should not be called without any validation of the caller's permissions. ⚠️
"""
updateFlashcard(input: UpdateFlashcardInput!): Flashcard!

"""
Deletes the flashcard with the specified ID. Throws an error if the flashcard does not exist.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void testLogFlashcardProgress(final HttpGraphQlTester graphQlTester) {
.success(true)
.timeToComplete(null)
.hintsUsed(0)
.responses(List.of(Response.builder().itemId(flashcardId2).response(0).build()))
.responses(List.of(Response.builder().itemId(flashcardId1).response(1).build(),Response.builder().itemId(flashcardId2).response(0).build()))
.build();

verify(topicPublisher).notifyUserWorkedOnContent(expectedEvent);
Expand All @@ -121,7 +121,7 @@ void testLogFlashcardProgress(final HttpGraphQlTester graphQlTester) {
.success(true)
.timeToComplete(null)
.hintsUsed(0)
.responses(List.of(Response.builder().itemId(flashcardId2).response(1).build()))
.responses(List.of(Response.builder().itemId(flashcardId1).response(1).build(),Response.builder().itemId(flashcardId2).response(1).build()))
.build();

verify(topicPublisher).notifyUserWorkedOnContent(expectedEvent2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ class QueryFlashcardTest {
@Transactional
void testQueryFlashcardsByIds(final GraphQlTester tester) {
final List<FlashcardSetEntity> expectedSets = testUtils.populateFlashcardSetRepository(flashcardSetRepository, courseId);
System.out.println( ("testId"+expectedSets.get(0).getFlashcards().get(0).getItemId()));
System.out.println( ("testId"+expectedSets.get(1).getFlashcards().get(1).getItemId()));
final List<FlashcardEntity> flashcardsToQuery = List.of(
expectedSets.get(0).getFlashcards().get(0),
expectedSets.get(1).getFlashcards().get(1)
);
List<UUID>ids=List.of(expectedSets.get(0).getFlashcards().get(0).getItemId(),expectedSets.get(1).getFlashcards().get(1).getItemId());
System.out.println(ids);
final String query = """
query($ids: [UUID!]!) {
flashcardsByIds(itemIds: $ids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,23 @@ public class TestAuthorization {
@Test
@Transactional
@Commit
void testUpdateFlashcardOnlyForAdmins(final GraphQlTester tester) {
final List<FlashcardSetEntity> set = testUtils.populateFlashcardSetRepository(flashcardSetRepository, courseId);

final UUID setOfFlashcard = set.get(0).getAssessmentId();
// Perform the update operation
final UUID flashcardToUpdate = set.get(0).getFlashcards().stream().findAny().orElseThrow().getItemId();
void testDeleteFlashcardOnlyForAdmins(final GraphQlTester tester) {
final List<FlashcardSetEntity> expectedSets = testUtils.populateFlashcardSetRepository(flashcardSetRepository, courseId);

final String query = """
mutation ($assessmentId: UUID!, $flashcardId: UUID!) {
mutation($assessmentId: UUID!, $flashcardId: UUID!) {
mutateFlashcardSet(assessmentId: $assessmentId) {
updateFlashcard(input: {
itemId: $flashcardId,
sides: [
{
label: "New_Side 1",
isQuestion: true,
isAnswer: false,
text: "{text: \\"New_Question 1\\"}"
},
{
label: "New_Side 2",
isQuestion: false,
isAnswer: true,
text: "{text: \\"New_Answer 1\\"}"
}
]
}) {
itemId
sides {
label
isQuestion
isAnswer
text
}
}
deleteFlashcard(id: $flashcardId)
}
}
}
""";

// Execute the update mutation query
final UUID setToDeleteFrom = expectedSets.get(0).getAssessmentId();
final UUID flashcardToDelete = expectedSets.get(0).getFlashcards().stream().findAny().orElseThrow().getItemId();

tester.document(query)
.variable("assessmentId", setOfFlashcard)
.variable("flashcardId", flashcardToUpdate)
.variable("assessmentId", setToDeleteFrom)
.variable("flashcardId", flashcardToDelete)
.execute()
.errors()
.satisfy(AuthorizationAsserts::assertIsMissingUserRoleError);
Expand Down

0 comments on commit b9bcaab

Please sign in to comment.