explore page working i believe

This commit is contained in:
durn
2026-04-17 19:30:25 -06:00
parent b26af70ef5
commit c2e6722829
9 changed files with 300 additions and 23 deletions
@@ -59,12 +59,15 @@ public class RecipeController {
@GetMapping("/search")
public ResponseEntity<List<RecipeDto>> searchRecipes(
@RequestParam(required = false) String name, // by not adding a name all recipes will appear basically
@RequestParam(required = false) List<String> tags // since users can choose no tags this isnt required
@RequestParam(required = false) List<String> tags, // since users can choose no tags this isnt required
@RequestParam(required = false) List<Integer> prices,
@RequestParam(required = false) List<Integer> cookTime,
@RequestParam(required = false) List<Integer> prepTime
) {
List<RecipeDto> recipes = recipeService.getRecipes(name, tags);
List<RecipeDto> recipes = recipeService.getRecipes(name, tags, prices, cookTime, prepTime);
return new ResponseEntity<>(recipes, HttpStatus.OK);
}
@@ -62,11 +62,18 @@ public class SiteController {
public String explore(
@RequestParam(required = false) String q,
@RequestParam(required = false) List<String> tags,
@RequestParam(required = false) List<Integer> prices,
@RequestParam(required = false) List<Integer> cookTime,
@RequestParam(required = false) List<Integer> prepTime,
Model model
) {
List<RecipeDto> recipes = recipeService.getRecipes(q, tags);
List<RecipeDto> recipes = recipeService.getRecipes(q, tags, prices, cookTime, prepTime);
model.addAttribute("recipes", recipes);
String displayQuery = q;
model.addAttribute("q", q);
model.addAttribute("tags", tags);
return "explore";
}
@@ -17,6 +17,7 @@ public class RecipeDto {
private List<StepDto> steps;
private List<ImageDto> images;
private List<TagDto> tags;
private Integer cost;
public RecipeDto() {
super();
@@ -24,7 +25,7 @@ public class RecipeDto {
public RecipeDto(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes,
Integer servings, UserDto userDto, String status, List<RecipeIngredientDto> ingredients,
List<StepDto> steps, List<ImageDto> images, List<TagDto> tags) {
List<StepDto> steps, List<ImageDto> images, List<TagDto> tags, Integer cost) {
super();
this.title = title;
this.description = description;
@@ -37,6 +38,7 @@ public class RecipeDto {
this.steps = steps;
this.images = images;
this.tags = tags;
this.cost = cost;
}
// getters and setters
@@ -136,4 +138,14 @@ public class RecipeDto {
public void setId(Integer id) {
this.id = id;
}
public Integer getCost() {
return cost;
}
public void setCost(Integer cost) {
this.cost = cost;
}
}
@@ -40,6 +40,10 @@ public class Recipe {
private String status;
@NotNull(message = "Please Provide a cost")
@Positive(message = "This value cannot be negative")
private Integer cost;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
@@ -76,7 +80,7 @@ public class Recipe {
}
public Recipe(String title, String description, Integer prepTimeMinutes, Integer cookTimeMinutes, Integer servings,
User user, String status) {
User user, String status, Integer cost) {
this.title = title;
this.description = description;
this.prepTimeMinutes = prepTimeMinutes;
@@ -86,6 +90,7 @@ public class Recipe {
this.status = status;
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
this.cost = cost;
}
// Getters and setters
@@ -208,4 +213,12 @@ public class Recipe {
public void setUsers(Set<User> users) {
this.users = users;
}
public Integer getCost() {
return cost;
}
public void setCost(Integer cost) {
this.cost = cost;
}
}
@@ -76,7 +76,7 @@ public class RecipeServiceImpl implements RecipeService {
RecipeDto dto = new RecipeDto(recipe.getTitle(), recipe.getDescription(), recipe.getPrepTimeMinutes(),
recipe.getCookTimeMinutes(), recipe.getServings(), userDto, recipe.getStatus(), ingredientDtos,
stepDtos, imageDtos, tagDtos);
stepDtos, imageDtos, tagDtos, recipe.getCost());
dto.setId(recipe.getId());
@@ -131,7 +131,7 @@ public class RecipeServiceImpl implements RecipeService {
Recipe recipe = new Recipe(dto.getTitle(), dto.getDescription(), dto.getPrepTimeMinutes(),
dto.getCookTimeMinutes(), dto.getServings(), currentUser, dto.getStatus());
dto.getCookTimeMinutes(), dto.getServings(), currentUser, dto.getStatus(), dto.getCost());
if (dto.getIngredients() != null) {
for (RecipeIngredientDto riDto : dto.getIngredients()) {
@@ -218,6 +218,7 @@ public class RecipeServiceImpl implements RecipeService {
existingRecipe.setCookTimeMinutes(recipeDto.getCookTimeMinutes());
existingRecipe.setServings(recipeDto.getServings());
existingRecipe.setStatus(recipeDto.getStatus());
existingRecipe.setCost(recipeDto.getCost());
List<RecipeIngredientDto> updatedIngredients = recipeDto.getIngredients();
List<RecipeIngredient> ingredientsToRemove = new ArrayList<>();
@@ -371,7 +372,7 @@ public class RecipeServiceImpl implements RecipeService {
@Override
@Transactional
public List<RecipeDto> getRecipes(String name, List<String> tags) {
public List<RecipeDto> getRecipes(String name, List<String> tags, List<Integer> prices, List<Integer> cookTime, List<Integer> prepTime) {
List<Recipe> recipes;
@@ -388,6 +389,45 @@ public class RecipeServiceImpl implements RecipeService {
.filter(recipe -> recipe.getTags().stream().anyMatch(tag -> tags.contains(tag.getName())))
.collect(Collectors.toList());
}
if (prices != null && !prices.isEmpty() && !recipes.isEmpty()) {
recipes = recipes.stream()
.filter(recipe -> prices.contains(recipe.getCost()))
.collect(Collectors.toList());
}
if (cookTime != null && !cookTime.isEmpty() && !recipes.isEmpty()) {
recipes = recipes.stream()
.filter(recipe -> {
int minutes = recipe.getCookTimeMinutes();
for (Integer ct : cookTime) {
if (ct == 15 && minutes <= 15) return true;
if (ct == 30 && minutes > 15 && minutes <= 30) return true;
if (ct == 60 && minutes > 30 && minutes <= 60) return true;
if (ct == 120 && minutes > 60 && minutes <= 120) return true;
if (ct == 121 && minutes > 120) return true;
}
return false;
})
.collect(Collectors.toList());
}
if (prepTime != null && !prepTime.isEmpty() && !recipes.isEmpty()) {
recipes = recipes.stream()
.filter(recipe -> {
int minutes = recipe.getPrepTimeMinutes();
for (Integer ct : prepTime) {
if (ct == 15 && minutes <= 15) return true;
if (ct == 30 && minutes > 15 && minutes <= 30) return true;
if (ct == 60 && minutes > 30 && minutes <= 60) return true;
if (ct == 240 && minutes > 60 && minutes <= 240) return true;
if (ct == 241 && minutes > 240) return true;
}
return false;
})
.collect(Collectors.toList());
}
List<RecipeDto> recipeList = new ArrayList<>();
@@ -17,7 +17,7 @@ public interface RecipeService {
RecipeDto getRecipeById(Integer recipeId);
List<RecipeDto> getRecipes(String name, List<String> tags);
List<RecipeDto> getRecipes(String name, List<String> tags, List<Integer> prices, List<Integer> cookTime, List<Integer> prepTime);
RecipeDto updateRecipe(RecipeDto recipedto, Integer Id, String currentUsername);