public void CanUpdateIncomeModelPropertiesByTheirId()
        {
            // Creating simple model
            IncomeMVCModel income = new IncomeMVCModel
            {
                Id = 2,
                Products = new List<IncomeMVCModel.ProductDetailsModel>
                {
                    new IncomeMVCModel.ProductDetailsModel
                    {
                        Product = new ProductMVCModel { Id = 2 },
                        Sizes = new List<IncomeMVCModel.ProductDetailsModel.SizeDetailsModel>
                        {
                            new IncomeMVCModel.ProductDetailsModel.SizeDetailsModel() {Size = new SizeMVCModel {Id = 3 } }
                        }
                    }
                }
            };

            income.UpdateModelPropertiesFromDomainModel(productService, sizeService);

            Assert.AreEqual(productService.GetById(2).Article, income.Products[0].Product.Article);
            Assert.AreEqual(sizeService.GetById(3).Id, income.Products[0].Sizes[0].Size.Id);
        }
Esempio n. 2
0
        public ActionResult Edit(IncomeMVCModel income, string inputSave, Guid? inputDeletingDetailsGuid, string addProductDetails)
        {
            if (income == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            income.UpdateModelPropertiesFromDomainModel(productService, sizeService);

            if (inputDeletingDetailsGuid != null)
            {
                var removingItem = income.Products.Find(p => p.Guid == inputDeletingDetailsGuid);

                if (!income.Products.Remove(removingItem))
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Failed to delete unexisting item");
                }

                ViewData.ModelState.Clear();

                return View(income);
            }

            if (!String.IsNullOrWhiteSpace(addProductDetails))
            {
                // Add new product.

                // Return updated View
            }

            if (!String.IsNullOrWhiteSpace(inputSave) && ModelState.IsValid)
            {

                var incomeDomain = income.ToDomainModel(incomeService, productService, sizeService);

                if (incomeDomain.IsNew())
                {
                    incomeService.Add(income.ToDomainModel(incomeService, productService, sizeService));
                }
                else
                {
                    incomeService.Update(incomeDomain);
                }

                incomeService.Save();

                return RedirectToAction("Index");
            }

            return View(income);
        }