Merge branch 'bugfix/add-ingredient' into 'main'

fix(server) Fixed error when adding ingredient.

See merge request cse1105/2025-2026/teams/csep-team-76!55
This commit is contained in:
Oskar Rasieński 2026-01-14 18:52:56 +01:00
commit 04c0315f3e
3 changed files with 11 additions and 7 deletions

View file

@ -20,13 +20,12 @@ public class Ingredient {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
public long id; public Long id;
// FIXME Dec 22 2025::temporarily made this not a unique constraint because of weird JPA behaviour // FIXME Dec 22 2025::temporarily made this not a unique constraint because of weird JPA behaviour
@Column(name = "name", nullable = false, unique = false) @Column(name = "name", nullable = false, unique = false)
public String name; public String name;
@Column(name = "protein", nullable = false) @Column(name = "protein", nullable = false)
public double proteinPer100g; public double proteinPer100g;
@ -70,7 +69,7 @@ public class Ingredient {
+ fatPer100g * KCAL_PER_GRAM_FAT; + fatPer100g * KCAL_PER_GRAM_FAT;
} }
public void setId(long id) { public void setId(Long id) {
this.id = id; this.id = id;
} }
@ -86,7 +85,7 @@ public class Ingredient {
return carbsPer100g; return carbsPer100g;
} }
public long getId() { public Long getId() {
return id; return id;
} }

View file

@ -3,6 +3,7 @@ package commons;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
class IngredientTest { class IngredientTest {
@ -25,7 +26,7 @@ class IngredientTest {
assertEquals(ZERO, sugar.proteinPer100g); assertEquals(ZERO, sugar.proteinPer100g);
assertEquals(ZERO, sugar.fatPer100g); assertEquals(ZERO, sugar.fatPer100g);
assertEquals(HUNDRED, sugar.carbsPer100g); assertEquals(HUNDRED, sugar.carbsPer100g);
assertEquals(0L, sugar.id); assertNull(sugar.id);
} }
@Test @Test

View file

@ -1,5 +1,6 @@
package server.service; package server.service;
import commons.Ingredient;
import commons.Recipe; import commons.Recipe;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -88,8 +89,11 @@ public class RecipeService {
recipeIngredient.setIngredient( recipeIngredient.setIngredient(
ingredientRepository ingredientRepository
.findByName(recipeIngredient.getIngredient().name) .findByName(recipeIngredient.getIngredient().name)
.orElseGet(() -> ingredientRepository.save(recipeIngredient.getIngredient()) .orElseGet(() -> {
)) Ingredient i = recipeIngredient.getIngredient();
i.setId(null);
return ingredientRepository.save(i);
}))
); );
recipeIngredientRepository.saveAll(recipe.getIngredients()); recipeIngredientRepository.saveAll(recipe.getIngredients());
return recipeRepository.save(recipe); return recipeRepository.save(recipe);