From a11e8146898ec9ff3892937a9cae030a1acb7851 Mon Sep 17 00:00:00 2001 From: alsrudursla Date: Thu, 14 Nov 2024 19:37:45 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=ED=83=9C=EA=B7=B8=20=EA=B2=80=EC=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/RecipeController.java | 10 +++++ .../dto/response/RecipeTagsResponseDto.java | 15 +++++++ .../BabalServer/service/RecipeService.java | 6 ++- .../service/RecipeServiceImpl.java | 43 ++++++++++++++++++- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/main/java/BabAl/BabalServer/dto/response/RecipeTagsResponseDto.java diff --git a/src/main/java/BabAl/BabalServer/controller/RecipeController.java b/src/main/java/BabAl/BabalServer/controller/RecipeController.java index 00219f7..03686b3 100644 --- a/src/main/java/BabAl/BabalServer/controller/RecipeController.java +++ b/src/main/java/BabAl/BabalServer/controller/RecipeController.java @@ -4,6 +4,7 @@ import BabAl.BabalServer.dto.request.RecipeRecommendationsDto; import BabAl.BabalServer.dto.response.RecipeIngredientsResponseDto; import BabAl.BabalServer.dto.response.RecipeRecommendationsResponseDto; +import BabAl.BabalServer.dto.response.RecipeTagsResponseDto; import BabAl.BabalServer.jwt.JwtUtil; import BabAl.BabalServer.service.RecipeService; import com.fasterxml.jackson.core.JsonProcessingException; @@ -35,6 +36,15 @@ public ApiResponse getIngredients(@RequestParam St return ApiResponse.onSuccess(recipeService.getIngredients(alpha)); } + @GetMapping("/tags") // 레시피 태그 검색 + @Operation(summary = "레시피 태그 검색", description = "레시피 태그 검색할 때 사용하는 API") + @ApiResponses(value = { + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "요청이 정상 처리되었습니다", content = @Content(mediaType = "application/json")), + }) + public ApiResponse getTags(@RequestParam String alpha) throws JsonProcessingException { + return ApiResponse.onSuccess(recipeService.getTags(alpha)); + } + @PostMapping("/recommendation") // 재료를 입력하면 사용자 프로필과 재료로 레시피 추천 @Operation(summary = "레시피 추천", description = "레시피 추천받을 때 사용하는 API") @ApiResponses(value = { diff --git a/src/main/java/BabAl/BabalServer/dto/response/RecipeTagsResponseDto.java b/src/main/java/BabAl/BabalServer/dto/response/RecipeTagsResponseDto.java new file mode 100644 index 0000000..9e54d8b --- /dev/null +++ b/src/main/java/BabAl/BabalServer/dto/response/RecipeTagsResponseDto.java @@ -0,0 +1,15 @@ +package BabAl.BabalServer.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class RecipeTagsResponseDto { + private int count; + private List tags; +} diff --git a/src/main/java/BabAl/BabalServer/service/RecipeService.java b/src/main/java/BabAl/BabalServer/service/RecipeService.java index 0935104..99276d7 100644 --- a/src/main/java/BabAl/BabalServer/service/RecipeService.java +++ b/src/main/java/BabAl/BabalServer/service/RecipeService.java @@ -3,15 +3,19 @@ import BabAl.BabalServer.dto.request.RecipeRecommendationsDto; import BabAl.BabalServer.dto.response.RecipeIngredientsResponseDto; import BabAl.BabalServer.dto.response.RecipeRecommendationsResponseDto; +import BabAl.BabalServer.dto.response.RecipeTagsResponseDto; import com.fasterxml.jackson.core.JsonProcessingException; import java.util.List; public interface RecipeService { - // 레시피 검색 + // 레시피 재료 검색 RecipeIngredientsResponseDto getIngredients(String alpha) throws JsonProcessingException; + // 레시피 태그 검색 + RecipeTagsResponseDto getTags(String alpha) throws JsonProcessingException; + // 레시피 추천 List getRecommendations(String userEmail, RecipeRecommendationsDto ingredients) throws JsonProcessingException; } diff --git a/src/main/java/BabAl/BabalServer/service/RecipeServiceImpl.java b/src/main/java/BabAl/BabalServer/service/RecipeServiceImpl.java index 47ccafb..43970f6 100644 --- a/src/main/java/BabAl/BabalServer/service/RecipeServiceImpl.java +++ b/src/main/java/BabAl/BabalServer/service/RecipeServiceImpl.java @@ -8,6 +8,7 @@ import BabAl.BabalServer.dto.request.RecipeRecommendationsDto; import BabAl.BabalServer.dto.response.RecipeIngredientsResponseDto; import BabAl.BabalServer.dto.response.RecipeRecommendationsResponseDto; +import BabAl.BabalServer.dto.response.RecipeTagsResponseDto; import BabAl.BabalServer.repository.UserRepository; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; @@ -48,7 +49,7 @@ public class RecipeServiceImpl implements RecipeService { @Value("${s3.recipe}") private String recipeUrl; - // 레시피 검색 + // 레시피 재료 검색 @Override public RecipeIngredientsResponseDto getIngredients(String alpha) { String alphabet = alpha.toLowerCase(); @@ -88,6 +89,46 @@ public RecipeIngredientsResponseDto getIngredients(String alpha) { } } + // 레시피 태그 검색 + @Override + public RecipeTagsResponseDto getTags(String alpha) throws JsonProcessingException { + String alphabet = alpha.toLowerCase(); + + try { + checkFile(recipeUrl, "RAW_recipes.csv"); + + // CSV 파일 읽기 + Set allTags = new HashSet<>(); + try (CSVReader csvReader = new CSVReader(new FileReader("RAW_recipes.csv"))) { + String[] record; + while ((record = csvReader.readNext()) != null) { + String tagList = record[5]; + List tags = Arrays.asList(tagList.replaceAll("[\\[\\]']", "").split(", ")); + + // alpha로 시작하는 재료만 추가 + for (String tag : tags) { + if (tag.toLowerCase().startsWith(alphabet)) { + allTags.add(tag); + } + } + } + } catch (CsvException e) { + throw new RuntimeException(e); + } + + // 필터링된 재료를 정렬 + List filteredTags = new ArrayList<>(allTags); + Collections.sort(filteredTags); + + // DTO에 결과를 담아 반환 + RecipeTagsResponseDto responseDto = new RecipeTagsResponseDto(filteredTags.size(), filteredTags); + return responseDto; + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + // 레시피 추천 @Override public List getRecommendations(String userEmail, RecipeRecommendationsDto ingredients) throws JsonProcessingException {