Skip to content

Commit

Permalink
feat: 이미지 삭제 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee committed Oct 8, 2024
1 parent 5fbefa8 commit 95ae8dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -58,6 +59,6 @@ private String uploadImageToStorage(InputStream inputStream, MultipartFile image
.build();

s3Client.putObject(putObjectRequest, fromInputStream(inputStream, contentLength));
return fileName;
return imageName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
}
}

0 comments on commit 95ae8dd

Please sign in to comment.