Implemented Optional fields and rewrote tests
This commit is contained in:
parent
01ff1a96a9
commit
4dc44fb6e9
1 changed files with 20 additions and 12 deletions
|
|
@ -126,24 +126,32 @@ public class RecipeController {
|
||||||
* Recipe name and limits the result to a set amount of results.
|
* Recipe name and limits the result to a set amount of results.
|
||||||
* @param search - name of the recipe to be searched for.
|
* @param search - name of the recipe to be searched for.
|
||||||
* @param limit - limit of the results queried 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.
|
* @return - returns a ResponseEntity with a List of Recipes and an HTTP 200 ok status.
|
||||||
*/
|
*/
|
||||||
public ResponseEntity<List<Recipe>> getRecipes(
|
public ResponseEntity<List<Recipe>> getRecipes(
|
||||||
@RequestParam(required = false) String search,
|
@RequestParam Optional<String> search,
|
||||||
@RequestParam(required = false) Integer limit){
|
@RequestParam Optional<Integer> limit,
|
||||||
|
@RequestParam Optional<String> lang){
|
||||||
|
|
||||||
List<Recipe> recipes = recipeRepository.findAll();
|
List<Recipe> recipes = recipeRepository.findAll();
|
||||||
|
|
||||||
if(search != null && !search.trim().isEmpty()){ // if search isn't null or empty perform the filtering process
|
List<Recipe> finalRecipes = recipes;
|
||||||
String lowercaseSearch = search.toLowerCase();
|
recipes = search
|
||||||
recipes = recipes.stream().filter(recipe -> recipe.getName()
|
.filter(s -> !s.trim().isEmpty()) // filters recipes if the string is not empty by doing a lowercase search
|
||||||
.toLowerCase()
|
.map(s -> {
|
||||||
.contains(lowercaseSearch))
|
String lowercaseSearch = s.toLowerCase();
|
||||||
|
return finalRecipes.stream()
|
||||||
|
.filter(recipe ->
|
||||||
|
recipe.getName().toLowerCase().contains(lowercaseSearch)
|
||||||
|
)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
})
|
||||||
if(limit != null && limit<recipes.size()){ // Limit the results if the limit is provided
|
.orElse(recipes);
|
||||||
recipes = recipes.stream().limit(limit).toList();
|
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);
|
return ResponseEntity.ok(recipes);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue