feat(recipe/controller): implementado guardado de recetas correctamente junto con la imagen

parent a7af2bad
......@@ -2,16 +2,20 @@ package com.example.apprecetas.recipe.infrastructure.controller;
import com.example.apprecetas.exception.UnprocessableEntityException;
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.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;
import org.springframework.http.ResponseEntity;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -25,18 +29,39 @@ public class CreateRecipeController {
private final CreateRecipeUseCase service;
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
public ResponseEntity<RecipeOutputDto> create(@RequestBody @Valid RecipeInputDto recipeInputDto, BindingResult result) {
@PostMapping(consumes = {"multipart/form-data"})
public ResponseEntity<RecipeOutputDto> create(@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);
}
URI location = URI.create("recipe");
return ResponseEntity.created(location).body(mapper.map(service.create(mapper.map(recipeInputDto))));
// Sube la imagen y obtiene la URL
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));
}
}
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