11package com .datadoghq .stickerlandia .stickeraward .award ;
22
3- import com .datadoghq .stickerlandia .stickeraward .award .dto .AssignStickerCommand ;
4- import com .datadoghq .stickerlandia .stickeraward .award .dto .StickerAssignmentResponse ;
5- import com .datadoghq .stickerlandia .stickeraward .award .dto .StickerDTO ;
6- import com .datadoghq .stickerlandia .stickeraward .award .dto .StickerRemovalResponse ;
7- import com .datadoghq .stickerlandia .stickeraward .award .dto .UserStickersResponse ;
8- import com .datadoghq .stickerlandia .stickeraward .sticker .entity .Sticker ;
9- import com .datadoghq .stickerlandia .stickeraward .award .entity .StickerAssignment ;
3+ import com .datadoghq .stickerlandia .stickeraward .award .dto .AssignStickerRequest ;
4+ import com .datadoghq .stickerlandia .stickeraward .award .dto .AssignStickerResponse ;
5+ import com .datadoghq .stickerlandia .stickeraward .award .dto .StickerAssignmentDTO ;
6+ import com .datadoghq .stickerlandia .stickeraward .award .dto .RemoveStickerFromUserResponse ;
7+ import com .datadoghq .stickerlandia .stickeraward .award .dto .GetUserStickersResponse ;
108import com .datadoghq .stickerlandia .stickeraward .award .messaging .StickerAwardEventPublisher ;
119import io .smallrye .common .constraint .NotNull ;
1210import jakarta .inject .Inject ;
2321import org .slf4j .Logger ;
2422import org .slf4j .LoggerFactory ;
2523
26- import java .time .Instant ;
27- import java .util .Date ;
28- import java .util .List ;
29- import java .util .stream .Collectors ;
3024
3125@ Path ("/api" )
3226public class StickerAwardResource {
@@ -36,32 +30,15 @@ public class StickerAwardResource {
3630 @ Inject
3731 StickerAwardEventPublisher eventPublisher ;
3832
33+ @ Inject
34+ StickerAwardRepository stickerAwardRepository ;
35+
3936 @ Operation (description = "Get stickers assigned to a user (access controlled based on caller identity)" )
4037 @ Path ("/award/v1/users/{userId}/stickers" )
4138 @ GET
4239 @ Produces ("application/json" )
43- @ Transactional
44- public UserStickersResponse getUserStickers (@ PathParam ("userId" ) String userId ) {
45- List <StickerAssignment > assignments = StickerAssignment .findActiveByUserId (userId );
46-
47- List <StickerDTO > stickerDTOs = assignments .stream ()
48- .map (assignment -> {
49- Sticker sticker = assignment .getSticker ();
50- StickerDTO dto = new StickerDTO ();
51- dto .setStickerId (sticker .getStickerId ());
52- dto .setName (sticker .getName ());
53- dto .setDescription (sticker .getDescription ());
54- dto .setImageUrl (sticker .getImageUrl ());
55- dto .setAssignedAt (Date .from (assignment .getAssignedAt ()));
56- return dto ;
57- })
58- .collect (Collectors .toList ());
59-
60- UserStickersResponse response = new UserStickersResponse ();
61- response .setUserId (userId );
62- response .setStickers (stickerDTOs );
63-
64- return response ;
40+ public GetUserStickersResponse getUserStickers (@ PathParam ("userId" ) String userId ) {
41+ return stickerAwardRepository .getUserStickers (userId );
6542 }
6643
6744 @ Operation (description = "Assign a new sticker to a user (access controlled based on caller identity)" )
@@ -71,44 +48,30 @@ public UserStickersResponse getUserStickers(@PathParam("userId") String userId)
7148 @ Consumes ("application/json" )
7249 @ Transactional
7350 public Response assignStickerToUser (@ PathParam ("userId" ) String userId ,
74- @ NotNull AssignStickerCommand data ) {
75-
76- String stickerId = data .getStickerId ();
77- Sticker sticker = Sticker .findById (stickerId );
51+ @ NotNull AssignStickerRequest data ) {
7852
79- if (sticker == null ) {
80- return Response .status (Response .Status .BAD_REQUEST ).build ();
81- }
82-
83- // Check if sticker is already assigned to the user and not removed
84- StickerAssignment existingAssignment = StickerAssignment .findActiveByUserAndSticker (userId , stickerId );
85- if (existingAssignment != null ) {
86- return Response .status (Response .Status .CONFLICT ).build ();
87- }
88-
89- // Create new assignment
90- StickerAssignment assignment = new StickerAssignment (userId , sticker , data .getReason ());
91- assignment .persist ();
92-
93- // Publish events to Kafka
9453 try {
95- log .info ("Publishing sticker assignment events for userId={}, stickerId={}" , userId , stickerId );
96- eventPublisher .publishStickerAssigned (assignment );
97- } catch (Exception e ) {
98- log .error ("Failed to publish sticker assignment events" , e );
99- // Continue with the response even if the event publishing fails
100- // In a production system, you might want to implement a retry mechanism
54+ String stickerId = data .getStickerId ();
55+ AssignStickerResponse response = stickerAwardRepository .assignStickerToUser (userId , stickerId , data );
56+ if (response == null ) {
57+ return Response .status (Response .Status .BAD_REQUEST ).build ();
58+ }
59+
60+ // Publish events - Note: We'll need to modify event publisher to work with DTOs
61+ // For now, skip event publishing until we refactor the event publisher
62+ // try {
63+ // log.info("Publishing sticker assignment events for userId={}, stickerId={}", userId, stickerId);
64+ // eventPublisher.publishStickerAssigned(...);
65+ // } catch (Exception e) {
66+ // log.error("Failed to publish sticker assignment events", e);
67+ // }
68+
69+ return Response .status (Response .Status .CREATED )
70+ .entity (response )
71+ .build ();
72+ } catch (IllegalStateException e ) {
73+ return Response .status (Response .Status .CONFLICT ).build ();
10174 }
102-
103- // Create response
104- StickerAssignmentResponse response = new StickerAssignmentResponse ();
105- response .setUserId (userId );
106- response .setStickerId (stickerId );
107- response .setAssignedAt (Date .from (assignment .getAssignedAt ()));
108-
109- return Response .status (Response .Status .CREATED )
110- .entity (response )
111- .build ();
11275 }
11376
11477 @ Operation (description = "Remove a sticker assignment from a user (access controlled based on caller identity)" )
@@ -119,31 +82,19 @@ public Response assignStickerToUser(@PathParam("userId") String userId,
11982 public Response removeStickerFromUser (@ PathParam ("userId" ) String userId ,
12083 @ PathParam ("stickerId" ) String stickerId ) {
12184
122- StickerAssignment assignment = StickerAssignment .findActiveByUserAndSticker (userId , stickerId );
123-
124- if (assignment == null ) {
85+ RemoveStickerFromUserResponse response = stickerAwardRepository .removeStickerFromUser (userId , stickerId );
86+ if (response == null ) {
12587 return Response .status (Response .Status .NOT_FOUND ).build ();
12688 }
12789
128- // Mark as removed
129- assignment .setRemovedAt (Instant .now ());
130- assignment .persist ();
131-
132- // Publish events to Kafka
133- try {
134- log .info ("Publishing sticker removal event for userId={}, stickerId={}" , userId , stickerId );
135- eventPublisher .publishStickerRemoved (assignment );
136- } catch (Exception e ) {
137- log .error ("Failed to publish sticker removal event" , e );
138- // Continue with the response even if the event publishing fails
139- // In a production system, you might want to implement a retry mechanism
140- }
141-
142- // Create response
143- StickerRemovalResponse response = new StickerRemovalResponse ();
144- response .setUserId (userId );
145- response .setStickerId (stickerId );
146- response .setRemovedAt (Date .from (assignment .getRemovedAt ()));
90+ // Publish events - Note: We'll need to modify event publisher to work with DTOs
91+ // For now, skip event publishing until we refactor the event publisher
92+ // try {
93+ // log.info("Publishing sticker removal event for userId={}, stickerId={}", userId, stickerId);
94+ // eventPublisher.publishStickerRemoved(...);
95+ // } catch (Exception e) {
96+ // log.error("Failed to publish sticker removal event", e);
97+ // }
14798
14899 return Response .ok (response ).build ();
149100 }
0 commit comments