|
1 | 1 | package com.datadoghq.stickerlandia.stickeraward.sticker; |
2 | 2 |
|
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; |
6 | 3 | import com.datadoghq.stickerlandia.stickeraward.sticker.dto.CreateStickerRequest; |
7 | 4 | import com.datadoghq.stickerlandia.stickeraward.sticker.dto.CreateStickerResponse; |
8 | 5 | import com.datadoghq.stickerlandia.stickeraward.sticker.dto.GetAllStickersResponse; |
9 | | -import com.datadoghq.stickerlandia.stickeraward.sticker.dto.GetStickerAssignmentsResponse; |
10 | 6 | import com.datadoghq.stickerlandia.stickeraward.sticker.dto.StickerDTO; |
11 | 7 | import com.datadoghq.stickerlandia.stickeraward.sticker.dto.StickerImageUploadResponse; |
12 | 8 | import com.datadoghq.stickerlandia.stickeraward.sticker.dto.UpdateStickerRequest; |
13 | 9 | import com.datadoghq.stickerlandia.stickeraward.sticker.entity.Sticker; |
14 | 10 | import io.quarkus.panache.common.Sort; |
15 | 11 | import jakarta.enterprise.context.ApplicationScoped; |
| 12 | +import jakarta.persistence.PersistenceException; |
16 | 13 | import jakarta.transaction.Transactional; |
17 | 14 | import java.io.InputStream; |
18 | 15 | import java.time.Instant; |
19 | 16 | import java.util.Date; |
20 | 17 | import java.util.List; |
21 | 18 | import java.util.UUID; |
22 | 19 | import java.util.stream.Collectors; |
| 20 | +import org.hibernate.exception.ConstraintViolationException; |
23 | 21 |
|
24 | 22 | /** Repository class for managing sticker operations. */ |
25 | 23 | @ApplicationScoped |
@@ -154,35 +152,21 @@ public StickerImageUploadResponse uploadStickerImage( |
154 | 152 | return null; // Placeholder return, actual implementation needed |
155 | 153 | } |
156 | 154 |
|
| 155 | + |
157 | 156 | /** |
158 | | - * Gets assignments for a specific sticker. |
| 157 | + * Updates the image key for a sticker. |
159 | 158 | * |
160 | 159 | * @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 |
| 160 | + * @param imageKey the new image key |
164 | 161 | */ |
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; |
| 162 | + @Transactional |
| 163 | + public void updateStickerImageKey(String stickerId, String imageKey) { |
| 164 | + Sticker sticker = Sticker.findById(stickerId); |
| 165 | + if (sticker != null) { |
| 166 | + sticker.setImageKey(imageKey); |
| 167 | + sticker.setUpdatedAt(Instant.now()); |
| 168 | + sticker.persist(); |
| 169 | + } |
186 | 170 | } |
187 | 171 |
|
188 | 172 | private StickerDTO toStickerMetadata(Sticker sticker) { |
@@ -213,18 +197,6 @@ private StickerDTO convertToDto(Sticker sticker) { |
213 | 197 | return toStickerMetadata(sticker); |
214 | 198 | } |
215 | 199 |
|
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 | 200 |
|
229 | 201 | /** |
230 | 202 | * Updates sticker metadata (alias for updateSticker). |
@@ -252,29 +224,16 @@ public boolean deleteSticker(String stickerId) { |
252 | 224 | return false; |
253 | 225 | } |
254 | 226 |
|
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(); |
| 227 | + try { |
| 228 | + sticker.delete(); |
| 229 | + return true; |
| 230 | + } catch (PersistenceException e) { |
| 231 | + // If we can't delete because of a constraint violation, the |
| 232 | + // sticker must be assigned |
| 233 | + if (e.getCause() instanceof ConstraintViolationException) { |
| 234 | + throw new IllegalStateException("Cannot delete sticker that is assigned to users"); |
| 235 | + } |
| 236 | + throw e; |
278 | 237 | } |
279 | 238 | } |
280 | 239 | } |
0 commit comments