Skip to content

Commit

Permalink
Merge pull request #79 from Team-FAB/025-Article-page-fix
Browse files Browse the repository at this point in the history
fix : 게시글 페이지 불러오기 수정
  • Loading branch information
yjjjwww authored Jun 19, 2023
2 parents d259edd + 7e9b269 commit 2084144
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ArticlePageDto> articleList;

public static ArticlePageResultDto toDto(Page<Article> articlePage) {
return ArticlePageResultDto.builder()
.totalCnt(articlePage.getTotalElements())
.articleList(ArticlePageDto.toDtoList(articlePage))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ public Page<Article> 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<Article> articleList = articleQuery.fetch();

return new PageImpl<>(articleList);
return new PageImpl<>(articleList, pageable, articleCountQuery.fetchOne());
}

public Page<Article> getArticleByFilter(Pageable pageable, boolean isRecruiting, String region,
Expand All @@ -62,29 +69,40 @@ public Page<Article> getArticleByFilter(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));
}

if (!"상관 없음".equals(region)) {
articleQuery = articleQuery.where(qArticle.region.eq(Seoul.fromValue(region)));
articleCountQuery = articleCountQuery.where(qArticle.region.eq(Seoul.fromValue(region)));
}

if (!"상관 없음".equals(period)) {
articleQuery = articleQuery.where(qArticle.period.eq(Period.fromValue(period)));
articleCountQuery = articleCountQuery.where(qArticle.period.eq(Period.fromValue(period)));
}

if (!"상관 없음".equals(price)) {
articleQuery = articleQuery.where(qArticle.price.loe(Integer.parseInt(price)));
articleCountQuery = articleCountQuery.where(qArticle.price.loe(Integer.parseInt(price)));
}

if (!"상관 없음".equals(gender)) {
articleQuery = articleQuery.where(qArticle.gender.eq(Gender.fromValue(gender)));
articleCountQuery = articleCountQuery.where(qArticle.gender.eq(Gender.fromValue(gender)));
}

List<Article> articleList = articleQuery.fetch();

return new PageImpl<>(articleList);
return new PageImpl<>(articleList, pageable, articleCountQuery.fetchOne());
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/com/fab/banggabgo/service/ArticleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,19 +39,14 @@ public interface ArticleService {
/**
* 게시글 페이징 불러오기
*/
List<ArticlePageDto> getArticleByPageable(Integer page, Integer size, boolean isRecruiting);
ArticlePageResultDto getArticleByPageable(Integer page, Integer size, boolean isRecruiting);

/**
* 게시글 검색하기
*/
List<ArticlePageDto> 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();

/**
* 게시글 찜 등록/삭제
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,7 +181,7 @@ public void deleteArticle(User user, Integer id) {
}

@Override
public List<ArticlePageDto> getArticleByPageable(Integer page, Integer size,
public ArticlePageResultDto getArticleByPageable(Integer page, Integer size,
boolean isRecruiting) {

page = page < 1 ? 1 : page;
Expand All @@ -189,11 +190,11 @@ public List<ArticlePageDto> getArticleByPageable(Integer page, Integer size,

Page<Article> articleList = articleRepository.getArticle(pageable, isRecruiting);

return ArticlePageDto.toDtoList(articleList);
return ArticlePageResultDto.toDto(articleList);
}

@Override
public List<ArticlePageDto> 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;
Expand All @@ -203,13 +204,7 @@ public List<ArticlePageDto> getArticleByFilter(Integer page, Integer size, boole
Page<Article> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2084144

Please sign in to comment.