diff --git a/server/src/main/java/server/api/RecipeController.java b/server/src/main/java/server/api/RecipeController.java index e2143e7..7021bdd 100644 --- a/server/src/main/java/server/api/RecipeController.java +++ b/server/src/main/java/server/api/RecipeController.java @@ -20,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import server.database.RecipeRepository; import server.service.RecipeService; import java.util.List; @@ -29,9 +30,11 @@ import java.util.logging.Logger; @RestController @RequestMapping("/api") public class RecipeController { + private static final Logger logger = Logger.getLogger(RecipeController.class.getName()); - private final SimpMessagingTemplate messagingTemplate; - private final RecipeService recipeService; + private SimpMessagingTemplate messagingTemplate; + private RecipeService recipeService; + private RecipeRepository recipeRepository; public RecipeController(RecipeService recipeService, SimpMessagingTemplate messagingTemplate) { this.recipeService = recipeService; @@ -128,4 +131,42 @@ public class RecipeController { messagingTemplate.convertAndSend(Topics.RECIPES, new DeleteRecipeMessage(id)); // Send to WS. return ResponseEntity.ok(true); } + + /** + * Performs a search based on a case-insensitive partial match on + * 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 Optional search, + @RequestParam Optional limit, + @RequestParam Optional lang){ + + List recipes = recipeRepository.findAll(); + + List 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); + + } + + + }