feat(recipe/application): added saving userId when CreateRecipe in service

parent 5f9e08b3
package com.example.apprecetas.recipe.application.impl; package com.example.apprecetas.recipe.application.impl;
import com.example.apprecetas.exception.EntityNotFoundException;
import com.example.apprecetas.recipe.application.CreateRecipeUseCase; import com.example.apprecetas.recipe.application.CreateRecipeUseCase;
import com.example.apprecetas.recipe.domain.entity.Recipe; import com.example.apprecetas.recipe.domain.entity.Recipe;
import com.example.apprecetas.recipe.domain.repository.CreateRecipeRepository; import com.example.apprecetas.recipe.domain.repository.CreateRecipeRepository;
import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper; import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper;
import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument; import com.example.apprecetas.recipe.infrastructure.repository.mongodb.RecipeDocument;
import com.example.apprecetas.user.domain.repository.ReadUserRepository;
import com.example.apprecetas.user.infrastructure.repository.mongodb.UserDocument;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
public class CreateRecipeUseCaseImpl implements CreateRecipeUseCase { public class CreateRecipeUseCaseImpl implements CreateRecipeUseCase {
private final CreateRecipeRepository repository; private final CreateRecipeRepository createRecipeRepository;
private final ReadUserRepository readUserRepository;
private final RecipeMapper mapper = Mappers.getMapper(RecipeMapper.class); private final RecipeMapper mapper = Mappers.getMapper(RecipeMapper.class);
...@@ -21,14 +27,17 @@ public class CreateRecipeUseCaseImpl implements CreateRecipeUseCase { ...@@ -21,14 +27,17 @@ public class CreateRecipeUseCaseImpl implements CreateRecipeUseCase {
public Recipe create(Recipe recipe) { public Recipe create(Recipe recipe) {
RecipeDocument recipeDocument = mapper.mapDocument(recipe); RecipeDocument recipeDocument = mapper.mapDocument(recipe);
/*recipeDocument.getSteps().forEach(step -> { // Get username from token
step.setRecipe(recipeDocument); String username = SecurityContextHolder.getContext().getAuthentication().getName();
});
recipeDocument.getIngredients().forEach(ingredient -> { // Search user
ingredient.setRecipe(recipeDocument); UserDocument user = readUserRepository.readByUsername(username)
});*/ .orElseThrow(() -> new EntityNotFoundException("Usuario con username " + username + " no encontrado"));
// Put userId to recipe
recipeDocument.setUserId(user.getId());
RecipeDocument savedRecipe = repository.create(recipeDocument); RecipeDocument savedRecipe = createRecipeRepository.create(recipeDocument);
return mapper.mapDocument(savedRecipe); return mapper.mapDocument(savedRecipe);
} }
} }
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