Skip to content

Commit

Permalink
feat: 태그 검색
Browse files Browse the repository at this point in the history
  • Loading branch information
alsrudursla committed Nov 14, 2024
1 parent 2a79816 commit a11e814
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/main/java/BabAl/BabalServer/controller/RecipeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,6 +36,15 @@ public ApiResponse<RecipeIngredientsResponseDto> 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<RecipeTagsResponseDto> getTags(@RequestParam String alpha) throws JsonProcessingException {
return ApiResponse.onSuccess(recipeService.getTags(alpha));
}

@PostMapping("/recommendation") // 재료를 입력하면 사용자 프로필과 재료로 레시피 추천
@Operation(summary = "레시피 추천", description = "레시피 추천받을 때 사용하는 API")
@ApiResponses(value = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> tags;
}
6 changes: 5 additions & 1 deletion src/main/java/BabAl/BabalServer/service/RecipeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecipeRecommendationsResponseDto> getRecommendations(String userEmail, RecipeRecommendationsDto ingredients) throws JsonProcessingException;
}
43 changes: 42 additions & 1 deletion src/main/java/BabAl/BabalServer/service/RecipeServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<String> 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<String> 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<String> 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<RecipeRecommendationsResponseDto> getRecommendations(String userEmail, RecipeRecommendationsDto ingredients) throws JsonProcessingException {
Expand Down

0 comments on commit a11e814

Please sign in to comment.