Merge branch 'feature/server_search_bar' into 'main'

Added backend server search functionality with unit tests

Closes #33

See merge request cse1105/2025-2026/teams/csep-team-76!33
This commit is contained in:
Zhongheng Liu 2026-01-09 16:35:02 +01:00
commit 07adedde3e

View file

@ -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<List<Recipe>> getRecipes(
@RequestParam Optional<String> search,
@RequestParam Optional<Integer> limit,
@RequestParam Optional<String> lang){
List<Recipe> recipes = recipeRepository.findAll();
List<Recipe> 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);
}
}