Merge branch 'feature/server-recipe-i18n' into 'main'

Server-side recipe internationalization

See merge request cse1105/2025-2026/teams/csep-team-76!47
This commit is contained in:
Natalia Cholewa 2026-01-09 22:28:31 +01:00
commit 2224fe1f51
7 changed files with 87 additions and 13 deletions

View file

@ -59,19 +59,35 @@ public class RecipeController {
}
/**
* Mapping for <code>GET /recipes(?limit=)</code>
* Mapping for <code>GET /recipes(?limit=)(&locales=)</code>
* <p>
* If the limit parameter is unspecified, return all recipes in the repository.
* @param limit Integer limit of items you want to get
* @return The list of recipes
*/
@GetMapping("/recipes")
public ResponseEntity<List<Recipe>> getRecipes(@RequestParam Optional<Integer> limit) {
public ResponseEntity<List<Recipe>> getRecipes(
@RequestParam Optional<List<String>> locales,
@RequestParam Optional<Integer> limit
) {
logger.info("GET /recipes called.");
return ResponseEntity.ok(
// Choose the right overload. One has a limit, other doesn't.
limit.map(recipeService::findAll).orElseGet(recipeService::findAll)
);
// 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));
return ResponseEntity.ok(recipes);
}
/**

View file

@ -19,6 +19,15 @@ import org.springframework.data.jpa.repository.JpaRepository;
import commons.Recipe;
import java.util.Collection;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface RecipeRepository extends JpaRepository<Recipe, Long> {
boolean existsByName(String name);
List<Recipe> findAllByLocaleIsIn(Collection<String> locales);
Page<Recipe> findAllByLocaleIsIn(Collection<String> locales, Pageable pageable);
}

View file

@ -7,6 +7,7 @@ import server.database.IngredientRepository;
import server.database.RecipeIngredientRepository;
import server.database.RecipeRepository;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@ -36,6 +37,14 @@ public class RecipeService {
return recipeRepository.findAll(PageRequest.of(0, limit)).toList();
}
public List<Recipe> findAllWithLocales(Collection<String> locales) {
return recipeRepository.findAllByLocaleIsIn(locales);
}
public List<Recipe> findAllWithLocales(Collection<String> locales, int limit) {
return recipeRepository.findAllByLocaleIsIn(locales, PageRequest.of(0, limit)).toList();
}
/**
* Creates a new recipe. Returns empty if the recipe with the same name already exists.
* @param recipe Recipe to be saved in the db.