Skip to content

Commit

Permalink
이미지 수정 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Johyunik committed Dec 4, 2023
1 parent 21cdfab commit d19d710
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ public boolean patchBoard(
@RequestHeader("decodedToken") String nickName,
@PathVariable int boardId,
@RequestPart("board") String boardJson,
@RequestPart(value = "images", required = false) MultipartFile[] images,
@RequestPart(value = "deletedImages", required = false) String[] deletedImages) throws UnsupportedEncodingException {
@RequestPart(value = "images", required = false) MultipartFile[] images
) throws UnsupportedEncodingException, JsonProcessingException {

String decodedNickname = new String(Base64.getDecoder().decode(nickName), "UTF-8");

boardService.patchBoard(boardId, boardJson, images, deletedImages, decodedNickname);
System.out.println(boardJson);


boardService.patchBoard(boardId, boardJson, images, decodedNickname);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/weatherfit/board/dto/BoardUpdateDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class BoardUpdateDTO {
private List<String> category;
private List<String> hashTag;
private List<ImageEntity> images;
private String[] deletedImages;
private Integer[] deletedImages;

}
30 changes: 13 additions & 17 deletions src/main/java/com/weatherfit/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public BoardEntity insertBoard(BoardEntity board) {

// 게시글 수정
@Transactional
public void patchBoard(int boardId, String boardJson, MultipartFile[] images, String[] deletedImages, String nickName) {
public void patchBoard(int boardId, String boardJson, MultipartFile[] images, String nickName) {
Optional<BoardEntity> optionalBoard = Optional.ofNullable(boardRepository.findById(boardId));

BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId));
Expand All @@ -172,27 +172,23 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St

for (MultipartFile image : images) {
String imageUrl = imageService.saveImage(image);
ImageEntity imageEntity = ImageEntity.builder()
.imageUrl(imageUrl)
.boardId(originalBoard)
.build();
imageRepository.save(imageEntity);

if (!imageRepository.existsByImageUrl(imageUrl)) {
ImageEntity imageEntity = ImageEntity.builder()
.imageUrl(imageUrl)
.boardId(originalBoard)
.build();
imageRepository.save(imageEntity);
originalBoard.getImages().add(imageEntity);

originalBoard.getImages().add(imageEntity);
}
}

// 삭제된 이미지 처리
if (deletedImages != null) {
for (String imageUrl : deletedImages) {
ImageEntity imageEntity = imageRepository.findByImageUrl(imageUrl);
if (imageEntity != null) {
imageRepository.delete(imageEntity);
imageService.deleteImage(imageUrl);
if (boardUpdateDTO.getDeletedImages() != null) {
for (Integer imageId : boardUpdateDTO.getDeletedImages()) {
Optional<ImageEntity> optionalImageEntity = imageRepository.findById(imageId);
ImageEntity imageEntity = optionalImageEntity.orElseThrow(() -> new IllegalArgumentException("해당 이미지가 존재하지 않습니다. id=" + imageId));

}
imageRepository.deleteById(imageId);
imageService.deleteImage(imageEntity.getImageUrl());
}
}

Expand Down

0 comments on commit d19d710

Please sign in to comment.