From 76a85d0cb65d6e91a39fb0c33b266b6b54248cde Mon Sep 17 00:00:00 2001 From: Johyunik Date: Sat, 2 Dec 2023 13:04:19 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EB=B6=80=EB=B6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/BoardController.java | 7 ++-- .../board/service/BoardService.java | 36 +++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/weatherfit/board/controller/BoardController.java b/src/main/java/com/weatherfit/board/controller/BoardController.java index dfdea2d..e0aafca 100644 --- a/src/main/java/com/weatherfit/board/controller/BoardController.java +++ b/src/main/java/com/weatherfit/board/controller/BoardController.java @@ -140,14 +140,15 @@ public boolean patchBoard( @RequestHeader("decodedToken") String nickName, @PathVariable int boardId, @RequestPart("board") String boardJson, - @RequestPart(value = "images", required = false) MultipartFile[] images) throws UnsupportedEncodingException { + @RequestPart(value = "images", required = false) MultipartFile[] newImages, + @RequestPart(value = "deleteImageIds", required = false) List deleteImageIds) throws UnsupportedEncodingException { String decodedNickname = new String(Base64.getDecoder().decode(nickName), "UTF-8"); - - boardService.patchBoard(boardId, boardJson, images, decodedNickname); + boardService.patchBoard(boardId, boardJson, newImages, deleteImageIds, decodedNickname); return true; } + // 게시글 삭제 @DeleteMapping("/delete/{boardId}") @ResponseBody diff --git a/src/main/java/com/weatherfit/board/service/BoardService.java b/src/main/java/com/weatherfit/board/service/BoardService.java index 18aaea7..ef70fc4 100644 --- a/src/main/java/com/weatherfit/board/service/BoardService.java +++ b/src/main/java/com/weatherfit/board/service/BoardService.java @@ -148,16 +148,33 @@ public BoardEntity insertBoard(BoardEntity board) { return board; } + // 게시글 수정 // 게시글 수정 @Transactional - public void patchBoard(int boardId, String boardJson, MultipartFile[] images, String nickName) { + public void patchBoard(int boardId, String boardJson, MultipartFile[] newImages, List deleteImageIds, String nickName) { Optional optionalBoard = Optional.ofNullable(boardRepository.findById(boardId)); BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId)); ObjectMapper objectMapper = new ObjectMapper(); - imageRepository.deleteByBoardId(originalBoard); + // 삭제할 이미지 삭제 + for (String id : deleteImageIds) { + imageRepository.deleteById(Integer.valueOf(id)); + } + + // 새로운 이미지 저장 + for (MultipartFile image : newImages) { + String imageUrl = imageService.saveImage(image); + + ImageEntity imageEntity = ImageEntity.builder() + .image_url(imageUrl) + .boardId(originalBoard) + .build(); + imageRepository.save(imageEntity); + } + + // 게시글 정보 수정 BoardUpdateDTO boardUpdateDTO; try { boardUpdateDTO = objectMapper.readValue(boardJson, BoardUpdateDTO.class); @@ -166,6 +183,7 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St } catch (JsonProcessingException e) { throw new RuntimeException(e); } + BoardEntity boardEntity = BoardEntity.builder() .boardId(boardId) .content(boardUpdateDTO.getContent()) @@ -175,17 +193,6 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St BoardEntity savedBoard = boardRepository.save(boardEntity); - for (MultipartFile image : images) { - String imageUrl = imageService.saveImage(image); - - ImageEntity imageEntity = ImageEntity.builder() - .image_url(imageUrl) - .boardId(savedBoard) - .build(); - imageRepository.save(imageEntity); - } - - String afterJoiendString = originalBoard.getTemperature() + "/" + String.join("/", originalBoard.getCategory()) + ":" + String.join("/", boardUpdateDTO.getCategory()); String afterJoiendString2 = String.join("/", originalBoard.getHashTag()) + ":" + String.join("/", boardUpdateDTO.getHashTag()); @@ -196,13 +203,12 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St originalBoard.setImages(boardUpdateDTO.getImages()); boardRepository.save(originalBoard); - // 카프카 전송 kafkaTemplate.send("category", 1, "category", afterJoiendString); kafkaTemplate.send("hashtag", 1, "hashtag", afterJoiendString2); - } + // 게시글 삭제 public void deleteBoard(int boardId) { Optional optionalBoard = Optional.ofNullable(boardRepository.findById(boardId));