made a few tests for ingredients also did a TODO, made the code for RecipeControllerTest a little bit more readable too
This commit is contained in:
parent
8542e66869
commit
e2ccad1116
3 changed files with 136 additions and 48 deletions
|
|
@ -31,12 +31,14 @@ import java.util.logging.Logger;
|
|||
@RequestMapping("/api")
|
||||
public class RecipeController {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(RecipeController.class.getName());
|
||||
private static final Logger logger = Logger.getLogger(
|
||||
RecipeController.class.getName());
|
||||
private SimpMessagingTemplate messagingTemplate;
|
||||
private RecipeService recipeService;
|
||||
private RecipeRepository recipeRepository;
|
||||
|
||||
public RecipeController(RecipeService recipeService, SimpMessagingTemplate messagingTemplate) {
|
||||
public RecipeController(RecipeService recipeService,
|
||||
SimpMessagingTemplate messagingTemplate) {
|
||||
this.recipeService = recipeService;
|
||||
this.messagingTemplate = messagingTemplate;
|
||||
logger.info("Initialized controller.");
|
||||
|
|
@ -72,24 +74,26 @@ public class RecipeController {
|
|||
) {
|
||||
logger.info("GET /recipes called.");
|
||||
|
||||
// TODO: maybe refactor this. this is horrid and evil and nightmare
|
||||
var recipes = locales
|
||||
.map(loc -> {
|
||||
return limit.map(lim -> {
|
||||
return recipeService.findAllWithLocales(loc, lim);
|
||||
})
|
||||
.orElseGet(() -> {
|
||||
return recipeService.findAllWithLocales(loc);
|
||||
});
|
||||
})
|
||||
.orElseGet(
|
||||
// Choose the right overload. One has a limit, other doesn't.
|
||||
() -> limit.map(recipeService::findAll).orElseGet(recipeService::findAll));
|
||||
|
||||
List<Recipe> recipes = locales
|
||||
.map(loc -> findWithLocales(loc, limit))
|
||||
.orElseGet(() -> findAll(limit));
|
||||
|
||||
return ResponseEntity.ok(recipes);
|
||||
}
|
||||
|
||||
private List<Recipe> findWithLocales(List<String> locales, Optional<Integer> limit) {
|
||||
return limit
|
||||
.map(lim -> recipeService.findAllWithLocales(locales, lim))
|
||||
.orElseGet(() -> recipeService.findAllWithLocales(locales));
|
||||
}
|
||||
|
||||
private List<Recipe> findAll(Optional<Integer> limit) {
|
||||
return limit
|
||||
.map(recipeService::findAll)
|
||||
.orElseGet(recipeService::findAll);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mapping for <code>POST /recipe/{id}</code>.
|
||||
* Also creates the ingredient elements if they do not exist.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue