Skip to content

Commit

Permalink
Merge branch 'develop' into refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
FeelingXD authored Jun 19, 2023
2 parents 764d038 + 93ad755 commit f6079d7
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 61 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 @@ -45,9 +45,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 @@ -70,10 +81,15 @@ public Page<Article> 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<Article> articleList = articleQuery.fetch();

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

@Override
Expand All @@ -89,7 +105,7 @@ public List<MyArticleDto> 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)
Expand All @@ -103,7 +119,8 @@ public List<FavoriteArticleDto> 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)
Expand All @@ -115,7 +132,9 @@ public List<ArticleInfoDto> 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()
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 @@ -30,7 +30,6 @@ PostMyInfoImageResultDto postMyInfoImage(User user, PostMyInfoImageRequestDto dt
throws IOException;
PatchMyInfoResultDto patchMyInfo(User user, PatchMyInfoRequestDto form);


List<ApplyListResultDto> getMyFromApplicantList(User user, Integer page, Integer size);

List<ApplyListResultDto> getMyToApplicantList(User user, Integer page, Integer size);
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 @@ -215,7 +215,7 @@ void FailNonAuthGetApplicantList() throws Exception {
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isUnauthorized());
}

@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 f6079d7

Please sign in to comment.