perf(recipe/controller): añadido tratamiento de la imagen en crear y actualizar receta

parent e364cc2f
......@@ -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());
......
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")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment