Fixed error when adding ingredient

This commit is contained in:
Oskar Rasieński 2026-01-14 18:39:14 +01:00
commit f048e0fe3e
2 changed files with 9 additions and 6 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

@ -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);