11package com .datadoghq .stickerlandia .stickeraward .sticker ;
22
3- import com .datadoghq .stickerlandia .stickeraward .award .dto .UserAssignmentDTO ;
4- import com .datadoghq .stickerlandia .stickeraward .award .entity .StickerAssignment ;
5- import com .datadoghq .stickerlandia .stickeraward .common .dto .PagedResponse ;
63import com .datadoghq .stickerlandia .stickeraward .sticker .dto .CreateStickerRequest ;
74import com .datadoghq .stickerlandia .stickeraward .sticker .dto .CreateStickerResponse ;
85import com .datadoghq .stickerlandia .stickeraward .sticker .dto .GetAllStickersResponse ;
9- import com .datadoghq .stickerlandia .stickeraward .sticker .dto .GetStickerAssignmentsResponse ;
106import com .datadoghq .stickerlandia .stickeraward .sticker .dto .StickerDTO ;
117import com .datadoghq .stickerlandia .stickeraward .sticker .dto .StickerImageUploadResponse ;
128import com .datadoghq .stickerlandia .stickeraward .sticker .dto .UpdateStickerRequest ;
139import com .datadoghq .stickerlandia .stickeraward .sticker .entity .Sticker ;
1410import io .quarkus .panache .common .Sort ;
1511import jakarta .enterprise .context .ApplicationScoped ;
12+ import jakarta .persistence .PersistenceException ;
1613import jakarta .transaction .Transactional ;
1714import java .io .InputStream ;
1815import java .time .Instant ;
1916import java .util .Date ;
2017import java .util .List ;
2118import java .util .UUID ;
2219import java .util .stream .Collectors ;
20+ import org .hibernate .exception .ConstraintViolationException ;
2321
2422/** Repository class for managing sticker operations. */
2523@ ApplicationScoped
@@ -155,34 +153,19 @@ public StickerImageUploadResponse uploadStickerImage(
155153 }
156154
157155 /**
158- * Gets assignments for a specific sticker.
156+ * Updates the image key for a sticker.
159157 *
160158 * @param stickerId the ID of the sticker
161- * @param page the page number (0-based)
162- * @param size the page size
163- * @return response containing paginated assignments
159+ * @param imageKey the new image key
164160 */
165- public GetStickerAssignmentsResponse getStickerAssignments (
166- String stickerId , int page , int size ) {
167- final List <UserAssignmentDTO > userAssignments =
168- StickerAssignment .findActiveByStickerId (stickerId ).stream ()
169- .skip ((long ) page * size )
170- .limit (size )
171- .map (this ::convertToUserAssignmentDto )
172- .collect (Collectors .toList ());
173-
174- PagedResponse pagination = new PagedResponse ();
175- pagination .setPage (page );
176- pagination .setSize (size );
177- pagination .setTotal ((int ) StickerAssignment .countActiveByStickerId (stickerId ));
178- final long totalCount = StickerAssignment .countActiveByStickerId (stickerId );
179- pagination .setTotalPages ((int ) Math .ceil ((double ) totalCount / size ));
180-
181- GetStickerAssignmentsResponse response = new GetStickerAssignmentsResponse ();
182- response .setStickerId (stickerId );
183- response .setAssignments (userAssignments );
184- response .setPagination (pagination );
185- return response ;
161+ @ Transactional
162+ public void updateStickerImageKey (String stickerId , String imageKey ) {
163+ Sticker sticker = Sticker .findById (stickerId );
164+ if (sticker != null ) {
165+ sticker .setImageKey (imageKey );
166+ sticker .setUpdatedAt (Instant .now ());
167+ sticker .persist ();
168+ }
186169 }
187170
188171 private StickerDTO toStickerMetadata (Sticker sticker ) {
@@ -213,19 +196,6 @@ private StickerDTO convertToDto(Sticker sticker) {
213196 return toStickerMetadata (sticker );
214197 }
215198
216- /**
217- * Converts a StickerAssignment entity to a UserAssignmentDTO.
218- *
219- * @param assignment the sticker assignment entity to convert
220- * @return the converted UserAssignmentDTO
221- */
222- private UserAssignmentDTO convertToUserAssignmentDto (StickerAssignment assignment ) {
223- UserAssignmentDTO dto = new UserAssignmentDTO ();
224- dto .setUserId (assignment .getUserId ());
225- dto .setAssignedAt (Date .from (assignment .getAssignedAt ()));
226- return dto ;
227- }
228-
229199 /**
230200 * Updates sticker metadata (alias for updateSticker).
231201 *
@@ -252,29 +222,16 @@ public boolean deleteSticker(String stickerId) {
252222 return false ;
253223 }
254224
255- // Check if sticker is assigned to any users
256- long assignmentCount = StickerAssignment .countActiveByStickerId (stickerId );
257- if (assignmentCount > 0 ) {
258- throw new IllegalStateException ("Cannot delete sticker that is assigned to users" );
259- }
260-
261- sticker .delete ();
262- return true ;
263- }
264-
265- /**
266- * Updates the image key for a sticker.
267- *
268- * @param stickerId the ID of the sticker
269- * @param imageKey the new image key
270- */
271- @ Transactional
272- public void updateStickerImageKey (String stickerId , String imageKey ) {
273- Sticker sticker = Sticker .findById (stickerId );
274- if (sticker != null ) {
275- sticker .setImageKey (imageKey );
276- sticker .setUpdatedAt (Instant .now ());
277- sticker .persist ();
225+ try {
226+ sticker .delete ();
227+ return true ;
228+ } catch (PersistenceException e ) {
229+ // If we can't delete because of a constraint violation, the
230+ // sticker must be assigned
231+ if (e .getCause () instanceof ConstraintViolationException ) {
232+ throw new IllegalStateException ("Cannot delete sticker that is assigned to users" );
233+ }
234+ throw e ;
278235 }
279236 }
280237}
0 commit comments