Example #1
0
 public HttpResponseMessage Put(int id, ProductModelUpdateRequest product)
 {
     return Invoke(Request, () =>
     {
         product.ProductID = id;
         var result = _service.UpdateProduct(product);
         var response = Request.CreatePutResponseFor(result);
         return response;
     });
 }
        public void UpdateProduct_LinkingToAnotherProductCategory()
        {
            Product existingProduct;
            ProductCategory existingProductCategory;
            using (var uow = _uow())
            {
                existingProduct = uow.Products.GetAll().FirstOrDefault(p =>p.ProductCategoryID != null);
                existingProductCategory = uow.ProductCategories.GetAll().FirstOrDefault(pc => pc.ProductCategoryID != existingProduct.ProductCategoryID);
            }

            using (new TransactionScope())
            {
                var updateModel = new ProductModelUpdateRequest()
                {
                    ProductID = 680,
                    ListPrice = 10M,
                    StandardCost = 20M,
                    ProductCategoryID = existingProductCategory.ProductCategoryID
                };

                // Act
                var updateResult = _service.UpdateProduct(updateModel);
                Assert.IsTrue(updateResult.Success);

                using (var uow = _uow())
                {
                    var updatedProduct = uow.Products.GetAll().Include(p => p.ProductCategory).FirstOrDefault(p => p.ProductID == updateModel.ProductID);
                    Assert.AreNotEqual(updatedProduct.ProductCategoryID, existingProduct.ProductCategoryID);
                    Assert.AreEqual(existingProductCategory.ProductCategoryID, updatedProduct.ProductCategoryID);
                }

            }
        }
        public void UpdateProduct_ModifyingProductCategoryValue()
        {
            using (new TransactionScope())
            {
                var updateModel = new ProductModelUpdateRequest()
                {
                    ProductID = 680,
                    ListPrice = 10M,
                    StandardCost = 20M,
                    ProductCategory = new ProductCategoryModel { Name = "Nana" },

                };

                // Act
                var updateResult = _service.UpdateProduct(updateModel);
                Assert.IsTrue(updateResult.Success);

                using (var uow = _uow())
                {
                    var updatedProduct = uow.Products.GetAll().Include(p => p.ProductCategory).FirstOrDefault(p => p.ProductID == updateModel.ProductID);
                    Assert.AreEqual(updateModel.ProductCategory.Name, updatedProduct.ProductCategory.Name);
                }
            }
        }
Example #4
0
        public Result UpdateProduct(ProductModelUpdateRequest product)
        {
            var watch = new Stopwatch();
            watch.Start();

            using (new OperationLogger(Log, m => m.Invoke(GetType(), MethodBase.GetCurrentMethod(), Context, String.Format("with product {0}", product.ToJson()))))
            {
                Console.Out.WriteLine("A - " + watch.ElapsedMilliseconds);

                // validates the model using data annotations and the IValidatableObject interface method.
                var results = Validate(product);
                if (results.Any()) return Result.CreateValidationErrors(results.ToArray());

                Console.Out.WriteLine("B - " + watch.ElapsedMilliseconds);

                try
                {
                    using (var uow = UoW())
                    {
                        Console.Out.WriteLine("C - " + watch.ElapsedMilliseconds);

                        var entity = uow.Products.GetAll()
                            .Include(p => p.ProductCategory)
                            .SingleOrDefault(p => p.ProductID == product.ProductID);

                        Console.Out.WriteLine("D - " + watch.ElapsedMilliseconds);

                        if (entity == null)
                        {
                            return Result.CreateNotFound<Product>(product.ProductID);
                        }

                        Mapper.Map(product, entity);
                        entity.ModifiedDate = DateTime.UtcNow;

                        uow.Commit();

                        Console.Out.WriteLine("E - " + watch.ElapsedMilliseconds);

                        Log.Info(m => m.Invoke(GetType(), MethodBase.GetCurrentMethod(), Context, String.Format(" product updated - id = {0}", entity.ProductID)));

                        Console.Out.WriteLine("F - " + watch.ElapsedMilliseconds);
                    }
                }
                catch (ProviderException e)
                {
                    Console.Out.WriteLine("G - " + watch.ElapsedMilliseconds);
                    Log.Exception(m => m.Invoke(GetType(), MethodBase.GetCurrentMethod(), Context, e.Errors.ToJson()), e);
                    Console.Out.WriteLine("H - " + watch.ElapsedMilliseconds);
                    return ResultExtensions.Create(e, (int) ServiceMessages.Codes.ProviderError);
                }

                Console.Out.WriteLine("I - " + watch.ElapsedMilliseconds);

                Console.Out.WriteLine("J - " + watch.ElapsedMilliseconds);
                return Result.CreateEmpty();
            }
        }