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

parent 5f9e08b3
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.domain.entity.Recipe;
import com.example.apprecetas.recipe.domain.repository.CreateRecipeRepository;
import com.example.apprecetas.recipe.infrastructure.mapper.RecipeMapper;
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 org.mapstruct.factory.Mappers;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
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);
......@@ -21,14 +27,17 @@ public class CreateRecipeUseCaseImpl implements CreateRecipeUseCase {
public Recipe create(Recipe recipe) {
RecipeDocument recipeDocument = mapper.mapDocument(recipe);
/*recipeDocument.getSteps().forEach(step -> {
step.setRecipe(recipeDocument);
});
recipeDocument.getIngredients().forEach(ingredient -> {
ingredient.setRecipe(recipeDocument);
});*/
// Get username from token
String username = SecurityContextHolder.getContext().getAuthentication().getName();
// Search user
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);
}
}
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