Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Alba María Álvarez
/
AppRecetas
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2c026c2b
authored
Sep 04, 2025
by
Alba María Álvarez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
feat(recipe/controller): implementado guardado de recetas correctamente junto con la imagen
parent
a7af2bad
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
5 deletions
src/main/java/com/example/apprecetas/recipe/infrastructure/controller/CreateRecipeController.java
src/main/java/com/example/apprecetas/recipe/infrastructure/controller/CreateRecipeController.java
View file @
2c026c2b
...
@@ -2,16 +2,20 @@ package com.example.apprecetas.recipe.infrastructure.controller;
...
@@ -2,16 +2,20 @@ package com.example.apprecetas.recipe.infrastructure.controller;
import
com.example.apprecetas.exception.UnprocessableEntityException
;
import
com.example.apprecetas.exception.UnprocessableEntityException
;
import
com.example.apprecetas.recipe.application.CreateRecipeUseCase
;
import
com.example.apprecetas.recipe.application.CreateRecipeUseCase
;
import
com.example.apprecetas.recipe.application.FileUploadUseCase
;
import
com.example.apprecetas.recipe.domain.entity.Recipe
;
import
com.example.apprecetas.recipe.infrastructure.controller.dto.input.RecipeInputDto
;
import
com.example.apprecetas.recipe.infrastructure.controller.dto.input.RecipeInputDto
;
import
com.example.apprecetas.recipe.infrastructure.controller.dto.output.RecipeOutputDto
;
import
com.example.apprecetas.recipe.infrastructure.controller.dto.output.RecipeOutputDto
;
import
com.example.apprecetas.recipe.infrastructure.mapper.IngredientMapper
;
import
com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper
;
import
com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper
;
import
com.example.apprecetas.recipe.infrastructure.mapper.StepMapper
;
import
jakarta.validation.Valid
;
import
jakarta.validation.Valid
;
import
lombok.RequiredArgsConstructor
;
import
lombok.RequiredArgsConstructor
;
import
org.mapstruct.factory.Mappers
;
import
org.mapstruct.factory.Mappers
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.ModelAttribute
;
import
org.springframework.web.bind.annotation.PostMapping
;
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.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
...
@@ -25,18 +29,39 @@ public class CreateRecipeController {
...
@@ -25,18 +29,39 @@ public class CreateRecipeController {
private
final
CreateRecipeUseCase
service
;
private
final
CreateRecipeUseCase
service
;
private
final
FileUploadUseCase
fileUploadUseCase
;
private
final
RecipeMapper
mapper
=
Mappers
.
getMapper
(
RecipeMapper
.
class
);
private
final
RecipeMapper
mapper
=
Mappers
.
getMapper
(
RecipeMapper
.
class
);
private
final
IngredientMapper
ingredientMapper
=
Mappers
.
getMapper
(
IngredientMapper
.
class
);
private
final
StepMapper
stepMapper
=
Mappers
.
getMapper
(
StepMapper
.
class
);
@PostMapping
@PostMapping
(
consumes
=
{
"multipart/form-data"
})
public
ResponseEntity
<
RecipeOutputDto
>
create
(
@
RequestBody
@Valid
RecipeInputDto
recipeInputDto
,
BindingResult
result
)
{
public
ResponseEntity
<
RecipeOutputDto
>
create
(
@
ModelAttribute
@Valid
RecipeInputDto
recipeInputDto
,
BindingResult
result
)
{
if
(
result
.
hasErrors
())
{
if
(
result
.
hasErrors
())
{
String
errorMsg
=
result
.
getFieldErrors
().
stream
()
String
errorMsg
=
result
.
getFieldErrors
().
stream
()
.
map
(
fieldError
->
fieldError
.
getField
()
+
": "
+
fieldError
.
getDefaultMessage
())
.
map
(
fieldError
->
fieldError
.
getField
()
+
": "
+
fieldError
.
getDefaultMessage
())
.
collect
(
Collectors
.
joining
(
";"
));
.
collect
(
Collectors
.
joining
(
";"
));
throw
new
UnprocessableEntityException
(
errorMsg
);
throw
new
UnprocessableEntityException
(
errorMsg
);
}
}
URI
location
=
URI
.
create
(
"recipe"
);
// Sube la imagen y obtiene la URL
return
ResponseEntity
.
created
(
location
).
body
(
mapper
.
map
(
service
.
create
(
mapper
.
map
(
recipeInputDto
))));
String
pictureUrl
=
null
;
if
(
recipeInputDto
.
getPicture
()
!=
null
&&
!
recipeInputDto
.
getPicture
().
isEmpty
())
{
pictureUrl
=
fileUploadUseCase
.
uploadFile
(
recipeInputDto
.
getPicture
());
}
// Crea la entidad de dominio y le asigna la URL
Recipe
recipe
=
new
Recipe
();
recipe
.
setName
(
recipeInputDto
.
getName
());
recipe
.
setDescription
(
recipeInputDto
.
getDescription
());
recipe
.
setPicture
(
pictureUrl
);
recipe
.
setIngredients
(
recipeInputDto
.
getIngredients
().
stream
().
map
(
ingredientMapper:
:
map
).
collect
(
Collectors
.
toSet
()));
recipe
.
setSteps
(
recipeInputDto
.
getSteps
().
stream
().
map
(
stepMapper:
:
map
).
collect
(
Collectors
.
toSet
()));
// Pasa el objeto al servicio
Recipe
createdRecipe
=
service
.
create
(
recipe
);
URI
location
=
URI
.
create
(
"recipe/"
+
createdRecipe
.
getId
());
return
ResponseEntity
.
created
(
location
).
body
(
mapper
.
map
(
createdRecipe
));
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment