Skip to content

Commit

Permalink
Merge pull request #237 from DuruDuru-UMC-7th/refactor/#-234레시피가-존재하지…
Browse files Browse the repository at this point in the history
…-않을-때-null-처리

Refactor: 레시피가 존재하지 않을 때 null 처리
  • Loading branch information
bingseok authored Feb 20, 2025
2 parents 4a96e1e + db9ec78 commit 9053212
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class Member extends BaseEntity {
private String nickName;

@Enumerated(EnumType.STRING)
@ColumnDefault("'U'")
private Gender gender;

private String mobile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@


public interface RecipeCommandService {
RecipeResponseDTO.RecipeDetailResponse getRecipeDetailByName(String recipeName);
public void setRecipeFavorite(Member member, String recipeSeq);
RecipeResponseDTO.RecipeDetailResponse getRecipeDetailByName(Member member, String recipeName);
void setRecipeFavorite(Member member, String recipeSeq);
RecipeResponseDTO.RecipePageResponse searchRecipesByIngredient(String ingredients, int page, int size);
RecipeResponseDTO.RecipePageResponse getFavoriteRecipes(Member member, int page, int size);
RecipeResponseDTO.RecipePageResponse getPopularRecipes(int page, int size);
RecipeResponseDTO.RecipePageResponse getRecipesWithIngredientInfo(Member member, int page, int size);
RecipeResponseDTO.RecipePageResponse searchRecipes(String query, int page, int size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ public class RecipeCommandServiceImpl implements RecipeCommandService {
// 특정 레시피 조회
@Override
@Transactional
public RecipeResponseDTO.RecipeDetailResponse getRecipeDetailByName(String recipeName) {
public RecipeResponseDTO.RecipeDetailResponse getRecipeDetailByName(Member member, String recipeName) {
String url = buildApiUrl(recipeName);
RecipeResponseDTO.RecipeApiResponse apiResponse = restTemplate.getForObject(url, RecipeResponseDTO.RecipeApiResponse.class);

if (apiResponse == null || apiResponse.getRecipes() == null || apiResponse.getRecipes().isEmpty()) {
throw new RecipeException(ErrorStatus.RECIPE_NOT_FOUND);
return RecipeResponseDTO.RecipeDetailResponse.builder()
.recipeName("")
.cookingMethod("")
.recipeType("")
.ingredients("")
.imageUrl("")
.manualSteps(Collections.emptyList())
.favoriteCount(0)
.isFavorite(false) // ✅ 기본값 false
.build();
}

RecipeResponseDTO.RecipeApiResponse.Recipe recipe = apiResponse.getRecipes().get(0);
Expand All @@ -55,6 +64,7 @@ public RecipeResponseDTO.RecipeDetailResponse getRecipeDetailByName(String recip
List<String> cleanedManualSteps = cleanManualSteps(recipe.getManualSteps());

long favoriteCount = memberRecipeRepository.countByRecipeName(recipeName);
boolean isFavorite = memberRecipeRepository.existsByMemberAndRecipeName(member, recipeName);

return RecipeResponseDTO.RecipeDetailResponse.builder()
.recipeName(recipe.getRcpNm())
Expand All @@ -64,6 +74,7 @@ public RecipeResponseDTO.RecipeDetailResponse getRecipeDetailByName(String recip
.imageUrl(recipe.getAttFileNoMk())
.manualSteps(cleanedManualSteps)
.favoriteCount(favoriteCount)
.isFavorite(isFavorite)
.build();
}

Expand All @@ -72,15 +83,15 @@ public RecipeResponseDTO.RecipeDetailResponse getRecipeDetailByName(String recip
@Override
@Transactional
public void setRecipeFavorite(Member member, String recipeName) {
if (memberRecipeRepository.existsByMemberAndRecipeName(member, recipeName)) {
memberRecipeRepository.deleteByMemberAndRecipeName(member, recipeName);
} else {
MemberRecipe memberRecipe = MemberRecipe.builder()
.member(member)
.recipeName(recipeName)
.build();
memberRecipeRepository.save(memberRecipe);
}
if (memberRecipeRepository.existsByMemberAndRecipeName(member, recipeName)) {
memberRecipeRepository.deleteByMemberAndRecipeName(member, recipeName);
} else {
MemberRecipe memberRecipe = MemberRecipe.builder()
.member(member)
.recipeName(recipeName)
.build();
memberRecipeRepository.save(memberRecipe);
}
}

@Override
Expand Down Expand Up @@ -134,7 +145,7 @@ public RecipeResponseDTO.RecipePageResponse searchRecipesByIngredient(String ing
String url = buildApiUrl(ingredients, startIdx, endIdx);
System.out.println("url: " + url);
RecipeResponseDTO.RecipeApiResponse apiResponse = restTemplate.getForObject(url, RecipeResponseDTO.RecipeApiResponse.class);

if (apiResponse == null || apiResponse.getRecipes() == null) {
return RecipeResponseDTO.RecipePageResponse.builder()
.page(page)
Expand Down Expand Up @@ -172,7 +183,6 @@ public RecipeResponseDTO.RecipePageResponse getFavoriteRecipes(Member member, in
List<RecipeResponseDTO.RecipeResponse> recipes = memberRecipesPage.stream()
.map(MemberRecipe::getRecipeName)
.map(this::fetchRecipeDetailFromApi)
.filter(Objects::nonNull) // null 응답 제거
.collect(Collectors.toList());

return RecipeResponseDTO.RecipePageResponse.builder()
Expand Down Expand Up @@ -200,7 +210,6 @@ public RecipeResponseDTO.RecipePageResponse getPopularRecipes(int page, int size
long favoriteCount = (long) record[1];
return fetchRecipeDetailWithFavoriteCount(recipeName, favoriteCount);
})
.filter(Objects::nonNull)
.collect(Collectors.toList());

return RecipeResponseDTO.RecipePageResponse.builder()
Expand Down Expand Up @@ -244,6 +253,16 @@ public RecipeResponseDTO.RecipePageResponse getRecipesWithIngredientInfo(Member
String url = buildApiUrl(joinedIngredients, 1, 50);
RecipeResponseDTO.RecipeApiResponse apiResponse = restTemplate.getForObject(url, RecipeResponseDTO.RecipeApiResponse.class);

if (apiResponse == null || apiResponse.getRecipes() == null || apiResponse.getRecipes().isEmpty()) {
return RecipeResponseDTO.RecipePageResponse.builder()
.page(page)
.size(size)
.totalPages(0)
.totalElements(0)
.recipes(Collections.emptyList()) // 빈 리스트 반환
.build();
}

// 응답된 레시피 데이터 필터링
if (apiResponse != null && apiResponse.getRecipes() != null) {
for (RecipeResponseDTO.RecipeApiResponse.Recipe recipe: apiResponse.getRecipes()) {
Expand Down Expand Up @@ -300,9 +319,12 @@ private RecipeResponseDTO.RecipeResponse fetchRecipeDetailWithFavoriteCount(Stri
String url = buildApiUrl(recipeName);

RecipeResponseDTO.RecipeApiResponse apiResponse = restTemplate.getForObject(url, RecipeResponseDTO.RecipeApiResponse.class);

if (apiResponse == null || apiResponse.getRecipes() == null || apiResponse.getRecipes().isEmpty()) {
return null;
return RecipeResponseDTO.RecipeResponse.builder()
.recipeName("")
.imageUrl("")
.favoriteCount(0)
.build();
}

RecipeResponseDTO.RecipeApiResponse.Recipe recipe = apiResponse.getRecipes().get(0);
Expand All @@ -314,12 +336,16 @@ private RecipeResponseDTO.RecipeResponse fetchRecipeDetailWithFavoriteCount(Stri
.build();
}


private RecipeResponseDTO.RecipeResponse fetchRecipeDetailFromApi(String recipeName) {
String url = buildApiUrl(recipeName);

RecipeResponseDTO.RecipeApiResponse apiResponse = restTemplate.getForObject(url, RecipeResponseDTO.RecipeApiResponse.class);
if (apiResponse == null || apiResponse.getRecipes() == null || apiResponse.getRecipes().isEmpty()) {
return null;
return RecipeResponseDTO.RecipeResponse.builder()
.recipeName("")
.imageUrl("")
.build();
}

RecipeResponseDTO.RecipeApiResponse.Recipe recipe = apiResponse.getRecipes().get(0);
Expand All @@ -330,6 +356,7 @@ private RecipeResponseDTO.RecipeResponse fetchRecipeDetailFromApi(String recipeN
.build();
}


private long getTotalElements(String ingredients) {
int batchSize = 200;
int currentStartIdx = 1;
Expand Down Expand Up @@ -390,4 +417,4 @@ private List<String> cleanManualSteps(List<String> manualSteps) {
.collect(Collectors.toList());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public class RecipeController {
})
@Operation(summary = "특정 레시피 조회 API", description = "특정 레시피를 조회하는 API입니다")
public ApiResponse<RecipeResponseDTO.RecipeDetailResponse> getRecipeByName(
@Parameter(name = "user", hidden = true) @AuthUser Member member,
@PathVariable String recipeName
){
RecipeResponseDTO.RecipeDetailResponse response = recipeService.getRecipeDetailByName(recipeName);
RecipeResponseDTO.RecipeDetailResponse response = recipeService.getRecipeDetailByName(member, recipeName);
return ApiResponse.onSuccess(SuccessStatus.RECIPE_FETCH_OK, response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public static class RecipeResponse {
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class RecipeDetailResponse {
private long favoriteCount; // 즐겨찾기 수
private String recipeName; // 메뉴명
private String cookingMethod; // 조리방법
private String recipeType; // 요리종류
private String ingredients; // 재료정보
private String imageUrl; // 이미지 경로(대)
private List<String> manualSteps; // 만드는 법 단계 목록
private long favoriteCount; // 즐겨찾기 수
private boolean isFavorite;
}

@Getter
Expand Down Expand Up @@ -158,4 +159,4 @@ public List<String> getManualSteps() {
.collect(Collectors.toList()); }
}
}
}
}

0 comments on commit 9053212

Please sign in to comment.