Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/de/schulung/quarkus/recipes/DifficultyConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.schulung.quarkus.recipes;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter(autoApply = true)
public class DifficultyConverter implements AttributeConverter<Difficulty, String> {

@Override
public String convertToDatabaseColumn(Difficulty attribute) {
if (attribute == null) {
return null;
}
return switch (attribute) {
case easy -> "easy";
case medium -> "medium";
case hard -> "hard";
};
}

@Override
public Difficulty convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
return switch (dbData) {
case "easy" -> Difficulty.easy;
case "medium" -> Difficulty.medium;
case "hard" -> Difficulty.hard;
default -> throw new IllegalArgumentException("Unknown difficulty: " + dbData);
};
}
}
1 change: 0 additions & 1 deletion src/main/java/de/schulung/quarkus/recipes/Ingredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class Ingredient {
* The unit of the ingredient.
*/
@NotNull
@Enumerated(EnumType.STRING)
private IngredientUnit unit;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package de.schulung.quarkus.recipes;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter(autoApply = true)
public class IngredientUnitConverter implements AttributeConverter<IngredientUnit, String> {

@Override
public String convertToDatabaseColumn(IngredientUnit attribute) {
if (attribute == null) {
return null;
}
return switch (attribute) {
case pieces -> "pieces";
case grams -> "grams";
case kilograms -> "kilograms";
case milliliters -> "milliliters";
case liters -> "liters";
case teaspoons -> "teaspoons";
case tablespoons -> "tablespoons";
case cups -> "cups";
case cloves -> "cloves";
case pinches -> "pinches";
case packages -> "packages";
case cans -> "cans";
case bottles -> "bottles";
case slices -> "slices";
case sprigs -> "sprigs";
case stalks -> "stalks";
case cubes -> "cubes";
};
}

@Override
public IngredientUnit convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
return switch (dbData) {
case "pieces" -> IngredientUnit.pieces;
case "grams" -> IngredientUnit.grams;
case "kilograms" -> IngredientUnit.kilograms;
case "milliliters" -> IngredientUnit.milliliters;
case "liters" -> IngredientUnit.liters;
case "teaspoons" -> IngredientUnit.teaspoons;
case "tablespoons" -> IngredientUnit.tablespoons;
case "cups" -> IngredientUnit.cups;
case "cloves" -> IngredientUnit.cloves;
case "pinches" -> IngredientUnit.pinches;
case "packages" -> IngredientUnit.packages;
case "cans" -> IngredientUnit.cans;
case "bottles" -> IngredientUnit.bottles;
case "slices" -> IngredientUnit.slices;
case "sprigs" -> IngredientUnit.sprigs;
case "stalks" -> IngredientUnit.stalks;
case "cubes" -> IngredientUnit.cubes;
default -> throw new IllegalArgumentException("Unknown ingredient unit: " + dbData);
};
}
}
3 changes: 1 addition & 2 deletions src/main/java/de/schulung/quarkus/recipes/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@ public class Recipe {
/**
* Difficulty level of the recipe.
*/
@Enumerated(EnumType.STRING)
private Difficulty difficulty;
/**
* List of ingredients.
*/
@NotNull
@Size(max = 100)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "recipe_id")
private List<@Valid @NotNull Ingredient> ingredients;
/**
Expand Down