-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMealPrepControllerAdapter.java
More file actions
168 lines (150 loc) · 7.72 KB
/
MealPrepControllerAdapter.java
File metadata and controls
168 lines (150 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.cuoco.adapter.in.controller;
import com.cuoco.adapter.in.controller.model.IngredientRequest;
import com.cuoco.adapter.in.controller.model.IngredientResponse;
import com.cuoco.adapter.in.controller.model.MealPrepConfigurationRequest;
import com.cuoco.adapter.in.controller.model.MealPrepFilterRequest;
import com.cuoco.adapter.in.controller.model.MealPrepRequest;
import com.cuoco.adapter.in.controller.model.MealPrepResponse;
import com.cuoco.adapter.in.controller.model.RecipeResponse;
import com.cuoco.adapter.in.controller.model.StepResponse;
import com.cuoco.application.port.in.GetMealPrepByIdQuery;
import com.cuoco.application.port.in.GetMealPrepFromIngredientsCommand;
import com.cuoco.application.usecase.model.Ingredient;
import com.cuoco.application.usecase.model.MealPrep;
import com.cuoco.application.usecase.model.Recipe;
import com.cuoco.shared.GlobalExceptionHandler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/meal-preps")
@Tag(name = "Meal Prep", description = "Obtains recipes for MealPrep from ingredients")
public class MealPrepControllerAdapter {
private final GetMealPrepFromIngredientsCommand getMealPrepFromIngredientsCommand;
private final GetMealPrepByIdQuery getMealPrepByIdQuery;
public MealPrepControllerAdapter(
GetMealPrepFromIngredientsCommand getMealPrepFromIngredientsCommand,
GetMealPrepByIdQuery getMealPrepByIdQuery
) {
this.getMealPrepFromIngredientsCommand = getMealPrepFromIngredientsCommand;
this.getMealPrepByIdQuery = getMealPrepByIdQuery;
}
@PostMapping
@Operation(summary = "Create meal preps")
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "Return all the created meal preps",
content = @Content(
mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = MealPrepResponse.class))
)
),
@ApiResponse(
responseCode = "503",
description = "Service unavailable",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = GlobalExceptionHandler.ApiErrorResponse.class)
)
)
})
public ResponseEntity<List<MealPrepResponse>> generate(@RequestBody MealPrepRequest mealPrepRequest) {
log.info("Executing GET mealPrep from ingredients with body {}", mealPrepRequest);
List<MealPrep> mealPreps = getMealPrepFromIngredientsCommand.execute(buildGenerateMealPrepCommand(mealPrepRequest));
List<MealPrepResponse> mealPrepsResponse = mealPreps.stream().map(this::buildResponse).toList();
log.info("Successfully generated recipes");
return ResponseEntity.ok(mealPrepsResponse);
}
@GetMapping("/{id}")
@Operation(summary = "Get meal prep by ID")
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "Return the meal prep ",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = MealPrepResponse.class)
)
),
@ApiResponse(
responseCode = "404",
description = "Meal prep not found with the provided ID",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = GlobalExceptionHandler.ApiErrorResponse.class)
)
),
@ApiResponse(
responseCode = "503",
description = "Service unavailable",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = GlobalExceptionHandler.ApiErrorResponse.class)
)
)
})
public ResponseEntity<MealPrepResponse> getById(@PathVariable Long id) {
log.info("Executing GET for find meal prep with ID {}", id);
MealPrep mealPrep = getMealPrepByIdQuery.execute(id);
MealPrepResponse mealPrepResponse = buildResponse(mealPrep);
return ResponseEntity.ok(mealPrepResponse);
}
private GetMealPrepFromIngredientsCommand.Command buildGenerateMealPrepCommand(MealPrepRequest mealPrepRequest) {
if(mealPrepRequest.getFilters() == null) mealPrepRequest.setFilters(new MealPrepFilterRequest());
if(mealPrepRequest.getConfiguration() == null) mealPrepRequest.setConfiguration(new MealPrepConfigurationRequest());
return GetMealPrepFromIngredientsCommand.Command.builder()
.ingredients(mealPrepRequest.getIngredients().stream().map(this::buildIngredient).toList())
.freeze(mealPrepRequest.getFilters().getFreeze())
.preparationTimeId(mealPrepRequest.getFilters().getPreparationTimeId())
.servings(mealPrepRequest.getFilters().getServings())
.cookLevelId(mealPrepRequest.getFilters().getCookLevelId())
.dietId(mealPrepRequest.getFilters().getDietId())
.typeIds(mealPrepRequest.getFilters().getTypeIds())
.allergiesIds(mealPrepRequest.getFilters().getAllergiesIds())
.dietaryNeedsIds(mealPrepRequest.getFilters().getDietaryNeedsIds())
.size(mealPrepRequest.getConfiguration().getSize())
.notInclude(mealPrepRequest.getConfiguration().getNotInclude())
.build();
}
private Ingredient buildIngredient(IngredientRequest ingredientRequest) {
return Ingredient.builder()
.name(ingredientRequest.getName())
.build();
}
private MealPrepResponse buildResponse(MealPrep mealPrep) {
return MealPrepResponse.builder()
.id(mealPrep.getId())
.title(mealPrep.getTitle())
.estimatedCookingTime(mealPrep.getEstimatedCookingTime())
.servings(mealPrep.getServings())
.freeze(mealPrep.getFreeze())
.steps(mealPrep.getSteps().stream().map(StepResponse::fromDomain).toList())
.recipes(mealPrep.getRecipes().stream().map(this::buildRecipeResponse).toList())
.ingredients(mealPrep.getIngredients().stream().map(IngredientResponse::fromDomain).toList())
.build();
}
private RecipeResponse buildRecipeResponse(Recipe recipe) {
return RecipeResponse.builder()
.id(recipe.getId())
.name(recipe.getName())
.subtitle(recipe.getSubtitle())
.description(recipe.getDescription())
.image(recipe.getImage())
.build();
}
}