Skip to content

Commit

Permalink
게시글 수정 부분 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Johyunik committed Dec 2, 2023
1 parent 7d7919e commit 76a85d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/com/weatherfit/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> deleteImageIds, String nickName) {
Optional<BoardEntity> 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);
Expand All @@ -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())
Expand All @@ -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());

Expand All @@ -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<BoardEntity> optionalBoard = Optional.ofNullable(boardRepository.findById(boardId));
Expand Down

0 comments on commit 76a85d0

Please sign in to comment.