Skip to content

Commit b59bed0

Browse files
committed
First fixed Commit
1 parent 9fa839b commit b59bed0

6 files changed

Lines changed: 78 additions & 38 deletions

File tree

src/main/java/com/webservice/algorithmchef/controller/RecipeImportController.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/com/webservice/algorithmchef/dto/recipe/CookRcpResponse.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,11 @@ public String getName() {
9090
return name;
9191
}
9292

93-
public String getParts() {
94-
return parts;
95-
}
96-
97-
public String getDescription() {
98-
return parts;
93+
public String[] getParts() {
94+
if (parts == null || parts.isBlank()) {
95+
return new String[0]; // 빈 배열 반환
96+
}
97+
return parts.split("[,\\n]+");
9998
}
10099

101100
public String getImageUrl() {

src/main/java/com/webservice/algorithmchef/model/Recipe.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ public class Recipe {
4040
@Column(nullable = false, columnDefinition = "LONGTEXT")
4141
private String instructions;
4242

43-
@Lob
44-
@Column(columnDefinition = "TEXT")
45-
private String neededIngredients;
46-
4743
private String imageUrl;
4844

4945
@Column(nullable = false)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.webservice.algorithmchef.repository;
2+
3+
import com.webservice.algorithmchef.model.RecipeIngredient;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface RecipeIngredientRepository extends JpaRepository<RecipeIngredient, Long> {
7+
}

src/main/java/com/webservice/algorithmchef/service/RecipeDataInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.springframework.boot.CommandLineRunner;
44
import org.springframework.stereotype.Component;
55

6-
@Component("recipeDataInitializer")
6+
@Component
77
public class RecipeDataInitializer implements CommandLineRunner {
88

99
private final RecipeImportService importService;

src/main/java/com/webservice/algorithmchef/service/RecipeImportService.java

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,36 @@
33
import com.webservice.algorithmchef.client.FoodSafetyApiClient;
44
import com.webservice.algorithmchef.dto.recipe.CookRcpResponse;
55
import com.webservice.algorithmchef.model.Recipe;
6+
import com.webservice.algorithmchef.model.RecipeIngredient;
7+
import com.webservice.algorithmchef.model.Ingredient;
68
import com.webservice.algorithmchef.repository.RecipeRepository;
9+
import com.webservice.algorithmchef.repository.RecipeIngredientRepository;
10+
import com.webservice.algorithmchef.repository.IngredientRepository;
11+
import org.springframework.data.domain.Page;
12+
import org.springframework.data.domain.PageRequest;
13+
import org.springframework.data.domain.Pageable;
714
import org.springframework.stereotype.Service;
815
import org.springframework.transaction.annotation.Transactional;
916

1017
import java.util.List;
18+
import java.util.ArrayList;
1119

1220
@Service
1321
public class RecipeImportService {
1422

1523
private final FoodSafetyApiClient apiClient;
1624
private final RecipeRepository recipeRepository;
25+
private final RecipeIngredientRepository recipeIngredientRepository;
26+
private final IngredientRepository ingredientRepository;
1727

1828
public RecipeImportService(FoodSafetyApiClient apiClient,
19-
RecipeRepository recipeRepository) {
29+
RecipeRepository recipeRepository,
30+
RecipeIngredientRepository recipeIngredientRepository,
31+
IngredientRepository ingredientRepository) {
2032
this.apiClient = apiClient;
2133
this.recipeRepository = recipeRepository;
34+
this.recipeIngredientRepository = recipeIngredientRepository;
35+
this.ingredientRepository = ingredientRepository;
2236
}
2337

2438
@Transactional
@@ -61,16 +75,15 @@ public void importAllFromFoodSafety() {
6175
continue;
6276
}
6377

64-
// 이미 같은 이름이 있으면 스킵
6578
if (recipeRepository.existsByName(name)) {
6679
System.out.println(" -> exist recipe, skip");
6780
continue;
6881
}
6982

83+
7084
Recipe r = new Recipe();
7185
r.setName(name);
7286
r.setDescription(null);
73-
r.setNeededIngredients(item.getParts());
7487
r.setInstructions(item.getInstructions());
7588
r.setImageUrl(item.getImageUrl());
7689
r.setKcal(item.getKcal());
@@ -80,15 +93,63 @@ public void importAllFromFoodSafety() {
8093
}
8194
r.setType(type);
8295

96+
8397
recipeRepository.save(r);
98+
99+
100+
List<RecipeIngredient> recipeIngredients = new ArrayList<>();
101+
for (String ingredientWithUnit : item.getParts()) {
102+
String ingredientName = extractIngredientName(ingredientWithUnit);
103+
if (ingredientName == null || ingredientName.isBlank()) {
104+
continue;
105+
}
106+
107+
108+
Ingredient ingredient = findMatchingIngredient(ingredientName.trim());
109+
if (ingredient == null) {
110+
System.out.println("Ingredient not found for: " + ingredientName);
111+
}
112+
113+
RecipeIngredient recipeIngredient = new RecipeIngredient();
114+
recipeIngredient.setRecipe(r);
115+
recipeIngredient.setNeededIngredients(ingredientWithUnit.trim());
116+
recipeIngredient.setIngredient(ingredient);
117+
118+
recipeIngredients.add(recipeIngredient);
119+
}
120+
121+
// Save RecipeIngredients to DB
122+
recipeIngredientRepository.saveAll(recipeIngredients);
84123
totalSaved++;
85124
System.out.println(" -> DB save complete (current new save: " + totalSaved + ")");
86125
}
87126

88127
start += BATCH_SIZE;
89128
}
90129

91-
System.out.println("[IMPORT] every import complete. A total" + totalSaved + " new saved");
130+
System.out.println("[IMPORT] every import complete. A total " + totalSaved + " new saved");
131+
}
132+
133+
private String extractIngredientName(String ingredientWithUnit) {
134+
String ingredientName = ingredientWithUnit.replaceAll("\\d+\\s*[a-zA-Z가-힣]+", "").trim();
135+
return ingredientName.isEmpty() ? null : ingredientName;
136+
}
137+
138+
private Ingredient findMatchingIngredient(String ingredientName) {
139+
ingredientName = ingredientName.trim();
140+
141+
Ingredient ingredient = ingredientRepository.findByName(ingredientName).orElse(null);
142+
if (ingredient == null) {
143+
Pageable pageable = PageRequest.of(0, 10);
144+
145+
Page<Ingredient> ingredientsPage = ingredientRepository.findByNameContaining(ingredientName, pageable);
146+
147+
if (!ingredientsPage.isEmpty()) {
148+
ingredient = ingredientsPage.getContent().get(0);
149+
}
150+
}
151+
152+
return ingredient;
92153
}
93154

94155
}

0 commit comments

Comments
 (0)