diff --git a/src/main/java/com/weatherfit/board/GlobalExceptionHandler.java b/src/main/java/com/weatherfit/board/GlobalExceptionHandler.java new file mode 100644 index 0000000..db2f6f9 --- /dev/null +++ b/src/main/java/com/weatherfit/board/GlobalExceptionHandler.java @@ -0,0 +1,16 @@ +package com.weatherfit.board; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(IllegalArgumentException.class) + public ResponseEntity handleIllegalArgumentException(IllegalArgumentException e) { + return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getMessage()); + } +} + diff --git a/src/main/java/com/weatherfit/board/controller/BoardController.java b/src/main/java/com/weatherfit/board/controller/BoardController.java index e747012..4dc85b4 100644 --- a/src/main/java/com/weatherfit/board/controller/BoardController.java +++ b/src/main/java/com/weatherfit/board/controller/BoardController.java @@ -59,6 +59,13 @@ public List listBoards(@RequestParam(required = false) Str return list; } + // 내가 쓴 게시글 조회 + @GetMapping("/myList") + public List myListBoards(@RequestHeader("decodedToken") String nickName) throws UnsupportedEncodingException { + String decodedNickname = new String(Base64.getDecoder().decode(nickName), "UTF-8"); + return boardService.findNickname(decodedNickname); + } + // 게시글 상세 조회 @GetMapping("/detail/{boardId}") public BoardDetailResponseDTO detailBoard(@PathVariable int boardId) { @@ -129,13 +136,15 @@ public String insertBoard(@RequestHeader("decodedToken") String nickName, @Reque @PatchMapping(value = "/edit/{boardId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @ResponseBody public boolean patchBoard( + @RequestHeader("decodedToken") String nickName, @PathVariable int boardId, @RequestPart("board") String boardJson, @RequestPart(value = "images", required = false) MultipartFile[] images) { ObjectMapper objectMapper = new ObjectMapper(); + try { BoardUpdateDTO boardUpdateDTO = objectMapper.readValue(boardJson, BoardUpdateDTO.class); - boardService.patchBoard(boardId, boardUpdateDTO, images); + boardService.patchBoard(boardId, boardUpdateDTO, images, nickName); return true; } catch (JsonProcessingException e) { throw new RuntimeException(e); @@ -146,8 +155,9 @@ public boolean patchBoard( @DeleteMapping("/delete/{boardId}") @ResponseBody public void deleteBoard( + @RequestHeader("decodedToken") String nickName, @PathVariable int boardId) { - boardService.deleteBoard(boardId); + boardService.deleteBoard(boardId, nickName); } // 게시글 검색 diff --git a/src/main/java/com/weatherfit/board/repository/BoardRepository.java b/src/main/java/com/weatherfit/board/repository/BoardRepository.java index a2402bc..1b48a6e 100644 --- a/src/main/java/com/weatherfit/board/repository/BoardRepository.java +++ b/src/main/java/com/weatherfit/board/repository/BoardRepository.java @@ -18,6 +18,8 @@ public interface BoardRepository extends JpaRepository, Bo BoardEntity findById(int id); + List findByNickName(String nickName); + List findByCategoryInAndHashTagIn(List categories, List hashTags); List findByHashTagIn(List hashTags); List findByCategoryIn(List categories); diff --git a/src/main/java/com/weatherfit/board/service/BoardService.java b/src/main/java/com/weatherfit/board/service/BoardService.java index 6583c5c..7230fbd 100644 --- a/src/main/java/com/weatherfit/board/service/BoardService.java +++ b/src/main/java/com/weatherfit/board/service/BoardService.java @@ -47,7 +47,7 @@ public List findAll() { .category(board.getCategory()) .temperature(board.getTemperature()); - if(!board.getImages().isEmpty()) { + if (!board.getImages().isEmpty()) { builder.images(board.entityToDTO(board.getImages().get(0))); } @@ -70,7 +70,7 @@ public List findDate() { .category(board.getCategory()) .temperature(board.getTemperature()); - if(!board.getImages().isEmpty()) { + if (!board.getImages().isEmpty()) { builder.images(board.entityToDTO(board.getImages().get(0))); } @@ -93,7 +93,7 @@ public List findLike() { .category(board.getCategory()) .temperature(board.getTemperature()); - if(!board.getImages().isEmpty()) { + if (!board.getImages().isEmpty()) { builder.images(board.entityToDTO(board.getImages().get(0))); } @@ -103,6 +103,29 @@ public List findLike() { return dtoList; } + // 내가 쓴 게시글 조회 + public List findNickname(String nickName) { + List entities = boardRepository.findByNickName(nickName); + List dtoList = new ArrayList<>(); + + for (BoardEntity board : entities) { + BoardListResponseDTO.BoardListResponseDTOBuilder builder = BoardListResponseDTO.builder() + .boardId(board.getBoardId()) + .nickName(board.getNickName()) + .likeCount(likeService.countLikes(board.getBoardId())) + .hashTag(board.getHashTag()) + .category(board.getCategory()) + .temperature(board.getTemperature()); + + if (!board.getImages().isEmpty()) { + builder.images(board.entityToDTO(board.getImages().get(0))); + } + dtoList.add(builder.build()); + } + return dtoList; + } + + // 게시글 상세 조회 public BoardEntity getBoardById(int boardId) { return boardRepository.findById(boardId); @@ -125,11 +148,14 @@ public BoardEntity insertBoard(BoardEntity board) { } // 게시글 수정 - public void patchBoard(int boardId, BoardUpdateDTO boardUpdateDTO, MultipartFile[] images) { + public void patchBoard(int boardId, BoardUpdateDTO boardUpdateDTO, MultipartFile[] images, String nickName) { Optional optionalBoard = Optional.ofNullable(boardRepository.findById(boardId)); BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId)); + if (!originalBoard.getNickName().equals(nickName)) { + throw new IllegalArgumentException("게시글 수정 권한이 없습니다."); + } List imageIdsToDelete = boardUpdateDTO.getImageIdsToDelete(); if (imageIdsToDelete != null && !imageIdsToDelete.isEmpty()) { @@ -175,11 +201,15 @@ public void patchBoard(int boardId, BoardUpdateDTO boardUpdateDTO, MultipartFile } // 게시글 삭제 - public void deleteBoard(int boardId) { + public void deleteBoard(int boardId, String nickName) { Optional optionalBoard = Optional.ofNullable(boardRepository.findById(boardId)); BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId)); + if (!originalBoard.getNickName().equals(nickName)) { + throw new IllegalArgumentException("게시글 삭제 권한이 없습니다."); + } + String afterJoiendString = originalBoard.getTemperature() + "/" + String.join("/", originalBoard.getCategory()); String afterJoiendString2 = String.join("/", originalBoard.getHashTag());