diff --git a/src/main/java/com/tomato/market/controller/BoardController.java b/src/main/java/com/tomato/market/controller/BoardController.java index 8b6a144..9a2084f 100644 --- a/src/main/java/com/tomato/market/controller/BoardController.java +++ b/src/main/java/com/tomato/market/controller/BoardController.java @@ -280,4 +280,31 @@ public ResponseDto updateStatus(@RequestBody PostDto postDto) { .data(result) .build(); } + + // 판매 목록 + @GetMapping("/board/sellList") + public ResponseDto getSellList(String userId) { + logger.info("BoardController.getSellList() is called"); + + List postDtoList = boardService.getSellList(userId); + logger.info("BoardController.getSellList() : 판매 목록 조회 성공"); + + // 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(responseDto) + .build(); + } + } diff --git a/src/main/java/com/tomato/market/dao/BoardDao.java b/src/main/java/com/tomato/market/dao/BoardDao.java index 48b5fa1..369494c 100644 --- a/src/main/java/com/tomato/market/dao/BoardDao.java +++ b/src/main/java/com/tomato/market/dao/BoardDao.java @@ -31,4 +31,6 @@ public interface BoardDao { FavoriteEntity findByUserIdAndPostNum(String userId, Integer postNum); List findByUserId(String userId); + + List findPostByUserId(String userId); } diff --git a/src/main/java/com/tomato/market/dao/impl/BoardDaoImpl.java b/src/main/java/com/tomato/market/dao/impl/BoardDaoImpl.java index d809af2..b3b0200 100644 --- a/src/main/java/com/tomato/market/dao/impl/BoardDaoImpl.java +++ b/src/main/java/com/tomato/market/dao/impl/BoardDaoImpl.java @@ -188,4 +188,17 @@ public List findByUserId(String userId) { logger.info("BoardDaoImpl.findByUserId() : 데이터 조회 성공"); return favoriteEntities; } + + @Override + public List findPostByUserId(String userId) { + logger.info("BoardDaoImpl.findPostByUserId() is called"); + List postEntities = postRepository.findByUserId(userId); + if (postEntities == null) { + logger.warn("BoardDaoImpl.findPostByUserId : 데이터 조회 실패"); + return null; + } + + logger.info("BoardDaoImpl.findPostByUserId : 데이터 조회 성공"); + return postEntities; + } } diff --git a/src/main/java/com/tomato/market/data/repository/PostRepository.java b/src/main/java/com/tomato/market/data/repository/PostRepository.java index 27ee15f..6cc8c48 100644 --- a/src/main/java/com/tomato/market/data/repository/PostRepository.java +++ b/src/main/java/com/tomato/market/data/repository/PostRepository.java @@ -1,5 +1,6 @@ package com.tomato.market.data.repository; +import java.util.List; import java.util.Optional; import org.springframework.data.domain.Page; @@ -17,4 +18,5 @@ public interface PostRepository extends JpaRepository { Optional findByPostNum(Integer postNum); + List findByUserId(String userId); } diff --git a/src/main/java/com/tomato/market/service/BoardService.java b/src/main/java/com/tomato/market/service/BoardService.java index d605862..b85be10 100644 --- a/src/main/java/com/tomato/market/service/BoardService.java +++ b/src/main/java/com/tomato/market/service/BoardService.java @@ -36,4 +36,6 @@ public interface BoardService { PostDto updatePost(PostDto postDto); PostDto updateStatus(PostDto postDto); + + List getSellList(String userId); } 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 1ad2979..82139ae 100644 --- a/src/main/java/com/tomato/market/service/impl/BoardServiceImpl.java +++ b/src/main/java/com/tomato/market/service/impl/BoardServiceImpl.java @@ -301,4 +301,23 @@ public PostDto updateStatus(PostDto postDto) { logger.info("BoardServiceImpl.updateStatus() : 게시글 수정 성공"); return PostDto.toPostDto(result); } + + @Override + public List getSellList(String userId) { + logger.info("BoardServiceImpl.getSellList() is called"); + + List postEntities = boardDao.findPostByUserId(userId); + if (postEntities == null) { + logger.warn("BoardServiceImpl.getSellList() : 판매 목록 조회 실패"); + throw new BoardException("판매 목록 조회에 실패했습니다."); + } + + logger.info("BoardServiceImpl.getSellList() : 판매 목록 조회 성공"); + List postDtoList = new ArrayList<>(); + for (PostEntity postEntity : postEntities) { + postDtoList.add(PostDto.toPostDto(postEntity)); + } + + return postDtoList; + } } diff --git a/src/test/java/com/tomato/market/controller/BoardControllerTest.java b/src/test/java/com/tomato/market/controller/BoardControllerTest.java index 3999e7f..6544aad 100644 --- a/src/test/java/com/tomato/market/controller/BoardControllerTest.java +++ b/src/test/java/com/tomato/market/controller/BoardControllerTest.java @@ -599,4 +599,38 @@ void updateStatusFailure() throws Exception { verify(boardService).updateStatus(any(PostDto.class)); } + + @Test + @DisplayName("판매_목록_조회_성공") + void getSellListSuccess() throws Exception { + given(boardService.getSellList(any(String.class))).willReturn(postList); + given(boardService.getPostImage(any(Integer.class))).willReturn(imageDto); + + mockMvc.perform(get("/api/board/sellList") + .param("userId", userId) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message", is("판매 목록 조회 성공"))) + .andExpect(jsonPath("$.data.postList").exists()) + .andExpect(jsonPath("$.data.imageList").exists()) + .andDo(print()); + + verify(boardService).getSellList(any(String.class)); + verify(boardService, times(2)).getPostImage(any(Integer.class)); + } + + @Test + @DisplayName("판매_목록_조회_실패") + void getSellListFailure() throws Exception { + given(boardService.getSellList(any(String.class))).willThrow(new BoardException("판매 목록 조회에 실패했습니다.")); + + mockMvc.perform(get("/api/board/sellList") + .param("userId", userId) + ) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message", is("판매 목록 조회에 실패했습니다."))) + .andDo(print()); + + verify(boardService).getSellList(any(String.class)); + } } diff --git a/src/test/java/com/tomato/market/service/BoardServiceTest.java b/src/test/java/com/tomato/market/service/BoardServiceTest.java index 256919e..e388581 100644 --- a/src/test/java/com/tomato/market/service/BoardServiceTest.java +++ b/src/test/java/com/tomato/market/service/BoardServiceTest.java @@ -484,4 +484,29 @@ void updateStatusFailure() { verify(boardDao).findPostByPostNum(any(Integer.class)); verify(boardDao).save(any(PostEntity.class)); } + + @Test + @DisplayName("판매_목록_조회_성공") + void getSellListSuccess() { + given(boardDao.findPostByUserId(any(String.class))).willReturn(postEntities); + + BoardServiceImpl boardService = new BoardServiceImpl(boardDao); + Assertions.assertEquals(boardService.getSellList(userId).toString(), postDtoList.toString()); + + verify(boardDao).findPostByUserId(any(String.class)); + } + + @Test + @DisplayName("판매_목록_조회_실패") + void getSellListFailure() { + given(boardDao.findPostByUserId(any(String.class))).willReturn(null); + + BoardServiceImpl boardService = new BoardServiceImpl(boardDao); + BoardException exception = Assertions.assertThrows(BoardException.class, () -> { + boardService.getSellList(userId); + }); + Assertions.assertEquals(exception.getMessage(), "판매 목록 조회에 실패했습니다."); + + verify(boardDao).findPostByUserId(any(String.class)); + } }