public async Task <Recipe> Handle(AddRecipeCommand command, CancellationToken cancellationToken) { if (!await ValidateInsertedIndexes(command)) { throw new DomainException("DomainException", "Some product id's does not exist in database."); } // // TODO: violate DRY principle ! Change it! // var allFoodProducts = await _foodProductRepository.GetAllAsync(); List <FoodProductDetails> productsDetails = new List <FoodProductDetails>(); foreach (var item in command.Products) { var foodProductName = allFoodProducts.SingleOrDefault(x => x.FoodProductId == item.FoodProductId).Name; FoodProductDetails fpd = new FoodProductDetails(item.FoodProductId, foodProductName, item.AmountValue, item.IsOptional); productsDetails.Add(fpd); } // TODO: throw Infrastrcutre Exception!! If category doesnt exist - 500 Internal Server Error. var recipeCategory = await _recipeRepository.GetRecipeCategoryByIdAsync(command.RecipeCategory); var recipe = new Recipe( command.Name, command.Description, recipeCategory, productsDetails, command.RequiredTime, command.LevelOfDifficulty); await _recipeRepository.AddRecipeAsync(recipe); await _unitOfWork.CommitAsync(cancellationToken); return(recipe); }
public void BaseSetUp() { recipeCategory = new RecipeCategory("Obiad"); foodProductDetails = new FoodProductDetails(1, new AmountValue(10.0f, Unit.Mililiter)); }