diff --git a/src/main/java/com/fab/banggabgo/controller/ArticleController.java b/src/main/java/com/fab/banggabgo/controller/ArticleController.java index 599b524..4f797fd 100644 --- a/src/main/java/com/fab/banggabgo/controller/ArticleController.java +++ b/src/main/java/com/fab/banggabgo/controller/ArticleController.java @@ -43,6 +43,14 @@ public ResponseEntity getArticle( return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity(); } + @GetMapping("/users/{userId}") + public ResponseEntity getUserArticles( + @PathVariable int userId + ) { + var result = articleService.getUserArticles(userId); + return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity(); + } + @PutMapping("/{id}") public ResponseEntity putArticle( @AuthenticationPrincipal User user, diff --git a/src/main/java/com/fab/banggabgo/dto/article/ArticleInfoDto.java b/src/main/java/com/fab/banggabgo/dto/article/ArticleInfoDto.java new file mode 100644 index 0000000..dfb373d --- /dev/null +++ b/src/main/java/com/fab/banggabgo/dto/article/ArticleInfoDto.java @@ -0,0 +1,26 @@ +package com.fab.banggabgo.dto.article; + +import com.fab.banggabgo.entity.Article; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ArticleInfoDto { + + private Integer id; + private String title; + + public static ArticleInfoDto toDto(Article article) { + return ArticleInfoDto.builder() + .id(article.getId()) + .title(article.getTitle()) + .build(); + } +} diff --git a/src/main/java/com/fab/banggabgo/repository/ArticleRepositoryCustom.java b/src/main/java/com/fab/banggabgo/repository/ArticleRepositoryCustom.java index e2e909a..0e8f823 100644 --- a/src/main/java/com/fab/banggabgo/repository/ArticleRepositoryCustom.java +++ b/src/main/java/com/fab/banggabgo/repository/ArticleRepositoryCustom.java @@ -1,5 +1,6 @@ package com.fab.banggabgo.repository; +import com.fab.banggabgo.dto.article.ArticleInfoDto; import com.fab.banggabgo.dto.mycontent.FavoriteArticleDto; import com.fab.banggabgo.dto.mycontent.MyArticleDto; import com.fab.banggabgo.entity.Article; @@ -20,4 +21,6 @@ Page
getArticleByFilter(Pageable pageable, boolean isRecruiting, String List getMyArticle(User user); List getFavoriteArticle(User user); + + List getUserArticle(User user); } diff --git a/src/main/java/com/fab/banggabgo/repository/impl/ArticleRepositoryImpl.java b/src/main/java/com/fab/banggabgo/repository/impl/ArticleRepositoryImpl.java index ddfe56d..c426e9c 100644 --- a/src/main/java/com/fab/banggabgo/repository/impl/ArticleRepositoryImpl.java +++ b/src/main/java/com/fab/banggabgo/repository/impl/ArticleRepositoryImpl.java @@ -1,5 +1,6 @@ package com.fab.banggabgo.repository.impl; +import com.fab.banggabgo.dto.article.ArticleInfoDto; import com.fab.banggabgo.dto.mycontent.FavoriteArticleDto; import com.fab.banggabgo.dto.mycontent.MyArticleDto; import com.fab.banggabgo.entity.Article; @@ -105,7 +106,7 @@ public List getMyArticle(User user) { .stream().map(MyArticleDto::toDto) .collect(Collectors.toList()); } - + public List getFavoriteArticle(User user){ var getMyFavoriteArticleQuery=queryFactory @@ -119,4 +120,19 @@ public List getFavoriteArticle(User user){ .stream().map(FavoriteArticleDto::toDto) .collect(Collectors.toList()); } + + public List getUserArticle(User user) { + + var getUserArticleQuery = queryFactory.selectFrom(qArticle) + .join(qArticle.user, qUser) + .fetchJoin() + .where(qUser.eq(user) + .and(qArticle.isDeleted.eq(false)) + .and(qArticle.isRecruiting.eq(true))) + .orderBy(qArticle.createDate.desc()); + + return getUserArticleQuery.fetch() + .stream().map(ArticleInfoDto::toDto) + .collect(Collectors.toList()); + } } diff --git a/src/main/java/com/fab/banggabgo/service/ArticleService.java b/src/main/java/com/fab/banggabgo/service/ArticleService.java index 8d7d51f..1e95836 100644 --- a/src/main/java/com/fab/banggabgo/service/ArticleService.java +++ b/src/main/java/com/fab/banggabgo/service/ArticleService.java @@ -2,6 +2,7 @@ import com.fab.banggabgo.dto.apply.ApplyUserResultDto; import com.fab.banggabgo.dto.article.ArticleEditDto; +import com.fab.banggabgo.dto.article.ArticleInfoDto; import com.fab.banggabgo.dto.article.ArticlePageDto; import com.fab.banggabgo.dto.article.ArticleRegisterDto; import com.fab.banggabgo.entity.User; @@ -19,6 +20,11 @@ public interface ArticleService { */ ArticlePageDto getArticle(Integer id); + /** + * 유저가 작성한 게시글 가져오기 + */ + List getUserArticles(Integer userId); + /** * 게시글 수정 */ diff --git a/src/main/java/com/fab/banggabgo/service/impl/ArticleServiceImpl.java b/src/main/java/com/fab/banggabgo/service/impl/ArticleServiceImpl.java index cec08ff..2f731c2 100644 --- a/src/main/java/com/fab/banggabgo/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/fab/banggabgo/service/impl/ArticleServiceImpl.java @@ -4,6 +4,7 @@ import com.fab.banggabgo.common.exception.ErrorCode; import com.fab.banggabgo.dto.apply.ApplyUserResultDto; import com.fab.banggabgo.dto.article.ArticleEditDto; +import com.fab.banggabgo.dto.article.ArticleInfoDto; import com.fab.banggabgo.dto.article.ArticlePageDto; import com.fab.banggabgo.dto.article.ArticleRegisterDto; import com.fab.banggabgo.entity.Apply; @@ -13,6 +14,7 @@ import com.fab.banggabgo.repository.ApplyRepository; import com.fab.banggabgo.repository.ArticleRepository; import com.fab.banggabgo.repository.LikeArticleRepository; +import com.fab.banggabgo.repository.UserRepository; import com.fab.banggabgo.service.ArticleService; import com.fab.banggabgo.type.ApproveStatus; import com.fab.banggabgo.type.Gender; @@ -35,6 +37,7 @@ public class ArticleServiceImpl implements ArticleService { private final ArticleRepository articleRepository; private final LikeArticleRepository likeArticleRepository; private final ApplyRepository applyRepository; + private final UserRepository userRepository; private static final String ADD_LIKE_ARTICLE_SUCCESS = "찜 등록 완료"; private static final String DELETE_LIKE_ARTICLE_SUCCESS = "찜 삭제 완료"; @@ -98,6 +101,14 @@ public ArticlePageDto getArticle(Integer id) { return ArticlePageDto.toDto(article); } + @Override + public List getUserArticles(Integer userId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_IS_NULL)); + + return articleRepository.getUserArticle(user); + } + @Override public void putArticle(User user, Integer id, ArticleEditDto dto) { diff --git a/src/test/java/com/fab/banggabgo/controller/ArticleControllerTest.java b/src/test/java/com/fab/banggabgo/controller/ArticleControllerTest.java index b1aa32a..335a446 100644 --- a/src/test/java/com/fab/banggabgo/controller/ArticleControllerTest.java +++ b/src/test/java/com/fab/banggabgo/controller/ArticleControllerTest.java @@ -225,6 +225,41 @@ void getArticleFail_ARTICLE_NOT_EXISTS() throws Exception { assertThat(errorCode).isEqualTo("ARTICLE_NOT_EXISTS"); } + @Test + @DisplayName("유저가 작성한 글 목록 가져오기 성공") + @WithMockUser + void getUserArticlesSuccess() throws Exception { + //given + //when + //then + mockMvc.perform(get("/api/articles/users/1") + .with(SecurityMockMvcRequestPostProcessors.csrf())) + .andExpect(status().isOk()) + .andDo(print()); + } + + @Test + @DisplayName("유저가 작성한 글 목록 가져오기 실패 : 유저가 존재하지 않음") + @WithMockUser + void getUserArticlesFail_USER_IS_NULL() throws Exception { + //given + doThrow(new CustomException(ErrorCode.USER_IS_NULL)) + .when(articleService) + .getUserArticles(anyInt()); + + //when + MvcResult result = mockMvc.perform(get("/api/articles/users/1") + .with(SecurityMockMvcRequestPostProcessors.csrf())) + .andExpect(status().isBadRequest()) + .andReturn(); + + //then + String responseBody = result.getResponse().getContentAsString(); + JsonNode responseJson = objectMapper.readTree(responseBody); + String errorCode = responseJson.get("code").asText(); + assertThat(errorCode).isEqualTo("USER_IS_NULL"); + } + @Test @DisplayName("글 수정 성공") @WithMockUser diff --git a/src/test/java/com/fab/banggabgo/service/impl/ArticleServiceImplTest.java b/src/test/java/com/fab/banggabgo/service/impl/ArticleServiceImplTest.java index 91dba2c..06d8eb0 100644 --- a/src/test/java/com/fab/banggabgo/service/impl/ArticleServiceImplTest.java +++ b/src/test/java/com/fab/banggabgo/service/impl/ArticleServiceImplTest.java @@ -14,6 +14,7 @@ import com.fab.banggabgo.common.exception.CustomException; import com.fab.banggabgo.common.exception.ErrorCode; import com.fab.banggabgo.dto.article.ArticleEditDto; +import com.fab.banggabgo.dto.article.ArticleInfoDto; import com.fab.banggabgo.dto.article.ArticlePageDto; import com.fab.banggabgo.dto.article.ArticleRegisterDto; import com.fab.banggabgo.entity.Article; @@ -22,6 +23,7 @@ import com.fab.banggabgo.repository.ApplyRepository; import com.fab.banggabgo.repository.ArticleRepository; import com.fab.banggabgo.repository.LikeArticleRepository; +import com.fab.banggabgo.repository.UserRepository; import com.fab.banggabgo.type.ActivityTime; import com.fab.banggabgo.type.ApproveStatus; import com.fab.banggabgo.type.Gender; @@ -56,6 +58,10 @@ class ArticleServiceImplTest { @Mock private ApplyRepository applyRepository; + + @Mock + private UserRepository userRepository; + @InjectMocks private ArticleServiceImpl articleService; @@ -199,6 +205,46 @@ void getArticleFail_ARTICLE_NOT_EXISTS() { assertEquals(exception.getMessage(), "존재하지 않는 게시글 입니다."); } + @Test + @DisplayName("유저가 작성한 글 목록 가져오기 성공") + void getUserArticlesSuccess() { + //given + List articleList = new ArrayList<>(); + + for (int i = 0; i < 5; i++) { + ArticleInfoDto articleInfoDto = ArticleInfoDto.builder() + .id(i + 1) + .title("글 제목" + i) + .build(); + + articleList.add(articleInfoDto); + } + + given(articleRepository.getUserArticle(any())) + .willReturn(articleList); + + given(userRepository.findById(anyInt())) + .willReturn(Optional.ofNullable(User.builder().build())); + + //when + List result = articleService.getUserArticles(1); + + //then + assertEquals(5, result.size()); + } + + @Test + @DisplayName("유저가 작성한 글 목록 가져오기 실패 : 유저가 존재하지 않음") + void getUserArticlesFail_USER_IS_NULL() { + //given + //when + CustomException exception = assertThrows(CustomException.class, + () -> articleService.getUserArticles(1)); + + //then + assertEquals(exception.getMessage(), "유저 정보를 불러오는데 실패했습니다."); + } + @Test @DisplayName("글 수정 성공") void putArticleSuccess() {