diff --git a/src/main/java/com/kim3ho1/yourprotein/config/SecurityConfig.java b/src/main/java/com/kim3ho1/yourprotein/config/SecurityConfig.java index 9bf055b..a6bc57c 100644 --- a/src/main/java/com/kim3ho1/yourprotein/config/SecurityConfig.java +++ b/src/main/java/com/kim3ho1/yourprotein/config/SecurityConfig.java @@ -31,7 +31,7 @@ public class SecurityConfig { private final String[] swaggerUrls = {"/swagger-ui/**", "/v3/**"}; - private final String[] authUrls = {"/", "/login/kakao"}; + private final String[] authUrls = {"/", "/login/kakao", "/recipe/**"}; private final String[] allowedUrls = Stream.concat(Arrays.stream(swaggerUrls), Arrays.stream(authUrls)) .toArray(String[]::new); @@ -106,4 +106,4 @@ CorsConfigurationSource corsConfigurationSource() { return config; }; } -} \ No newline at end of file +} diff --git a/src/main/java/com/kim3ho1/yourprotein/controller/RecipeController.java b/src/main/java/com/kim3ho1/yourprotein/controller/RecipeController.java index 3ad25c0..abe6237 100644 --- a/src/main/java/com/kim3ho1/yourprotein/controller/RecipeController.java +++ b/src/main/java/com/kim3ho1/yourprotein/controller/RecipeController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController; import com.kim3ho1.yourprotein.domain.Recipe; +import com.kim3ho1.yourprotein.dto.RecipeResponseDto; import com.kim3ho1.yourprotein.service.RecipeService; import lombok.RequiredArgsConstructor; @@ -31,13 +32,13 @@ public String crawlingRecipe() { // 단백질량 기반 레시피 추천 @GetMapping("/recommend") - public ResponseEntity> searchRecommendedRecipe(@Param("protein") String protein) { + public ResponseEntity> searchRecommendedRecipe(@Param("protein") double protein) { return ResponseEntity.ok(recipeService.getRecommendedRecipe(protein)); } // 레시피 검색 @GetMapping("/search") - public ResponseEntity> searchRecipes(@Param("keyword") String keyword) { + public ResponseEntity> searchRecipes(@Param("keyword") String keyword) { return ResponseEntity.ok(recipeService.searchRecipes(keyword)); } diff --git a/src/main/java/com/kim3ho1/yourprotein/domain/Recipe.java b/src/main/java/com/kim3ho1/yourprotein/domain/Recipe.java index 2131d97..4286b2c 100644 --- a/src/main/java/com/kim3ho1/yourprotein/domain/Recipe.java +++ b/src/main/java/com/kim3ho1/yourprotein/domain/Recipe.java @@ -30,7 +30,7 @@ public class Recipe { private String details; // 재료 private String kcal; private String carbo; - private String protein; + private double protein; private String fat; private String na; private String kind; // 요리 종류 diff --git a/src/main/java/com/kim3ho1/yourprotein/dto/RecipeResponseDto.java b/src/main/java/com/kim3ho1/yourprotein/dto/RecipeResponseDto.java new file mode 100644 index 0000000..f4c0532 --- /dev/null +++ b/src/main/java/com/kim3ho1/yourprotein/dto/RecipeResponseDto.java @@ -0,0 +1,49 @@ +package com.kim3ho1.yourprotein.dto; + +import java.util.List; + +import com.kim3ho1.yourprotein.domain.Recipe; + +import jakarta.persistence.Column; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +public class RecipeResponseDto { + @Data + @AllArgsConstructor + @Builder + public static class RecipeDetailResponseDto { + private Long id; + + private String recipeName; + private String details; // 재료 + private String kcal; + private String carbo; + private double protein; + private String fat; + private String na; + private String kind; // 요리 종류 + private List images; + + private List manual; + + } + public static RecipeDetailResponseDto from(Recipe recipe) { + return RecipeResponseDto.RecipeDetailResponseDto.builder() + .id(recipe.getId()) + .recipeName(recipe.getRecipeName()) + .details(recipe.getDetails()) + .kcal(recipe.getKcal()) + .carbo(recipe.getCarbo()) + .protein(recipe.getProtein()) + .fat(recipe.getFat()) + .na(recipe.getNa()) + .kind(recipe.getKind()) + .images(List.of(recipe.getImages().split("\n"))) + .manual(List.of(recipe.getManual().split("\n"))).build(); + } +} diff --git a/src/main/java/com/kim3ho1/yourprotein/repository/RecipeRepository.java b/src/main/java/com/kim3ho1/yourprotein/repository/RecipeRepository.java index aa3c02a..39dc7ba 100644 --- a/src/main/java/com/kim3ho1/yourprotein/repository/RecipeRepository.java +++ b/src/main/java/com/kim3ho1/yourprotein/repository/RecipeRepository.java @@ -11,7 +11,8 @@ import com.kim3ho1.yourprotein.domain.Recipe; @Repository public interface RecipeRepository extends JpaRepository { - List searchAllByProtein(String protein); + @Query(value = "select * from recipe where protein <= :protein order by protein desc limit 5 ; ", nativeQuery = true) + List searchAllByProtein(@Param("protein") double protein); @Query(value = "select * from recipe where recipe_name like %:keyword% ; ", nativeQuery = true) List searchAllByRecipeName(@Param("keyword") String keyword); diff --git a/src/main/java/com/kim3ho1/yourprotein/service/RecipeService.java b/src/main/java/com/kim3ho1/yourprotein/service/RecipeService.java index e1d01e7..55b8d15 100644 --- a/src/main/java/com/kim3ho1/yourprotein/service/RecipeService.java +++ b/src/main/java/com/kim3ho1/yourprotein/service/RecipeService.java @@ -5,7 +5,9 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -13,8 +15,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.kim3ho1.yourprotein.domain.Food; import com.kim3ho1.yourprotein.domain.Recipe; +import com.kim3ho1.yourprotein.dto.RecipeResponseDto; import com.kim3ho1.yourprotein.repository.RecipeRepository; import lombok.RequiredArgsConstructor; @@ -81,7 +83,7 @@ public void crawlingRecipe() { .details(recipe.get("RCP_PARTS_DTLS").asText()) .kcal(recipe.get("INFO_ENG").asText()) .carbo(recipe.get("INFO_CAR").asText()) - .protein(recipe.get("INFO_PRO").asText()) + .protein(recipe.get("INFO_PRO").asDouble()) .fat(recipe.get("INFO_FAT").asText()) .na(recipe.get("INFO_NA").asText()) .kind(recipe.get("RCP_PAT2").asText()) @@ -101,13 +103,28 @@ public void crawlingRecipe() { } } - public List getRecommendedRecipe(String protein) { - return recipeRepository.searchAllByProtein(protein); + public List getRecommendedRecipe(double protein) { + List recipes = recipeRepository.searchAllByProtein(protein); + List lists = new ArrayList<>(); + recipes.stream().map( + recipe -> { + return lists.add(RecipeResponseDto.from(recipe)); + } + + ).collect(Collectors.toList()); + return lists; } - public List searchRecipes(String keyword) { + public List searchRecipes(String keyword) { List recipes = recipeRepository.searchAllByRecipeName(keyword); - return recipes; + List lists = new ArrayList<>(); + recipes.stream().map( + recipe -> { + return lists.add(RecipeResponseDto.from(recipe)); + } + + ).collect(Collectors.toList()); + return lists; } public Recipe getRecipe(Long recipeId) {