Exemplo n.º 1
0
        public async Task AddIngredient(Guid recipeId, string name, double quantity, string unitOfMeasurement)
        {
            Ingredient ingredient;

            ProductStore productStoreCheapest;
            int          nrOfProductsNecessary = 0;
            double       cost = 0;

            if (!await _productsRepository.Exists(name))
            {
                productStoreCheapest = null;
            }
            else
            {
                Product product = (await _productsRepository.GetByName(name)).FirstOrDefault();
                productStoreCheapest = await _productStoresRepository.GetCheapestStoreByProduct(product.Id);
            }

            if (productStoreCheapest != null)
            {
                Product product = await _productsRepository.GetById(productStoreCheapest.ProductId);

                double quantityAfterConversion = _productsRepository.ConvertUnits(unitOfMeasurement, product.UnitOfMeasurement, quantity);
                nrOfProductsNecessary = (int)Math.Round(quantityAfterConversion / product.Quantity) + 1;
                cost = nrOfProductsNecessary * productStoreCheapest.Price;
            }

            ingredient = Ingredient.Create(recipeId, productStoreCheapest.Id, name, quantity, cost, unitOfMeasurement, nrOfProductsNecessary);
            await Add(ingredient);
        }