From 9fbadf8fc718c258f456e651783fe4651f9de069 Mon Sep 17 00:00:00 2001 From: Hyunuk Date: Sat, 11 Nov 2023 06:55:13 +0900 Subject: [PATCH] =?UTF-8?q?[tomato-market/plan#22]=20Controller=20?= =?UTF-8?q?=EB=B0=8F=20Test=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../market/controller/BoardController.java | 25 ++++++++++++++++--- .../market/service/impl/BoardServiceImpl.java | 5 +++- .../controller/BoardControllerTest.java | 6 +++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/tomato/market/controller/BoardController.java b/src/main/java/com/tomato/market/controller/BoardController.java index a72f062..80603c7 100644 --- a/src/main/java/com/tomato/market/controller/BoardController.java +++ b/src/main/java/com/tomato/market/controller/BoardController.java @@ -209,16 +209,35 @@ public ResponseDto getFavorite(String userId, Integer postNum) { } @GetMapping("/board/favorite/list") - public ResponseDto> getFavoriteList(String userId) { + public ResponseDto getFavoriteList(String userId) { logger.info("BoardController.getFavoriteList() is called"); + // 관심 목록 번호 조회 List favoriteDtoList = boardService.getFavoriteList(userId); logger.info("BoardController.getFavoriteList() : 관심 목록 조회 성공"); - return ResponseDto.>builder() + // 관심 목록에 대한 PostList 조회 + // fovoriteList에 대한 가공 -> 조회 + List postDtoList = new ArrayList<>(); + for (FavoriteDto favoriteDto : favoriteDtoList) { + postDtoList.add(boardService.getPost(favoriteDto.getPostNum())); + } + + // PostList에 대한 ImageList 조회 + List imageDtoList = new ArrayList<>(); + for (PostDto postDto : postDtoList) { + imageDtoList.add(boardService.getPostImage(postDto.getPostNum())); + } + + PostListResponseDto responseDto = PostListResponseDto.builder() + .postList(postDtoList) + .imageList(imageDtoList) + .build(); + + return ResponseDto.builder() .status(HttpStatus.OK) .message("관심 목록 조회 성공") - .data(favoriteDtoList) + .data(responseDto) // 어떤 형식으로 넘기지? .build(); } } diff --git a/src/main/java/com/tomato/market/service/impl/BoardServiceImpl.java b/src/main/java/com/tomato/market/service/impl/BoardServiceImpl.java index d36dcb3..d24714d 100644 --- a/src/main/java/com/tomato/market/service/impl/BoardServiceImpl.java +++ b/src/main/java/com/tomato/market/service/impl/BoardServiceImpl.java @@ -249,7 +249,10 @@ public List getFavoriteList(String userId) { logger.info("BoardServiceImpl.getFavoriteList() : 관심 목록 조회 성공"); List favoriteDtoList = new ArrayList<>(); for (FavoriteEntity favoriteEntity : favoriteEntities) { - favoriteDtoList.add(FavoriteDto.toFavoriteDto(favoriteEntity)); + if (favoriteEntity.getStatus() == 1) { + // JPA로 바꿔도 가능 + favoriteDtoList.add(FavoriteDto.toFavoriteDto(favoriteEntity)); + } } return favoriteDtoList; } diff --git a/src/test/java/com/tomato/market/controller/BoardControllerTest.java b/src/test/java/com/tomato/market/controller/BoardControllerTest.java index bebb05c..8f539bd 100644 --- a/src/test/java/com/tomato/market/controller/BoardControllerTest.java +++ b/src/test/java/com/tomato/market/controller/BoardControllerTest.java @@ -488,13 +488,19 @@ void getFavoriteFailure() throws Exception { @DisplayName("게시글_관심_목록_조회_성공") void getFavoriteListSuccess() throws Exception { given(boardService.getFavoriteList(userId)).willReturn(favoriteDtoList); + given(boardService.getPost(postNum)).willReturn(postDto); + given(boardService.getPostImage(postNum)).willReturn(imageDto); mockMvc.perform(get("/api/board/favorite/list").param("userId", userId)) .andExpect(status().isOk()) .andExpect(jsonPath("$.message", is("관심 목록 조회 성공"))) + .andExpect(jsonPath("$.data.postList").exists()) + .andExpect(jsonPath("$.data.imageList").exists()) .andDo(print()); verify(boardService).getFavoriteList(userId); + verify(boardService, times(2)).getPost(postNum); + verify(boardService, times(2)).getPostImage(postNum); } @Test