예제 #1
0
        /// <summary>
        /// Try to add a new Product to the database
        /// </summary>
        /// <param name="product"></param>
        /// <returns>False if Product does not meet business rules.  Errors are contained in the Errors collection of the Product</returns>
        public bool TryAdd(Product product)
        {
            if (IsValid(product))
            {
                repository.Add(product);
                return true;
            }

            return false;
        }
예제 #2
0
        public void TryAdd_should_populate_Product_error_collection_when_Sku_exists()
        {
            repository.Setup(x => x.Count(It.IsAny<DetachedCriteria>())).Returns(1);
            repository.Setup(x => x.Add(It.IsAny<Product>())).Verifiable();
            var product = new Product();

            var result = service.TryAdd(product);

            Assert.That(result, Is.False);
            Assert.That(product.Errors.Any(), Is.True);
            repository.Verify(x=>x.Add(It.IsAny<Product>()), Times.Never());
        }
예제 #3
0
        public void FromEntity(Product productEntity)
        {
            Id = productEntity.Id;
            Name = productEntity.Name;
            Description = productEntity.Description;
            Sku = productEntity.Sku;
            if (productEntity.Prices != null)
            {
                Prices = productEntity.Prices.Select(x => new EditPrice { Id = x.Id, Value = x.Value, IsTaxIncluded = x.IsTaxIncluded, TaxRate = x.TaxRate, Currency = x.Currency }).ToList(); // need to figure out automapper asap
            }

            IsDeleted = productEntity.IsDeleted;
            if (productEntity.Categories != null)
            {
                Categories =
                    productEntity.Categories.Select(
                        x => new EditCategory { Id = x.Id, Name = x.Name, Description = x.Description }).ToList();
            }

            Quantity = productEntity.Quantity;
        }
예제 #4
0
        public ActionResult Add(EditProduct product)
        {
            var productEntity = new Product();
            if (ModelState.IsValid) // validate inputs first
            {
                productEntity = product.ToEntity();
                if (!productService.TryAdd(productEntity)) // validate business logic
                {
                    AddModelStateErrors(productEntity.Errors);
                }
            }

            if (!ModelState.IsValid)
            {
                ViewBag.Title = Localisation.Admin.PageContent.Add;
                ViewBag.Product = Localisation.Admin.PageContent.Product;
                ViewBag.ViewType = "Add";
                return View("Edit", product);
            }
            return RedirectToAction("Edit", new { id = productEntity.Id });
        }
예제 #5
0
        /// <summary>
        /// Business Rule: Sku's must be unique
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        private bool SkuExists(Product product)
        {
            var searchCriteria = DetachedCriteria.For(typeof(Product))
                                                 .Add(!Restrictions.Eq("Id", product.Id))
                                                 .Add(Restrictions.Eq("Sku", product.Sku));

            var productsWithSameSkuAndDifferentIds = repository.Count(searchCriteria);
            return productsWithSameSkuAndDifferentIds > 0;
        }
예제 #6
0
        /// <summary>
        /// Ensure the product is validated against business rules
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        private bool IsValid(Product product)
        {
            if (SkuExists(product))
            {
                product.AddError(new ErrorInfo("Sku", Localisation.ViewModels.EditProduct.SkuExists));
            }

            return !product.Errors.Any();
        }
예제 #7
0
 /// <summary>
 /// Try to update the Product
 /// </summary>
 /// <param name="product"></param>
 /// <returns>False if Product does not meet business rules.  Errors are contained in the Errors Collection of the Product</returns>
 public bool TryUpdate(Product product)
 {
     if (IsValid(product))
     {
         repository.Update(product);
         return true;
     }
     return false;
 }
예제 #8
0
        public Product ToEntity()
        {
            var product = new Product
                              {
                                  Id = Id,
                                  Description = Description,
                                  Name = Name,
                                  Quantity = Quantity,
                                  Sku = Sku
                              };

            return product;
        }
예제 #9
0
        public void TryUpdate_should_invoke_repository_update_when_Sku_does_not_exist()
        {
            repository.Setup(x => x.Count(It.IsAny<DetachedCriteria>())).Returns(0).Verifiable();
            repository.Setup(x => x.Update(It.IsAny<Product>())).Verifiable();

            var product = new Product();
            var result = service.TryUpdate(product);

            Assert.That(result, Is.True);
            Assert.That(product.Errors, Is.Empty);
            repository.Verify(x => x.Count(It.IsAny<DetachedCriteria>()), Times.AtLeastOnce());
            repository.Verify(x => x.Update(It.IsAny<Product>()), Times.AtLeastOnce());
        }
예제 #10
0
        public void ThisMethod_should_create_data_for_ChopShop_when_it_is_executed()
        {
            using (var tx = session.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                // Create Categories
                var engines = new Category { Name = "Engines", Description = "All the engines you can imagine" };
                var tyres = new Category { Name = "Tyres", Description = "We have round tyres and square tyres!" };
                var parts = new Category { Name = "Body Parts", Description = "Doors, Wings and all sorts of Panels" };
                var cars = new Category { Name = "Cars", Description = "All shapes, most sizes, lots of prices" };
                var lorries = new Category { Name = "Lorries", Description = "Red ones, Yellow ones and even Blue ones" };
                var bicycles = new Category
                                   {
                                       Name = "Bicycles",
                                       Description = "Some of these have two wheels - some have only one"
                                   };
                var bicycleParts = new Category { Name = "Bicycle Parts", Description = "Derailleurs, Gears, Pedals" };

                engines.Parent = cars;
                tyres.Parent = cars;
                bicycleParts.Parent = bicycles;
                parts.Parent = lorries;

                var categories = new List<Category>
                                     {
                                         engines,
                                         tyres,
                                         parts,
                                         cars,
                                         lorries,
                                         bicycles,
                                         bicycleParts
                                     };
                foreach (var category in categories)
                {
                    session.Save(category);
                }

                // Create Products
                var bluepedal = new Product
                                    {
                                        Name = "Blue Pedal",
                                        Description = "This pedal is blue",
                                        Quantity = 10,
                                        Sku = "BluePedal0001"
                                    };
                var redpedal = new Product
                                   {
                                       Name = "Red Pedal",
                                       Description = "This pedal is red",
                                       Quantity = 3,
                                       Sku = "RedPedal0002"
                                   };
                var bigEngine = new Product
                                    {
                                        Name = "3.0 L V12",
                                        Description = "This is a fast one",
                                        Sku = "Eng00v12",
                                        Quantity = 2
                                    };

                var products = new List<Product> { bluepedal, redpedal, bigEngine };
                products.AddRange(GetProducts());
                foreach (var product in products)
                {
                    session.Save(product);
                }

                // Associate Categories To Products
                bicycleParts.Products = new List<Product> { bluepedal, redpedal };
                engines.Products = new List<Product> { bigEngine };

                session.SaveOrUpdate(bicycleParts);
                session.SaveOrUpdate(engines);

                var adminUser = new AdminUser { Email = "*****@*****.**", Name = "admin", Password = "******" };
                session.SaveOrUpdate(adminUser);

                tx.Commit();
            }
        }