diff --git a/src/main/java/com/weatherfit/board/service/BoardService.java b/src/main/java/com/weatherfit/board/service/BoardService.java index b011a92..c80ceed 100644 --- a/src/main/java/com/weatherfit/board/service/BoardService.java +++ b/src/main/java/com/weatherfit/board/service/BoardService.java @@ -165,26 +165,25 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St throw new RuntimeException(e); } - for (ImageEntity imageEntity : originalBoard.getImages()) { - imageService.deleteImage(imageEntity); + for (Integer imageId : boardUpdateDTO.getDeletedImages()) { + Optional optionalImage = imageRepository.findById(imageId); + ImageEntity imageEntity = optionalImage.orElseThrow(() -> new IllegalArgumentException("해당 이미지가 존재하지 않습니다. id=" + imageId)); imageRepository.delete(imageEntity); } - originalBoard.getImages().clear(); for (MultipartFile image : images) { String imageUrl = imageService.saveImage(image); String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); - if (!imageRepository.existsByImageUrl(imageUrl)) { - ImageEntity imageEntity = ImageEntity.builder() - .fileName(fileName) - .imageUrl(imageUrl) - .boardId(originalBoard) - .build(); - imageRepository.save(imageEntity); + ImageEntity imageEntity = ImageEntity.builder() + .fileName(fileName) + .imageUrl(imageUrl) + .boardId(originalBoard) + .build(); + imageRepository.save(imageEntity); + + originalBoard.getImages().add(imageEntity); - originalBoard.getImages().add(imageEntity); - } } BoardEntity boardEntity = BoardEntity.builder() @@ -194,8 +193,6 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St .hashTag(boardUpdateDTO.getHashTag()) .build(); - BoardEntity savedBoard = boardRepository.save(boardEntity); - String afterJoiendString = originalBoard.getTemperature() + "/" + String.join("/", originalBoard.getCategory()) + ":" + String.join("/", boardUpdateDTO.getCategory()); String afterJoiendString2 = String.join("/", originalBoard.getHashTag()) + ":" + String.join("/", boardUpdateDTO.getHashTag()); diff --git a/src/main/java/com/weatherfit/board/service/ImageService.java b/src/main/java/com/weatherfit/board/service/ImageService.java index 817d949..1a998af 100644 --- a/src/main/java/com/weatherfit/board/service/ImageService.java +++ b/src/main/java/com/weatherfit/board/service/ImageService.java @@ -50,13 +50,10 @@ public String saveImage(MultipartFile file) { } - public void deleteImage(ImageEntity imageEntity) { - String imageUrl = imageEntity.getImageUrl(); -// String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); - amazonS3Client.deleteObject(bucketName, imageUrl); - } +// public void deleteImage(ImageEntity imageEntity) { +// String imageUrl = imageEntity.getImageUrl(); +//// String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); +// amazonS3Client.deleteObject(bucketName, imageUrl); +// } } - - -