Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions refrigerator/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,6 @@ FodyWeavers.xsd

# End of https://www.toptal.com/developers/gitignore/api/macos,windows,git,java,intellij,visualstudio,eclipse,gradle,netbeans
/src/main/resources/application.yml

.idea/
*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import moja.refrigerator.dto.recipe.response.RecipeResponse;
import moja.refrigerator.service.recipe.RecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -58,4 +59,14 @@ public void updateRecipe(
public List<RecipeRecommendResponse> getRecommendedRecipes(@RequestParam Long userPk) {
return recipeService.getRecommendedRecipes(userPk);
}

@GetMapping("/random")
public ResponseEntity<RecipeRecommendResponse> getRandomRecipe() {
try {
RecipeRecommendResponse randomRecipe = recipeService.getRandomRecipe();
return ResponseEntity.ok(randomRecipe);
} catch (IllegalStateException e) {
return ResponseEntity.notFound().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package moja.refrigerator.dto.recipe.response;

import lombok.Data;

@Data
public class RecipeIngredientInfo {
private String ingredientName;
private boolean isNecessary;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.Data;

import java.util.List;

@Data
public class RecipeRecommendResponse {
private long recipePk;
Expand All @@ -11,4 +13,5 @@ public class RecipeRecommendResponse {
private double matchRate;
private long remainExpirationDays;
private String urgentIngredientName;
private List<RecipeIngredientInfo> ingredients;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ void updateRecipe(
,List<MultipartFile> files
);
List<RecipeRecommendResponse> getRecommendedRecipes(Long userPk);
RecipeRecommendResponse getRandomRecipe();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import moja.refrigerator.dto.recipe.request.RecipeCreateRequest;
import moja.refrigerator.dto.recipe.request.RecipeUpdateRequest;
import moja.refrigerator.dto.recipe.response.RecipeDetailResponse;
import moja.refrigerator.dto.recipe.response.RecipeIngredientInfo;
import moja.refrigerator.dto.recipe.response.RecipeRecommendResponse;
import moja.refrigerator.dto.recipe.response.RecipeResponse;
import moja.refrigerator.repository.ingredient.IngredientMyRefrigeratorRepository;
Expand Down Expand Up @@ -316,4 +317,35 @@ private RecipeMatchResult checkRecipeMatch(Recipe recipe, List<IngredientMyRefri
isMatched ? urgentIngredientName : null
);
}
@Override
public RecipeRecommendResponse getRandomRecipe() {
List<Recipe> allRecipes = recipeRepository.findAll();

if (allRecipes.isEmpty()) {
throw new IllegalStateException("등록된 레시피가 없습니다.");
}

Random random = new Random();
int randomIndex = random.nextInt(allRecipes.size());
Recipe selectedRecipe = allRecipes.get(randomIndex);

// Response 객체로 변환
RecipeRecommendResponse response = mapper.map(selectedRecipe, RecipeRecommendResponse.class);

// 재료 정보 추가
List<RecipeIngredient> recipeIngredients = recipeIngredientRepository.findByRecipe(selectedRecipe);
List<RecipeIngredientInfo> ingredientInfoList = recipeIngredients.stream()
.map(ri -> {
RecipeIngredientInfo info = new RecipeIngredientInfo();
info.setIngredientName(ri.getIngredientManagement().getIngredientName());
info.setNecessary(ri.isIngredientIsNecessary());
return info;
})
.collect(Collectors.toList());

response.setIngredients(ingredientInfoList);

return response;
}

}