Skip to content

Commit

Permalink
refactor : #128 컨트롤러에서 Enum 타입 변환
Browse files Browse the repository at this point in the history
  • Loading branch information
PHS00 committed Oct 23, 2023
1 parent 7b6db2e commit 796c2fa
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class GetVoteListService {
static int size = 5;

public GetVoteListResponse.MainAndFinishPage getVoteList(
long userId, int page, String sort, String active, String category) {
long userId, int page, Sort sort, Active active, Category category) {
/*
투표 중 active = continue 이고, createdDate가 최신순으로 정렬하여 가져와서 보여준다
사용자의 id를 가져와서 참여한 투표와 참여하지 않은 투표를 다른 데이터 형식으로 반환한다.
Expand All @@ -56,23 +56,23 @@ public GetVoteListResponse.MainAndFinishPage getVoteList(
return new GetVoteListResponse.MainAndFinishPage(votes, isLast);
}

private Slice<VoteEntity> getVoteListByRequest(String active, String category, String sort) {
private Slice<VoteEntity> getVoteListByRequest(Active active, Category category, Sort sort) {
// 메인페이지 요청인지 완료된 페이지 요청인지 검사
if (Active.isContinueRequest(active)) {
return findContinueVotes(category, sort);
}
return findCompleteVotes(category, sort);
}

private Slice<VoteEntity> findCompleteVotes(String category, String sort) {
private Slice<VoteEntity> findCompleteVotes(Category category, Sort sort) {
// 카테고리 요청 확인
if (Category.isTotalRequest(category)) {
return completeOrderBySort(sort);
}
return completeByCategoryOrderBySort(getCategory(category), sort);
return completeByCategoryOrderBySort(category, sort);
}

private Slice<VoteEntity> completeOrderBySort(String sort) {
private Slice<VoteEntity> completeOrderBySort(Sort sort) {
Pageable pageable = PageRequest.of(page, size);
LocalDateTime now = LocalDateTime.now();

Expand All @@ -83,7 +83,7 @@ private Slice<VoteEntity> completeOrderBySort(String sort) {
return voteRepository.findAllFinishVotesOrderByVoteTotalCount(now, pageable);
}

private Slice<VoteEntity> completeByCategoryOrderBySort(Category category, String sort) {
private Slice<VoteEntity> completeByCategoryOrderBySort(Category category, Sort sort) {
Pageable pageable = PageRequest.of(page, size);

LocalDateTime now = LocalDateTime.now();
Expand All @@ -96,15 +96,15 @@ private Slice<VoteEntity> completeByCategoryOrderBySort(Category category, Strin
now, category, pageable);
}

private Slice<VoteEntity> findContinueVotes(String category, String sort) {
private Slice<VoteEntity> findContinueVotes(Category category, Sort sort) {
// 카테고리 요청 확인
if (Category.isTotalRequest(category)) {
return continueOrderBySort(sort);
}
return continueByCategoryOrderBySort(getCategory(category), sort);
return continueByCategoryOrderBySort(category, sort);
}

private Slice<VoteEntity> continueOrderBySort(String sort) {
private Slice<VoteEntity> continueOrderBySort(Sort sort) {
Pageable pageable = PageRequest.of(page, size);
LocalDateTime now = LocalDateTime.now();

Expand All @@ -115,7 +115,7 @@ private Slice<VoteEntity> continueOrderBySort(String sort) {
return voteRepository.findAllContinueVotesOrderByVoteTotalCount(now, pageable);
}

private Slice<VoteEntity> continueByCategoryOrderBySort(Category category, String sort) {
private Slice<VoteEntity> continueByCategoryOrderBySort(Category category, Sort sort) {
Pageable pageable = PageRequest.of(page, size);
LocalDateTime now = LocalDateTime.now();

Expand All @@ -128,10 +128,6 @@ private Slice<VoteEntity> continueByCategoryOrderBySort(Category category, Strin
now, category, pageable);
}

private Category getCategory(String category) {
return Category.findCategory(category);
}

public GetVoteListResponse.MyPage getVoteListInMyPageByParticipate(Long userId) {
// 임의 유저값 가져옴 나중에 유효성 처리 해야함
GetVoteListResponse.MyPage responseBody = new GetVoteListResponse.MyPage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SearchVoteListService {
static int size = 5;

public SearchVoteListResponse searchVoteList(
Long userId, int page, String keyword, String sort, String category) {
Long userId, int page, String keyword, Sort sort, Category category) {
this.page = page;

// 1. vote list 를 가져온다
Expand All @@ -42,15 +42,15 @@ public SearchVoteListResponse searchVoteList(
return new SearchVoteListResponse(votes, isLast);
}

private Slice<VoteEntity> findVotes(String keyword, String category, String sort) {
private Slice<VoteEntity> findVotes(String keyword, Category category, Sort sort) {
// 카테고리 요청 확인
if (Category.isTotalRequest(category)) {
return OrderBySort(keyword, sort);
}
return ByCategoryOrderBySort(keyword, Category.findCategory(category), sort);
return ByCategoryOrderBySort(keyword, category, sort);
}

private Slice<VoteEntity> OrderBySort(String keyword, String sort) {
private Slice<VoteEntity> OrderBySort(String keyword, Sort sort) {
Pageable pageable = PageRequest.of(page, size);

if (Sort.isCurrentRequest(sort)) {
Expand All @@ -59,7 +59,7 @@ private Slice<VoteEntity> OrderBySort(String keyword, String sort) {
return voteRepository.searchVotesOrderByVoteTotalCount(keyword, pageable);
}

private Slice<VoteEntity> ByCategoryOrderBySort(String keyword, Category category, String sort) {
private Slice<VoteEntity> ByCategoryOrderBySort(String keyword, Category category, Sort sort) {
Pageable pageable = PageRequest.of(page, size);

if (Sort.isCurrentRequest(sort)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Sort findSort(String sort) {
.orElseThrow(() -> new SortException("잘못된 정렬 방식입니다."));
}

public static boolean isCurrentRequest(String sort) {
return findSort(sort) == Sort.CURRENT;
public static boolean isCurrentRequest(Sort sort) {
return sort == Sort.CURRENT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Active findActive(String active) {
.orElseThrow(() -> new NullException("해당 active는 존재하지 않습니다."));
}

public static boolean isContinueRequest(String active) {
return findActive(active) == Active.CONTINUE;
public static boolean isContinueRequest(Active active) {
return active == Active.CONTINUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static Category findCategory(String category) {
.orElseThrow(() -> new CategoryException("해당 카테고리는 존재하지 않습니다."));
}

public static boolean isTotalRequest(String category) {
return findCategory(category) == Category.TOTAL;
public static boolean isTotalRequest(Category category) {
return category == Category.TOTAL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.kakao.golajuma.common.support.respnose.ApiResponseGenerator;
import com.kakao.golajuma.common.support.respnose.MessageCode;
import com.kakao.golajuma.vote.domain.service.GetVoteListService;
import com.kakao.golajuma.vote.web.controller.converter.GetVoteListRequestConverter;
import com.kakao.golajuma.vote.web.dto.response.GetVoteListResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -18,6 +19,7 @@
public class GetVoteListController {

private final GetVoteListService getVoteListService;
private final GetVoteListRequestConverter getVoteListRequestConverter;

// 투표 조회 - 메인페이지, 완료된 페이지
@GetMapping("/votes")
Expand All @@ -28,8 +30,14 @@ public class GetVoteListController {
@RequestParam(defaultValue = "current") String sort,
@RequestParam(defaultValue = "continue") String active,
@RequestParam(defaultValue = "total") String category) {

GetVoteListResponse.MainAndFinishPage responseDto =
getVoteListService.getVoteList(userId, page, sort, active, category);
getVoteListService.getVoteList(
userId,
page,
getVoteListRequestConverter.toSort(sort),
getVoteListRequestConverter.toActive(active),
getVoteListRequestConverter.toCategory(category));

return ApiResponseGenerator.success(responseDto, HttpStatus.OK, MessageCode.GET);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.kakao.golajuma.common.support.respnose.ApiResponseGenerator;
import com.kakao.golajuma.common.support.respnose.MessageCode;
import com.kakao.golajuma.vote.domain.service.SearchVoteListService;
import com.kakao.golajuma.vote.web.controller.converter.GetVoteListRequestConverter;
import com.kakao.golajuma.vote.web.dto.response.SearchVoteListResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -18,6 +19,7 @@
public class SearchVoteListController {

private final SearchVoteListService searchVoteListService;
private final GetVoteListRequestConverter getVoteListRequestConverter;

// 투표 검색 -> 투표 리스트 조회
@GetMapping("/votes/search")
Expand All @@ -28,7 +30,12 @@ public ApiResponse<ApiResponseBody.SuccessBody<SearchVoteListResponse>> searchVo
@RequestParam(defaultValue = "current") String sort,
@RequestParam(defaultValue = "total") String category) {
SearchVoteListResponse responseDto =
searchVoteListService.searchVoteList(userId, page, keyword, sort, category);
searchVoteListService.searchVoteList(
userId,
page,
keyword,
getVoteListRequestConverter.toSort(sort),
getVoteListRequestConverter.toCategory(category));

return ApiResponseGenerator.success(responseDto, HttpStatus.OK, MessageCode.GET);
}
Expand Down

0 comments on commit 796c2fa

Please sign in to comment.