|
| 1 | +package de.unistuttgart.iste.meitrex.gamification_service.dapr; |
| 2 | + |
| 3 | +import de.unistuttgart.iste.meitrex.common.dapr.TopicPublisher; |
| 4 | +import de.unistuttgart.iste.meitrex.common.event.RequestHexadPlayerTypeEvent; |
| 5 | +import de.unistuttgart.iste.meitrex.gamification_service.service.IPlayerHexadScoreService; |
| 6 | +import io.dapr.Topic; |
| 7 | +import io.dapr.client.domain.CloudEvent; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.web.bind.annotation.PostMapping; |
| 11 | +import org.springframework.web.bind.annotation.RequestBody; |
| 12 | +import org.springframework.web.bind.annotation.RestController; |
| 13 | + |
| 14 | +/** |
| 15 | + * Listener for RequestHexadPlayerTypeEvent. |
| 16 | + * When a request is received, it fetches the user's hexad player type |
| 17 | + * and publishes a UserHexadPlayerTypeSetEvent in response. |
| 18 | + */ |
| 19 | +@Slf4j |
| 20 | +@RestController |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class RequestHexadPlayerTypeEventListener { |
| 23 | + |
| 24 | + private final IPlayerHexadScoreService playerHexadScoreService; |
| 25 | + private final TopicPublisher topicPublisher; |
| 26 | + |
| 27 | + @Topic(name = "request-hexad-player-type", pubsubName = "meitrex") |
| 28 | + @PostMapping(path = "/request-hexad-player-type-pubsub") |
| 29 | + public void onRequestHexadPlayerTypeEvent(@RequestBody CloudEvent<RequestHexadPlayerTypeEvent> cloudEvent) { |
| 30 | + RequestHexadPlayerTypeEvent event = cloudEvent.getData(); |
| 31 | + |
| 32 | + log.info("Received RequestHexadPlayerTypeEvent for user: {}", event.getUserId()); |
| 33 | + |
| 34 | + try { |
| 35 | + // Fetch the player hexad score for the user |
| 36 | + var playerHexadScore = playerHexadScoreService.getById(event.getUserId()); |
| 37 | + |
| 38 | + if (playerHexadScore != null) { |
| 39 | + log.info("Publishing UserHexadPlayerTypeSetEvent for user: {}", event.getUserId()); |
| 40 | + playerHexadScoreService.sendUserHexadPlayerTypeSetEvent(event.getUserId(), playerHexadScore); |
| 41 | + } else { |
| 42 | + log.warn("No hexad player type found for user: {}", event.getUserId()); |
| 43 | + } |
| 44 | + } catch (Exception e) { |
| 45 | + log.error("Error handling RequestHexadPlayerTypeEvent for user {}: {}", |
| 46 | + event.getUserId(), e.getMessage(), e); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments