diff --git a/src/main/java/com/fab/banggabgo/controller/ArticleController.java b/src/main/java/com/fab/banggabgo/controller/ArticleController.java index 4f797fd..353bcad 100644 --- a/src/main/java/com/fab/banggabgo/controller/ArticleController.java +++ b/src/main/java/com/fab/banggabgo/controller/ArticleController.java @@ -80,12 +80,6 @@ public ResponseEntity getArticleByPageable( return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity(); } - @GetMapping("/total") - public ResponseEntity getArticleTotalCnt() { - var result = articleService.getArticleTotalCnt(); - return ApiResponse.builder().code(ResponseCode.RESPONSE_SUCCESS).data(result).toEntity(); - } - @GetMapping("/filter") public ResponseEntity getArticleByFilter( @RequestParam("page") Integer page, diff --git a/src/main/java/com/fab/banggabgo/dto/article/ArticlePageResultDto.java b/src/main/java/com/fab/banggabgo/dto/article/ArticlePageResultDto.java new file mode 100644 index 0000000..90f15b8 --- /dev/null +++ b/src/main/java/com/fab/banggabgo/dto/article/ArticlePageResultDto.java @@ -0,0 +1,28 @@ +package com.fab.banggabgo.dto.article; + +import com.fab.banggabgo.entity.Article; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.springframework.data.domain.Page; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ArticlePageResultDto { + + private long totalCnt; + private List articleList; + + public static ArticlePageResultDto toDto(Page
articlePage) { + return ArticlePageResultDto.builder() + .totalCnt(articlePage.getTotalElements()) + .articleList(ArticlePageDto.toDtoList(articlePage)) + .build(); + } +} 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 5b2859a..6902cab 100644 --- a/src/main/java/com/fab/banggabgo/repository/impl/ArticleRepositoryImpl.java +++ b/src/main/java/com/fab/banggabgo/repository/impl/ArticleRepositoryImpl.java @@ -45,9 +45,20 @@ public Page
getArticle(Pageable pageable, boolean isRecruiting) { .limit(pageable.getPageSize()) .distinct(); + var articleCountQuery = queryFactory.select(qArticle.count()) + .from(qArticle) + .join(qArticle.user, qUser) + .where(qArticle.isDeleted.eq(false)) + .distinct(); + + if (isRecruiting) { + articleQuery = articleQuery.where(qArticle.isRecruiting.eq(true)); + articleCountQuery = articleCountQuery.where(qArticle.isRecruiting.eq(true)); + } + List
articleList = articleQuery.fetch(); - return new PageImpl<>(articleList); + return new PageImpl<>(articleList, pageable, articleCountQuery.fetchOne()); } public Page
getArticleByFilter(Pageable pageable, boolean isRecruiting, String region, @@ -70,10 +81,15 @@ public Page
getArticleByFilter(Pageable pageable, boolean isRecruiting, .distinct(); + var articleCountQuery = queryFactory.select(qArticle.count()) + .from(qArticle) + .join(qArticle.user, qUser) + .where(qArticle.isDeleted.eq(false)) + .distinct(); List
articleList = articleQuery.fetch(); - return new PageImpl<>(articleList); + return new PageImpl<>(articleList, pageable, articleCountQuery.fetchOne()); } @Override @@ -89,7 +105,7 @@ public List getMyArticle(User user) { var getMyArticleQuery=queryFactory.selectFrom(qArticle) .join(qArticle.user ,qUser) .fetchJoin() - .where(qUser.eq(user).and(eqDelete(false))) + .where(qUser.eq(user),eqDelete(false)) .orderBy(qArticle.isRecruiting.desc() ,qArticle.createDate.desc()); return getMyArticleQuery.fetch() .stream().map(MyArticleDto::toDto) @@ -103,7 +119,8 @@ public List getFavoriteArticle(User user){ .from(qLikeArticle) .join(qLikeArticle.article,qArticle) .join(qLikeArticle.user,qUser) - .where(qUser.eq(user),eqDelete(false)); + .where(qUser.eq(user), + eqDelete(false)); return getMyFavoriteArticleQuery.fetch() .stream().map(FavoriteArticleDto::toDto) @@ -115,7 +132,9 @@ public List getUserArticle(User user) { var getUserArticleQuery = queryFactory.selectFrom(qArticle) .join(qArticle.user, qUser) .fetchJoin() - .where(qUser.eq(user),eqDelete(false),eqRecruiting(true)) + .where(qUser.eq(user), + eqDelete(false), + eqRecruiting(true)) .orderBy(qArticle.createDate.desc()); return getUserArticleQuery.fetch() diff --git a/src/main/java/com/fab/banggabgo/service/ArticleService.java b/src/main/java/com/fab/banggabgo/service/ArticleService.java index 1e95836..c2e6c48 100644 --- a/src/main/java/com/fab/banggabgo/service/ArticleService.java +++ b/src/main/java/com/fab/banggabgo/service/ArticleService.java @@ -4,6 +4,7 @@ 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.ArticlePageResultDto; import com.fab.banggabgo.dto.article.ArticleRegisterDto; import com.fab.banggabgo.entity.User; import java.util.List; @@ -38,19 +39,14 @@ public interface ArticleService { /** * 게시글 페이징 불러오기 */ - List getArticleByPageable(Integer page, Integer size, boolean isRecruiting); + ArticlePageResultDto getArticleByPageable(Integer page, Integer size, boolean isRecruiting); /** * 게시글 검색하기 */ - List getArticleByFilter(Integer page, Integer size, boolean isRecruiting, + ArticlePageResultDto getArticleByFilter(Integer page, Integer size, boolean isRecruiting, String region, String period, String price, String gender); - /** - * 게시글 전체 개수 - */ - Integer getArticleTotalCnt(); - /** * 게시글 찜 등록/삭제 */ diff --git a/src/main/java/com/fab/banggabgo/service/MyContentService.java b/src/main/java/com/fab/banggabgo/service/MyContentService.java index 4535b35..838f3b7 100644 --- a/src/main/java/com/fab/banggabgo/service/MyContentService.java +++ b/src/main/java/com/fab/banggabgo/service/MyContentService.java @@ -30,7 +30,6 @@ PostMyInfoImageResultDto postMyInfoImage(User user, PostMyInfoImageRequestDto dt throws IOException; PatchMyInfoResultDto patchMyInfo(User user, PatchMyInfoRequestDto form); - List getMyFromApplicantList(User user, Integer page, Integer size); List getMyToApplicantList(User user, Integer page, Integer size); 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 2f731c2..5f1af8a 100644 --- a/src/main/java/com/fab/banggabgo/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/fab/banggabgo/service/impl/ArticleServiceImpl.java @@ -6,6 +6,7 @@ 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.ArticlePageResultDto; import com.fab.banggabgo.dto.article.ArticleRegisterDto; import com.fab.banggabgo.entity.Apply; import com.fab.banggabgo.entity.Article; @@ -180,7 +181,7 @@ public void deleteArticle(User user, Integer id) { } @Override - public List getArticleByPageable(Integer page, Integer size, + public ArticlePageResultDto getArticleByPageable(Integer page, Integer size, boolean isRecruiting) { page = page < 1 ? 1 : page; @@ -189,11 +190,11 @@ public List getArticleByPageable(Integer page, Integer size, Page
articleList = articleRepository.getArticle(pageable, isRecruiting); - return ArticlePageDto.toDtoList(articleList); + return ArticlePageResultDto.toDto(articleList); } @Override - public List getArticleByFilter(Integer page, Integer size, boolean isRecruiting, + public ArticlePageResultDto getArticleByFilter(Integer page, Integer size, boolean isRecruiting, String region, String period, String price, String gender) { page = page < 1 ? 1 : page; @@ -203,13 +204,7 @@ public List getArticleByFilter(Integer page, Integer size, boole Page
articleList = articleRepository.getArticleByFilter(pageable, isRecruiting, region, period, price, gender); - return ArticlePageDto.toDtoList(articleList); - } - - @Override - public Integer getArticleTotalCnt() { - - return articleRepository.getArticleTotalCnt(); + return ArticlePageResultDto.toDto(articleList); } @Override diff --git a/src/test/java/com/fab/banggabgo/controller/ArticleControllerTest.java b/src/test/java/com/fab/banggabgo/controller/ArticleControllerTest.java index 335a446..ce79643 100644 --- a/src/test/java/com/fab/banggabgo/controller/ArticleControllerTest.java +++ b/src/test/java/com/fab/banggabgo/controller/ArticleControllerTest.java @@ -558,19 +558,6 @@ void getArticleByFilterSuccess() throws Exception { .andDo(print()); } - @Test - @DisplayName("게시글 총 개수 가져오기") - @WithMockUser - void getArticleTotalCntSuccess() throws Exception { - //given - //when - //then - mockMvc.perform(get("/api/articles/total") - .with(SecurityMockMvcRequestPostProcessors.csrf())) - .andExpect(status().isOk()) - .andDo(print()); - } - @Test @DisplayName("글 찜 등록 및 삭제 성공") @WithMockUser diff --git a/src/test/java/com/fab/banggabgo/controller/MyContentControllerTest.java b/src/test/java/com/fab/banggabgo/controller/MyContentControllerTest.java index 9772243..62ce855 100644 --- a/src/test/java/com/fab/banggabgo/controller/MyContentControllerTest.java +++ b/src/test/java/com/fab/banggabgo/controller/MyContentControllerTest.java @@ -215,7 +215,7 @@ void FailNonAuthGetApplicantList() throws Exception { .with(SecurityMockMvcRequestPostProcessors.csrf())) .andExpect(status().isUnauthorized()); } - + @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 06d8eb0..3f9e731 100644 --- a/src/test/java/com/fab/banggabgo/service/impl/ArticleServiceImplTest.java +++ b/src/test/java/com/fab/banggabgo/service/impl/ArticleServiceImplTest.java @@ -507,7 +507,7 @@ void getArticleByPageableSuccess_ISRECRUITING_TRUE() { var result = articleService.getArticleByPageable(1, 5, true); //then - assertEquals(5, result.size()); + assertEquals(5, result.getArticleList().size()); } @Test @@ -541,7 +541,7 @@ void getArticleByPageableSuccess_ALL_ARTICLES() { var result = articleService.getArticleByPageable(1, 5, false); //then - assertEquals(5, result.size()); + assertEquals(5, result.getArticleList().size()); } @Test @@ -576,7 +576,7 @@ void getArticleByFilterSuccess_ISRECRUITING_TRUE() { var result = articleService.getArticleByFilter(1, 5, true, "서초구", "1개월 ~ 3개월", "1000000", "남성"); //then - assertEquals(5, result.size()); + assertEquals(5, result.getArticleList().size()); } @Test @@ -612,21 +612,7 @@ void getArticleByFilterSuccess_ALL_ARTICLES() { "남성"); //then - assertEquals(5, result.size()); - } - - @Test - @DisplayName("글 전체 개수 가져오기 성공") - void getArticleTotalCntSuccess() { - //given - given(articleRepository.getArticleTotalCnt()) - .willReturn(5); - - //when - Integer result = articleService.getArticleTotalCnt(); - - //then - assertEquals(5, result); + assertEquals(5, result.getArticleList().size()); } @Test