Skip to content

Commit b5d0ddc

Browse files
committed
Added image support for recipes
1 parent fc9607d commit b5d0ddc

File tree

17 files changed

+102
-49
lines changed

17 files changed

+102
-49
lines changed

app/data/images/spaghett.jpg

110 KB
Loading

app/src/main/java/dev/blaauwendraad/recipe_book/data/model/RecipeEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public class RecipeEntity extends PanacheEntityBase {
3535
@Nullable
3636
public String description;
3737

38+
@Column(name = "image_name")
39+
@Nullable
40+
public String imageName;
41+
3842
@Column(name = "num_servings")
3943
@SuppressWarnings("NullAway.Init")
4044
public Integer numServings;

app/src/main/java/dev/blaauwendraad/recipe_book/repository/RecipeRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public Long persistRecipeEntity(
3434
UserAccountEntity userAccountEntity,
3535
String title,
3636
@Nullable String description,
37+
@Nullable String imageName,
3738
Integer numServings,
3839
PreparationTime preparationTime,
3940
List<Ingredient> ingredients,
@@ -49,6 +50,7 @@ public Long persistRecipeEntity(
4950
var recipeEntity = existingRecipeEntity != null ? existingRecipeEntity : new RecipeEntity();
5051
recipeEntity.title = title;
5152
recipeEntity.description = description;
53+
recipeEntity.imageName = imageName;
5254
recipeEntity.numServings = numServings;
5355
recipeEntity.preparationTime = preparationTime;
5456

app/src/main/java/dev/blaauwendraad/recipe_book/service/RecipeService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private RecipeSummary toRecipeSummary(RecipeEntity recipeEntity) {
6262
recipeEntity.id,
6363
recipeEntity.title,
6464
recipeEntity.description,
65+
recipeEntity.imageName,
6566
recipeEntity.numServings,
6667
recipeEntity.preparationTime,
6768
recipeEntity.author == null ? null : new Author(recipeEntity.author.id, recipeEntity.author.username));
@@ -77,6 +78,7 @@ public Recipe getRecipeById(Long recipeId) {
7778
recipeEntity.id,
7879
recipeEntity.title,
7980
recipeEntity.description,
81+
recipeEntity.imageName,
8082
recipeEntity.numServings,
8183
recipeEntity.preparationTime,
8284
recipeEntity.author == null ? null : new Author(recipeEntity.author.id, recipeEntity.author.username),
@@ -92,6 +94,7 @@ public Recipe getRecipeById(Long recipeId) {
9294
public Long createRecipe(
9395
String title,
9496
@Nullable String description,
97+
@Nullable String imageName,
9598
Integer numServings,
9699
PreparationTime preparationTime,
97100
Long userId,
@@ -106,6 +109,7 @@ public Long createRecipe(
106109
userAccountEntity,
107110
title,
108111
description,
112+
imageName,
109113
numServings,
110114
preparationTime,
111115
ingredients,
@@ -134,6 +138,7 @@ public void updateRecipe(
134138
Long recipeId,
135139
String title,
136140
@Nullable String description,
141+
@Nullable String imageName,
137142
Integer numServings,
138143
PreparationTime preparationTime,
139144
Long userId,
@@ -156,6 +161,7 @@ public void updateRecipe(
156161
existingRecipeEntity.author,
157162
title,
158163
description,
164+
imageName,
159165
numServings,
160166
preparationTime,
161167
ingredients,

app/src/main/java/dev/blaauwendraad/recipe_book/service/model/Recipe.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public record Recipe(
77
Long id,
88
String title,
99
@Nullable String description,
10+
@Nullable String imageName,
1011
Integer numServings,
1112
PreparationTime preparationTime,
1213
@Nullable Author author,

app/src/main/java/dev/blaauwendraad/recipe_book/service/model/RecipeSummary.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public record RecipeSummary(
66
Long id,
77
String title,
88
@Nullable String description,
9+
@Nullable String imageName,
910
Integer numServings,
1011
PreparationTime preparationTime,
1112
@Nullable Author author) {}

app/src/main/java/dev/blaauwendraad/recipe_book/web/RecipeResource.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import jakarta.ws.rs.Produces;
3333
import jakarta.ws.rs.core.MediaType;
3434
import jakarta.ws.rs.core.Response;
35+
import java.io.File;
3536
import org.eclipse.microprofile.jwt.JsonWebToken;
3637

3738
@ApplicationScoped
@@ -69,6 +70,7 @@ public RecipeSummariesResponse getRecipeSummaries(@PathParam("filter") RecipeSum
6970
recipeSummary.id(),
7071
recipeSummary.title(),
7172
recipeSummary.description(),
73+
recipeSummary.imageName(),
7274
recipeSummary.numServings(),
7375
recipeSummary.preparationTime(),
7476
recipeSummary.author() == null
@@ -93,6 +95,7 @@ public RecipeResponse getRecipe(@PathParam("recipeId") Long id) {
9395
recipe.id(),
9496
recipe.title(),
9597
recipe.description(),
98+
recipe.imageName(),
9699
recipe.numServings(),
97100
recipe.preparationTime(),
98101
recipe.author() == null
@@ -107,6 +110,22 @@ public RecipeResponse getRecipe(@PathParam("recipeId") Long id) {
107110
.toList()));
108111
}
109112

113+
@GET
114+
@Path("/{recipeId}/image")
115+
@PermitAll
116+
@Produces("image/jpeg")
117+
public Response getRecipeImage(@PathParam("recipeId") Long id) {
118+
Recipe recipe = recipeService.getRecipeById(id);
119+
if (recipe == null) {
120+
throw new NotFoundException("Recipe not found with recipeId: " + id);
121+
}
122+
File imageFile = new File("data/images/" + recipe.imageName());
123+
if (!imageFile.exists()) {
124+
throw new NotFoundException("Image not found for recipeId: " + id);
125+
}
126+
return Response.ok(imageFile).build();
127+
}
128+
110129
@POST
111130
@Produces(MediaType.APPLICATION_JSON)
112131
@Consumes(MediaType.APPLICATION_JSON)
@@ -115,6 +134,7 @@ public SaveRecipeResponseDto createRecipe(@NotNull @Valid SaveRecipeRequestDto n
115134
Long recipeId = recipeService.createRecipe(
116135
newRecipe.title(),
117136
newRecipe.description(),
137+
newRecipe.imageName(),
118138
newRecipe.numServings(),
119139
newRecipe.preparationTime(),
120140
Long.valueOf(jwt.getName()),
@@ -138,6 +158,7 @@ public SaveRecipeResponseDto updateRecipe(
138158
recipeId,
139159
updatedRecipe.title(),
140160
updatedRecipe.description(),
161+
updatedRecipe.imageName(),
141162
updatedRecipe.numServings(),
142163
updatedRecipe.preparationTime(),
143164
Long.valueOf(jwt.getName()),

app/src/main/java/dev/blaauwendraad/recipe_book/web/model/RecipeDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public record RecipeDto(
88
Long id,
99
String title,
1010
@Nullable String description,
11+
@Nullable String imageName,
1112
Integer numServings,
1213
PreparationTime preparationTime,
1314
@Nullable RecipeAuthorDto author,

app/src/main/java/dev/blaauwendraad/recipe_book/web/model/RecipeSummaryDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public record RecipeSummaryDto(
77
Long id,
88
String title,
99
@Nullable String description,
10+
@Nullable String imageName,
1011
Integer numServings,
1112
PreparationTime preparationTime,
1213
@Nullable RecipeAuthorDto author) {}

app/src/main/java/dev/blaauwendraad/recipe_book/web/model/SaveRecipeRequestDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public record SaveRecipeRequestDto(
1414
@NotBlank @Size(min = 5, max = 100) String title,
1515
@Nullable @Size(max = 2000) String description,
16+
@Nullable @Size(max = 255) String imageName,
1617
@NotNull @Positive @Max(100) Integer numServings,
1718
@NotNull PreparationTime preparationTime,
1819
@Size(min = 1, max = 50) List<@Valid IngredientDto> ingredients,

0 commit comments

Comments
 (0)