From 95ae8dd17c0679c6799f98034e0ed6e12f42630a Mon Sep 17 00:00:00 2001 From: Arachne <66822642+Arachneee@users.noreply.github.com> Date: Tue, 8 Oct 2024 16:38:30 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/haengdong/application/EventService.java | 13 +++++++++++++ .../{ImageUploadService.java => ImageService.java} | 7 ++++--- .../presentation/admin/AdminEventController.java | 12 +++++++++++- 3 files changed, 28 insertions(+), 4 deletions(-) rename server/src/main/java/server/haengdong/application/{ImageUploadService.java => ImageService.java} (90%) diff --git a/server/src/main/java/server/haengdong/application/EventService.java b/server/src/main/java/server/haengdong/application/EventService.java index a2867fbf0..73db3023b 100644 --- a/server/src/main/java/server/haengdong/application/EventService.java +++ b/server/src/main/java/server/haengdong/application/EventService.java @@ -121,4 +121,17 @@ public List<EventImageAppResponse> findImages(String token) { .map(image -> new EventImageAppResponse(baseUrl + image.getName())) .toList(); } + + @Transactional + public String deleteImage(String token, Long imageId) { + EventImage eventImage = eventImageRepository.findById(imageId) + .orElseThrow(() -> new HaengdongException(HaengdongErrorCode.IMAGE_NOT_FOUND)); + + Event event = eventImage.getEvent(); + if (event.isTokenMismatch(token)) { + throw new AuthenticationException(HaengdongErrorCode.PASSWORD_INVALID); + } + eventImageRepository.delete(eventImage); + return eventImage.getName(); + } } diff --git a/server/src/main/java/server/haengdong/application/ImageUploadService.java b/server/src/main/java/server/haengdong/application/ImageService.java similarity index 90% rename from server/src/main/java/server/haengdong/application/ImageUploadService.java rename to server/src/main/java/server/haengdong/application/ImageService.java index a3bef9b01..bae5d7612 100644 --- a/server/src/main/java/server/haengdong/application/ImageUploadService.java +++ b/server/src/main/java/server/haengdong/application/ImageService.java @@ -15,6 +15,7 @@ import server.haengdong.exception.HaengdongErrorCode; import server.haengdong.exception.HaengdongException; import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; import software.amazon.awssdk.services.s3.model.PutObjectRequest; @Slf4j @@ -46,8 +47,8 @@ private ImageNameAppResponse uploadImage(MultipartFile image) { } private String uploadImageToStorage(InputStream inputStream, MultipartFile image) { - String fileName = UUID.randomUUID() + image.getOriginalFilename(); - String key = directoryPath + fileName; + String imageName = UUID.randomUUID() + image.getOriginalFilename(); + String key = directoryPath + imageName; long contentLength = image.getSize(); PutObjectRequest putObjectRequest = PutObjectRequest.builder() @@ -58,6 +59,6 @@ private String uploadImageToStorage(InputStream inputStream, MultipartFile image .build(); s3Client.putObject(putObjectRequest, fromInputStream(inputStream, contentLength)); - return fileName; + return imageName; } } diff --git a/server/src/main/java/server/haengdong/presentation/admin/AdminEventController.java b/server/src/main/java/server/haengdong/presentation/admin/AdminEventController.java index 502e4c0ed..976858427 100644 --- a/server/src/main/java/server/haengdong/presentation/admin/AdminEventController.java +++ b/server/src/main/java/server/haengdong/presentation/admin/AdminEventController.java @@ -29,7 +29,6 @@ public class AdminEventController { public ResponseEntity<Void> authenticate() { return ResponseEntity.ok().build(); } - @PatchMapping("/api/admin/events/{eventId}") public ResponseEntity<Void> updateEvent( @PathVariable("eventId") String token, @@ -48,4 +47,15 @@ public void uploadImages( List<ImageNameAppResponse> imageNames = imageUploadService.uploadImages(images); eventService.saveImages(token, imageNames); } + + @DeleteMapping("/api/admin/events/{eventId}/images/{imageId}") + public ResponseEntity<Void> deleteImage( + @PathVariable("eventId") String token, + @PathVariable("imageId") Long imageId + ) { + String imageName = eventService.deleteImage(token, imageId); + imageUploadService.deleteImage(imageName); + + return ResponseEntity.ok().build(); + } }