From 4dc44fb6e9d8aed726cd7b3584fad900a846234a Mon Sep 17 00:00:00 2001 From: Rithvik Sriram Date: Thu, 8 Jan 2026 22:38:52 +0100 Subject: [PATCH] Implemented Optional fields and rewrote tests --- .../java/server/api/RecipeController.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/server/api/RecipeController.java b/server/src/main/java/server/api/RecipeController.java index f28d705..f861f34 100644 --- a/server/src/main/java/server/api/RecipeController.java +++ b/server/src/main/java/server/api/RecipeController.java @@ -126,24 +126,32 @@ public class RecipeController { * Recipe name and limits the result to a set amount of results. * @param search - name of the recipe to be searched for. * @param limit - limit of the results queried for. + * @param lang - stores the info of the language of the user to provide server support/ * @return - returns a ResponseEntity with a List of Recipes and an HTTP 200 ok status. */ public ResponseEntity> getRecipes( - @RequestParam(required = false) String search, - @RequestParam(required = false) Integer limit){ + @RequestParam Optional search, + @RequestParam Optional limit, + @RequestParam Optional lang){ List recipes = recipeRepository.findAll(); - if(search != null && !search.trim().isEmpty()){ // if search isn't null or empty perform the filtering process - String lowercaseSearch = search.toLowerCase(); - recipes = recipes.stream().filter(recipe -> recipe.getName() - .toLowerCase() - .contains(lowercaseSearch)) - .toList(); - } - if(limit != null && limit finalRecipes = recipes; + recipes = search + .filter(s -> !s.trim().isEmpty()) // filters recipes if the string is not empty by doing a lowercase search + .map(s -> { + String lowercaseSearch = s.toLowerCase(); + return finalRecipes.stream() + .filter(recipe -> + recipe.getName().toLowerCase().contains(lowercaseSearch) + ) + .toList(); + }) + .orElse(recipes); + recipes = limit // filters based on limit if provided + .filter(l -> l < finalRecipes.size()) + .map(l -> finalRecipes.stream().limit(l).toList()) + .orElse(recipes); return ResponseEntity.ok(recipes); }