33import com .webservice .algorithmchef .client .FoodSafetyApiClient ;
44import com .webservice .algorithmchef .dto .recipe .CookRcpResponse ;
55import com .webservice .algorithmchef .model .Recipe ;
6+ import com .webservice .algorithmchef .model .RecipeIngredient ;
7+ import com .webservice .algorithmchef .model .Ingredient ;
68import 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 ;
714import org .springframework .stereotype .Service ;
815import org .springframework .transaction .annotation .Transactional ;
916
1017import java .util .List ;
18+ import java .util .ArrayList ;
1119
1220@ Service
1321public 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