Skip to content

Commit ce79a99

Browse files
committed
listens to RequestUserSkillLevelEvent and sends UserHexadPlayerTypeSetEvent as response
1 parent 589a16b commit ce79a99

4 files changed

Lines changed: 66 additions & 2 deletions

File tree

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ repositories {
107107

108108
dependencies {
109109

110-
implementation 'de.unistuttgart.iste.meitrex:meitrex-common:1.4.4'
110+
implementation 'de.unistuttgart.iste.meitrex:meitrex-common:1.4.12pre5'
111111
implementation 'de.unistuttgart.iste.meitrex:content_service:1.5.0rc1'
112112

113113
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
@@ -127,7 +127,7 @@ dependencies {
127127
runtimeOnly 'org.postgresql:postgresql'
128128
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
129129
annotationProcessor 'org.projectlombok:lombok'
130-
testImplementation 'de.unistuttgart.iste.meitrex:meitrex-common-test:1.4.4'
130+
testImplementation 'de.unistuttgart.iste.meitrex:meitrex-common-test:1.4.11'
131131
testImplementation 'org.springframework.boot:spring-boot-starter-test'
132132
testImplementation 'org.springframework:spring-webflux'
133133
testImplementation 'org.springframework.graphql:spring-graphql-test'

src/main/java/de/unistuttgart/iste/meitrex/skilllevel_service/controller/SubscriptionController.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import de.unistuttgart.iste.meitrex.common.event.CourseChangeEvent;
55
import de.unistuttgart.iste.meitrex.common.event.CrudOperation;
6+
import de.unistuttgart.iste.meitrex.common.event.RequestUserSkillLevelEvent;
67
import de.unistuttgart.iste.meitrex.common.event.UserProgressUpdatedEvent;
78
import de.unistuttgart.iste.meitrex.common.event.ItemChangeEvent;
89

@@ -83,4 +84,24 @@ public Mono<Void> onItemChanged(@RequestBody final CloudEvent<ItemChangeEvent> c
8384
}
8485
});
8586
}
87+
88+
/**
89+
* Dapr topic subscription to handle requests for user skill levels.
90+
* When a service requests skill levels for a user, this publishes UserSkillLevelChangedEvent for each skill level the user has.
91+
*/
92+
@Topic(name = "request-user-skill-level", pubsubName = "meitrex")
93+
@PostMapping(path = "/skilllevel-service/request-user-skill-level-pubsub")
94+
public Mono<Void> onRequestUserSkillLevel(@RequestBody final CloudEvent<RequestUserSkillLevelEvent> cloudEvent) {
95+
return Mono.fromRunnable(() -> {
96+
try {
97+
RequestUserSkillLevelEvent event = cloudEvent.getData();
98+
99+
skillLevelService.publishAllSkillLevelsForUser(event.getUserId());
100+
} catch (final Exception e) {
101+
// we need to catch all exceptions because otherwise if some invalid data is in the message queue
102+
// it will never get processed and instead the service will just crash forever
103+
log.error("Error while processing request user skill level event", e);
104+
}
105+
});
106+
}
86107
}

src/main/java/de/unistuttgart/iste/meitrex/skilllevel_service/persistence/repository/AllSkillLevelsRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ public interface AllSkillLevelsRepository extends JpaRepository<AllSkillLevelsEn
1414

1515
List<AllSkillLevelsEntity> findByIdSkillId(UUID skillId);
1616

17+
List<AllSkillLevelsEntity> findByIdUserId(UUID userId);
18+
1719
}

src/main/java/de/unistuttgart/iste/meitrex/skilllevel_service/service/SkillLevelService.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.unistuttgart.iste.meitrex.skilllevel_service.service;
22

3+
import de.unistuttgart.iste.meitrex.common.dapr.TopicPublisher;
4+
import de.unistuttgart.iste.meitrex.common.event.skilllevels.UserSkillLevelChangedEvent;
35
import de.unistuttgart.iste.meitrex.generated.dto.*;
46
import de.unistuttgart.iste.meitrex.common.event.ItemResponse;
57
import de.unistuttgart.iste.meitrex.skilllevel_service.persistence.entity.AllSkillLevelsEntity;
@@ -34,6 +36,8 @@ public class SkillLevelService {
3436
private final ItemDifficultyRepository itemDifficultyRepository;
3537

3638
private final SkillsForCourseRepository skillsForCourseRepository;
39+
40+
private final TopicPublisher topicPublisher;
3741

3842
/**
3943
* Recalculates the skill levels for a given user and responses.
@@ -170,4 +174,41 @@ private SkillLevelEntity initializeSkillLevelEntity(final float initialValue) {
170174
skillLevelEntity.setLog(new ArrayList<>());
171175
return skillLevelEntity;
172176
}
177+
178+
/**
179+
* Publishes UserSkillLevelChangedEvent for all skill levels of a given user.
180+
* This is used to respond to RequestUserSkillLevelEvent.
181+
*
182+
* @param userId The ID of the user to publish skill levels for
183+
*/
184+
public void publishAllSkillLevelsForUser(final UUID userId) {
185+
List<AllSkillLevelsEntity> allSkillLevels = skillLevelsRepository.findByIdUserId(userId);
186+
187+
for (AllSkillLevelsEntity skillLevelEntity : allSkillLevels) {
188+
UUID skillId = skillLevelEntity.getId().getSkillId();
189+
190+
publishSkillLevelEvent(userId, skillId, BloomLevel.REMEMBER, skillLevelEntity.getRemember().getValue());
191+
publishSkillLevelEvent(userId, skillId, BloomLevel.UNDERSTAND, skillLevelEntity.getUnderstand().getValue());
192+
publishSkillLevelEvent(userId, skillId, BloomLevel.APPLY, skillLevelEntity.getApply().getValue());
193+
publishSkillLevelEvent(userId, skillId, BloomLevel.ANALYZE, skillLevelEntity.getAnalyze().getValue());
194+
publishSkillLevelEvent(userId, skillId, BloomLevel.EVALUATE, skillLevelEntity.getEvaluate().getValue());
195+
publishSkillLevelEvent(userId, skillId, BloomLevel.CREATE, skillLevelEntity.getCreate().getValue());
196+
}
197+
}
198+
199+
/**
200+
* Method to publish a single UserSkillLevelChangedEvent.
201+
* oldValue and newValue are set to the same value since this is just for publishing existing skill levels and not for indicating a change
202+
*/
203+
private void publishSkillLevelEvent(UUID userId, UUID skillId, BloomLevel bloomLevel, float value) {
204+
UserSkillLevelChangedEvent event = UserSkillLevelChangedEvent.builder()
205+
.userId(userId)
206+
.skillId(skillId)
207+
.bloomLevel(bloomLevel)
208+
.oldValue(value)
209+
.newValue(value)
210+
.build();
211+
212+
topicPublisher.notifyUserSkillLevelChanged(event);
213+
}
173214
}

0 commit comments

Comments
 (0)