TIL_WIL

TIL_230228

성-민 2023. 3. 1. 02:27

미니프로젝트 5일차 페이지네이션과 좋아요 기능을 구현했다. 다른 팀원들은 이미지 업로드 기능을 구현하려고 아직 진행 중이나 오류에서 벗어나지 못하고 있다. 팀원 속도를 따라가며 공부 중인데 내일 오후까지 에러를 해결하도록 해야겠다.

 

페이지네이션을 위해 값을 정렬 후 @PathVariable로 값을 받아와 해당 범위를 반환해 주었다.

해당 범위를 초과할 경우 오류가 생겨서 해결하기 위해 List사이즈에 대한 조건문을 걸어서 오류가 생기지 않도록 값을 줄여서 반환해 주었다.

프론트엔드에서도 이중으로 값을 줄여서 보내주도록 만들어 오류가 생기지 않도록 했다.

 

그리고 좋아요를 Integer 값으로 만들어서 repo에서 Desc 정렬해주어 좋아요 순으로 정렬도 가능하도록 구현했다.

 

이 과정에서 GenerationTarget encountered exception accepting command : Error executing DDL 문제가 생겼는데

Like가 예약어여서 문제가 발생했다.

 

Controller

@GetMapping("/recipe/{a}/{b}")
public List<RecipeSearchDto> getRecipes(@PathVariable int a, @PathVariable int b){
    return recipeService.getRecipes(a, b);
}

 

Service

@Transactional(readOnly = true)
public List<RecipeSearchDto> getRecipes(int a, int b) {
    if (a >= b) throw new CustomException(ErrorCode.INVALID_REQUEST);
    List<Recipe> recipeList = recipeRepository.findAllByOrderByRecipeLikeDesc();
    List<RecipeSearchDto> responseDtoList = new ArrayList<>();
    for (Recipe recipe : recipeList) {
        responseDtoList.add(new RecipeSearchDto(recipe, recipeLikeRepository.countByRecipe(recipe)));
    }
    List<RecipeSearchDto> answer = new ArrayList<>();
    for(int i=0; i<=b-a; i++){
        if(responseDtoList.size()>b){
            answer = new ArrayList<>(responseDtoList.subList(a-1, b));
        }else if(responseDtoList.size()>a-1+i){
            answer.add(responseDtoList.get(a - 1 + i));
        }
    }
    return answer;
}