internal IResult <ChileProduct> CreateChileProduct(CreateChileProductParameters parameters, DateTime timeStamp, out List <AdditiveTypeKey> deletedIngredients)
        {
            deletedIngredients = null;
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var chileType = _productUnitOfWork.ChileTypeRepository.FindByKey(parameters.ChileTypeKey);

            if (chileType == null)
            {
                return(new InvalidResult <ChileProduct>(null, string.Format(UserMessages.ChileTypeNotFound, parameters.ChileTypeKey.KeyValue)));
            }

            var productResult = CreateProduct(parameters.Parameters, ProductTypeEnum.Chile);

            if (!productResult.Success)
            {
                return(productResult.ConvertTo((ChileProduct)null));
            }
            var product = productResult.ResultingObject;

            var chileProduct = _productUnitOfWork.ChileProductRepository.Add(new ChileProduct
            {
                Id          = product.Id,
                ChileTypeId = parameters.ChileTypeKey.ChileTypeKey_ChileTypeId,

                ChileState             = parameters.Parameters.ChileState,
                Mesh                   = parameters.Parameters.Mesh,
                IngredientsDescription = parameters.Parameters.IngredientsDescription,
                Product                = product
            });

            var setRangesResult = new SetChileProductAttributeRangesCommand(_productUnitOfWork).Execute(chileProduct, parameters.Parameters, timeStamp, parameters.AttributeRanges);

            if (!setRangesResult.Success)
            {
                return(setRangesResult.ConvertTo <ChileProduct>());
            }

            return(new SetChileProductIngredientsCommand(_productUnitOfWork).Execute(chileProduct, parameters.Parameters, timeStamp, parameters.Ingredients, out deletedIngredients));
        }
Beispiel #2
0
            public void Creates_new_tblProduct_record_as_expected()
            {
                //Arrange
                var chileType = RVCUnitOfWork.ChileTypeRepository.Filter(t => true).FirstOrDefault();

                if (chileType == null)
                {
                    Assert.Inconclusive("No suitable ChileType to test.");
                }

                var additiveTypes = RVCUnitOfWork.AdditiveTypeRepository.Filter(a => true).Take(3).ToList()
                                    .Select((a, n) => new
                {
                    AdditiveType = a,
                    Percentage   = n + 1
                })
                                    .ToList();

                var parameters = new CreateChileProductParameters
                {
                    UserToken              = TestUser.UserName,
                    ChileState             = ChileStateEnum.FinishedGoods,
                    ChileTypeKey           = chileType.ToChileTypeKey(),
                    ProductName            = "Test Chile Product",
                    ProductCode            = "1234",
                    IngredientsDescription = "lotsa stuff - and things as well",
                    Ingredients            = additiveTypes.Select(a => new SetChileProductIngredientParameters
                    {
                        AdditiveTypeKey = a.AdditiveType.ToAdditiveTypeKey(),
                        Percentage      = a.Percentage
                    }),

                    AttributeRanges = new List <ISetAttributeRangeParameters>
                    {
                        new SetAttributeRangeParameters
                        {
                            AttributeNameKey = StaticAttributeNames.Asta.ToAttributeNameKey(),
                            RangeMin         = 1,
                            RangeMax         = 2
                        }
                    }
                };

                //Act
                var result = Service.CreateChileProduct(parameters);

                result.AssertSuccess();
                var resultString = GetKeyFromConsoleString(ConsoleOutput.SynchedTblProduct);

                //Assert
                MockKillSwitch.Verify(k => k.Engage(), Times.Never());

                var prodId = int.Parse(resultString);

                using (var oldContext = new RioAccessSQLEntities())
                {
                    var tblProduct = oldContext.tblProducts.FirstOrDefault(p => p.ProdID == prodId);
                    Assert.AreEqual(parameters.ProductName, tblProduct.Product);
                    Assert.AreEqual(int.Parse(parameters.ProductCode), tblProduct.ProdID);

                    Assert.AreEqual(1, (double)tblProduct.AstaStart, 0.01);
                    Assert.AreEqual(2, (double)tblProduct.AstaEnd, 0.01);
                    Assert.AreEqual(parameters.IngredientsDescription, tblProduct.IngrDesc);

                    var tblIngrs = oldContext.tblProductIngrs.Where(i => i.ProdID == prodId).ToList();
                    foreach (var additive in additiveTypes)
                    {
                        var tblIngr = tblIngrs.FirstOrDefault(i => i.IngrID == additive.AdditiveType.Id);
                        if (tblIngr != null)
                        {
                            Assert.AreEqual(additive.Percentage, (double)tblIngr.Percentage, 0.01);
                        }
                    }
                }
            }