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
1f529615
authored
Sep 10, 2025
by
Alba María Álvarez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
perf(recipe/controller): añadido tratamiento de la imagen en crear y actualizar receta
parent
e364cc2f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
17 deletions
src/main/java/com/example/apprecetas/recipe/infrastructure/controller/CreateRecipeController.java
src/main/java/com/example/apprecetas/recipe/infrastructure/controller/UpdateRecipeController.java
src/main/java/com/example/apprecetas/recipe/infrastructure/controller/CreateRecipeController.java
View file @
1f529615
...
...
@@ -6,9 +6,7 @@ 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.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.StepMapper
;
import
jakarta.validation.Valid
;
import
lombok.RequiredArgsConstructor
;
import
org.mapstruct.factory.Mappers
;
...
...
@@ -32,8 +30,6 @@ public class CreateRecipeController {
private
final
FileUploadUseCase
fileUploadUseCase
;
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
(
consumes
=
{
"multipart/form-data"
})
public
ResponseEntity
<
RecipeOutputDto
>
create
(
@ModelAttribute
@Valid
RecipeInputDto
recipeInputDto
,
BindingResult
result
)
{
...
...
@@ -49,15 +45,8 @@ public class CreateRecipeController {
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
recipe
=
mapper
.
map
(
recipeInputDto
);
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
());
...
...
src/main/java/com/example/apprecetas/recipe/infrastructure/controller/UpdateRecipeController.java
View file @
1f529615
package
com
.
example
.
apprecetas
.
recipe
.
infrastructure
.
controller
;
import
com.example.apprecetas.exception.UnprocessableEntityException
;
import
com.example.apprecetas.recipe.application.FileUploadUseCase
;
import
com.example.apprecetas.recipe.application.ReadRecipeUseCase
;
import
com.example.apprecetas.recipe.application.UpdateRecipeUseCase
;
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.output.RecipeOutputDto
;
import
com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper
;
import
com.example.apprecetas.user.application.ReadUserUseCase
;
import
com.example.apprecetas.user.domain.entity.User
;
import
jakarta.validation.Valid
;
import
lombok.RequiredArgsConstructor
;
import
org.mapstruct.factory.Mappers
;
...
...
@@ -29,16 +28,23 @@ public class UpdateRecipeController {
private
final
ReadRecipeUseCase
readRecipeUseCase
;
private
final
FileUploadUseCase
fileUploadUseCase
;
private
final
RecipeMapper
mapper
=
Mappers
.
getMapper
(
RecipeMapper
.
class
);
@PutMapping
(
"/{id}"
)
public
ResponseEntity
<
RecipeOutputDto
>
update
(
@PathVariable
String
id
,
@
RequestBody
@Valid
RecipeInputDto
recipeInputDto
,
BindingResult
result
)
{
@PutMapping
(
value
=
"/{id}"
,
consumes
=
{
"multipart/form-data"
}
)
public
ResponseEntity
<
RecipeOutputDto
>
update
(
@PathVariable
String
id
,
@
ModelAttribute
@Valid
RecipeInputDto
recipeInputDto
,
BindingResult
result
)
{
if
(
result
.
hasErrors
())
{
String
errorMsg
=
result
.
getFieldErrors
().
stream
()
.
map
(
fieldError
->
fieldError
.
getField
()
+
": "
+
fieldError
.
getDefaultMessage
())
.
collect
(
Collectors
.
joining
(
";"
));
throw
new
UnprocessableEntityException
(
errorMsg
);
}
// Sube la imagen y obtiene la URL
String
pictureUrl
=
null
;
if
(
recipeInputDto
.
getPicture
()
!=
null
&&
!
recipeInputDto
.
getPicture
().
isEmpty
())
{
pictureUrl
=
fileUploadUseCase
.
uploadFile
(
recipeInputDto
.
getPicture
());
}
Recipe
recipe
=
readRecipeUseCase
.
readById
(
id
);
String
userId
=
SecurityContextHolder
.
getContext
().
getAuthentication
().
getName
();
...
...
@@ -46,7 +52,11 @@ public class UpdateRecipeController {
if
(!
recipe
.
getUserId
().
equals
(
userId
))
return
ResponseEntity
.
status
(
HttpStatus
.
UNAUTHORIZED
).
build
();
return
ResponseEntity
.
ok
().
body
(
mapper
.
map
(
service
.
update
(
id
,
mapper
.
map
(
recipeInputDto
))));
Recipe
recipeToUpdate
=
mapper
.
map
(
recipeInputDto
);
recipeToUpdate
.
setPicture
(
pictureUrl
);
Recipe
updatedRecipe
=
service
.
update
(
id
,
recipeToUpdate
);
return
ResponseEntity
.
ok
().
body
(
mapper
.
map
(
updatedRecipe
));
}
@PatchMapping
(
"/{id}/favorite"
)
...
...
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