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.

View file

@ -64,7 +64,7 @@ public class RecipeControllerTest {
public void setup(TestInfo info) {
recipes = LongStream
.range(0, NUM_RECIPES)
.mapToObj(x -> new Recipe(null, "Recipe " + x, List.of(), List.of()))
.mapToObj(x -> new Recipe(null, "Recipe " + x, "en", List.of(), List.of()))
.toList();
controller = new RecipeController(
recipeService,
@ -107,7 +107,7 @@ public class RecipeControllerTest {
@Tag("test-from-init-data")
public void getManyRecipes() {
// The number of recipes returned is the same as the entire input list
assertEquals(recipes.size(), controller.getRecipes(Optional.empty()).getBody().size());
assertEquals(recipes.size(), controller.getRecipes(Optional.empty(), Optional.empty()).getBody().size());
}
@Test
@ -115,7 +115,29 @@ public class RecipeControllerTest {
public void getSomeRecipes() {
final int LIMIT = 5;
// The number of recipes returned is the same as the entire input list
assertEquals(LIMIT, controller.getRecipes(Optional.of(LIMIT)).getBody().size());
assertEquals(LIMIT, controller.getRecipes(Optional.empty(), Optional.of(LIMIT)).getBody().size());
}
@Test
@Tag("test-from-init-data")
public void getManyRecipesWithLocale() {
// The number of recipes returned is the same as the entire input list
assertEquals(recipes.size(), controller.getRecipes(Optional.of(List.of("en", "nl")), Optional.empty()).getBody().size());
}
@Test
@Tag("test-from-init-data")
public void getNoRecipesWithLocale() {
// should have NO Dutch recipes (thank god)
assertEquals(0, controller.getRecipes(Optional.of(List.of("nl")), Optional.empty()).getBody().size());
}
@Test
@Tag("test-from-init-data")
public void getSomeRecipesWithLocale() {
final int LIMIT = 5;
// The number of recipes returned is the same as the entire input list
assertEquals(LIMIT, controller.getRecipes(Optional.of(List.of("en", "nl")), Optional.of(LIMIT)).getBody().size());
}
@Test