|
10 | 10 | import com.datadoghq.stickerlandia.stickeraward.beans.UserStickersResponseApiResponse; |
11 | 11 | import com.datadoghq.stickerlandia.stickeraward.entity.Sticker; |
12 | 12 | import com.datadoghq.stickerlandia.stickeraward.entity.StickerAssignment; |
| 13 | +import com.datadoghq.stickerlandia.stickeraward.messaging.StickerEventPublisher; |
13 | 14 | import io.smallrye.common.constraint.NotNull; |
14 | 15 | import jakarta.inject.Inject; |
15 | 16 | import jakarta.transaction.Transactional; |
|
22 | 23 | import jakarta.ws.rs.Produces; |
23 | 24 | import jakarta.ws.rs.core.Response; |
24 | 25 | import org.eclipse.microprofile.openapi.annotations.Operation; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
25 | 28 |
|
26 | 29 | import java.time.Instant; |
27 | 30 | import java.util.ArrayList; |
|
31 | 34 |
|
32 | 35 | @Path("/api") |
33 | 36 | public class StickerAwardResource { |
| 37 | + |
| 38 | + private static final Logger log = LoggerFactory.getLogger(StickerAwardResource.class); |
| 39 | + |
| 40 | + @Inject |
| 41 | + StickerEventPublisher eventPublisher; |
34 | 42 |
|
35 | 43 | @Operation(description = "Get stickers assigned to a user (access controlled based on caller identity)") |
36 | 44 | @Path("/award/v1/users/{userId}/stickers") |
@@ -100,6 +108,16 @@ public Response assignStickerToUser(@PathParam("userId") String userId, |
100 | 108 | // Create new assignment |
101 | 109 | StickerAssignment assignment = new StickerAssignment(userId, sticker, data.getReason()); |
102 | 110 | assignment.persist(); |
| 111 | + |
| 112 | + // Publish events to Kafka |
| 113 | + try { |
| 114 | + log.info("Publishing sticker assignment events for userId={}, stickerId={}", userId, stickerId); |
| 115 | + eventPublisher.publishStickerAssigned(assignment); |
| 116 | + } catch (Exception e) { |
| 117 | + log.error("Failed to publish sticker assignment events", e); |
| 118 | + // Continue with the response even if the event publishing fails |
| 119 | + // In a production system, you might want to implement a retry mechanism |
| 120 | + } |
103 | 121 |
|
104 | 122 | // Create response |
105 | 123 | StickerAssignmentResponse response = new StickerAssignmentResponse(); |
@@ -139,6 +157,16 @@ public Response removeStickerFromUser(@PathParam("userId") String userId, |
139 | 157 | // Mark as removed |
140 | 158 | assignment.setRemovedAt(Instant.now()); |
141 | 159 | assignment.persist(); |
| 160 | + |
| 161 | + // Publish events to Kafka |
| 162 | + try { |
| 163 | + log.info("Publishing sticker removal event for userId={}, stickerId={}", userId, stickerId); |
| 164 | + eventPublisher.publishStickerRemoved(assignment); |
| 165 | + } catch (Exception e) { |
| 166 | + log.error("Failed to publish sticker removal event", e); |
| 167 | + // Continue with the response even if the event publishing fails |
| 168 | + // In a production system, you might want to implement a retry mechanism |
| 169 | + } |
142 | 170 |
|
143 | 171 | // Create response |
144 | 172 | StickerRemovalResponse response = new StickerRemovalResponse(); |
|
0 commit comments