Fixed error when adding ingredient
This commit is contained in:
parent
624a0cbe86
commit
f048e0fe3e
2 changed files with 9 additions and 6 deletions
|
|
@ -20,13 +20,12 @@ public class Ingredient {
|
|||
|
||||
@Id
|
||||
@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
|
||||
@Column(name = "name", nullable = false, unique = false)
|
||||
public String name;
|
||||
|
||||
|
||||
@Column(name = "protein", nullable = false)
|
||||
public double proteinPer100g;
|
||||
|
||||
|
|
@ -70,7 +69,7 @@ public class Ingredient {
|
|||
+ fatPer100g * KCAL_PER_GRAM_FAT;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
|
@ -86,7 +85,7 @@ public class Ingredient {
|
|||
return carbsPer100g;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package server.service;
|
||||
|
||||
import commons.Ingredient;
|
||||
import commons.Recipe;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
|
@ -88,8 +89,11 @@ public class RecipeService {
|
|||
recipeIngredient.setIngredient(
|
||||
ingredientRepository
|
||||
.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());
|
||||
return recipeRepository.save(recipe);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue