refactor(planner): extract computeCurrentScore helper in PlanningService

Eliminates duplicated currentSlots→score pattern that appeared in both
getSuggestions and getVarietyPreview.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-09 12:11:44 +02:00
committed by marcel
parent 89a549a1c8
commit 9928591b48

View File

@@ -137,11 +137,7 @@ public class PlanningService {
.map(cl -> cl.getRecipe().getId()) .map(cl -> cl.getRecipe().getId())
.collect(Collectors.toSet()); .collect(Collectors.toSet());
List<SimulatedSlot> currentSlots = plan.getSlots().stream() double currentScore = computeCurrentScore(plan, config, recentlyCookedIds);
.map(s -> new SimulatedSlot(s.getRecipe(), s.getSlotDate()))
.toList();
double currentScore = currentSlots.isEmpty() ? MAX_VARIETY_SCORE
: scoreFromSimulatedSlots(currentSlots, config, recentlyCookedIds);
List<Recipe> allRecipes = recipeRepository.findByHouseholdIdAndDeletedAtIsNull(householdId); List<Recipe> allRecipes = recipeRepository.findByHouseholdIdAndDeletedAtIsNull(householdId);
@@ -184,6 +180,14 @@ public class PlanningService {
return scoreFromSimulatedSlots(simulatedSlots, config, recentlyCookedIds); return scoreFromSimulatedSlots(simulatedSlots, config, recentlyCookedIds);
} }
private double computeCurrentScore(WeekPlan plan, VarietyScoreConfig config, Set<UUID> recentlyCookedIds) {
List<SimulatedSlot> currentSlots = plan.getSlots().stream()
.map(s -> new SimulatedSlot(s.getRecipe(), s.getSlotDate()))
.toList();
return currentSlots.isEmpty() ? MAX_VARIETY_SCORE
: scoreFromSimulatedSlots(currentSlots, config, recentlyCookedIds);
}
private record SimulatedSlot(Recipe recipe, LocalDate date) {} private record SimulatedSlot(Recipe recipe, LocalDate date) {}
@Transactional(readOnly = true) @Transactional(readOnly = true)
@@ -201,11 +205,7 @@ public class PlanningService {
.map(cl -> cl.getRecipe().getId()) .map(cl -> cl.getRecipe().getId())
.collect(Collectors.toSet()); .collect(Collectors.toSet());
List<SimulatedSlot> currentSlots = plan.getSlots().stream() double currentScore = computeCurrentScore(plan, config, recentlyCookedIds);
.map(s -> new SimulatedSlot(s.getRecipe(), s.getSlotDate()))
.toList();
double currentScore = currentSlots.isEmpty() ? MAX_VARIETY_SCORE
: scoreFromSimulatedSlots(currentSlots, config, recentlyCookedIds);
double projectedScore = simulateVarietyScore(plan, candidate, date, config, recentlyCookedIds); double projectedScore = simulateVarietyScore(plan, candidate, date, config, recentlyCookedIds);
return new VarietyPreviewResponse(currentScore, projectedScore, projectedScore - currentScore); return new VarietyPreviewResponse(currentScore, projectedScore, projectedScore - currentScore);