Example #1
0
        public ActionResult AddPart(int id)
        {
            var product = _productService.GetProduct(id);

            var viewModel = new PartViewModel()
            {
                Categories = _productService.GetAllPartCategoriesByProduct(product)
                .Select(c => new SelectListItem() { Value = c.Id.ToString(), Text = c.Name }),
                ExistingParts = product.Parts.Select(p => new PartModel(p)).ToList(),
                ProductId = id
            };

            return View(viewModel);
        }
Example #2
0
        public ActionResult AddPart(PartViewModel model)
        {
            var product = _productService.GetProduct(model.ProductId);

            // Should not be validated if category is not selected from the dropdownlist
            if (model.PartDetails.CategoryId == 0)
            {
                ModelState.Remove("PartDetails.CategoryId");
            }

            if (!ModelState.IsValid)
            {
                var viewModel = new PartViewModel()
                {
                    Categories = _productService.GetAllPartCategoriesByProduct(product)
                    .Select(c => new SelectListItem() { Value = c.Id.ToString(), Text = c.Name }),
                    ExistingParts = product.Parts.Select(p => new PartModel(p)).ToList(),
                    ProductId = product.Id
                };

                return View(model);
            }

            // Create a list of incompatible parts based on choices made in view
            var incompatibleParts = new List<Part>();
            if (model.ExistingParts != null && model.ExistingParts.Count > 0)
            {
                foreach (var item in model.ExistingParts)
                {
                    if (item.IsIncompatible)
                    {
                        incompatibleParts.Add(product.Parts.First(p => p.Id == item.Id));
                    }
                }
            }

            // All parts of the same category should be considered to be incompatible
            var sameCategory = product.Parts.Where(p => p.Category.Id == model.PartDetails.CategoryId).Select(p => p);
            incompatibleParts.AddRange(sameCategory);

            // Get Part Category if already exists, else create new when building Part model
            var partCategory = product.Parts.FirstOrDefault(p => p.Category.Id == model.PartDetails.CategoryId);

            var part = new Part()
            {
                Category = partCategory != null ? partCategory.Category : new PartCategory { Name = model.PartDetails.Category },
                ImagePath = SaveImage(model.PartDetails.Image.ImageUpload),
                LeadTime = model.PartDetails.LeadTime,
                Name = model.PartDetails.Name,
                Price = model.PartDetails.Price,
                StockKeepingUnit = model.PartDetails.StockKeepingUnit,
                IncompatibleParts = incompatibleParts
            };

            product.Parts.Add(part);
            _productService.UpdateProduct(product);

            part = _productService.GetProduct(model.ProductId).Parts.LastOrDefault();

            // Must go through and update each parts list of incompatible parts
            if (model.ExistingParts != null && model.ExistingParts.Count > 0)
            {
                foreach (var item in model.ExistingParts)
                {
                    if (item.IsIncompatible)
                    {
                        var partUpdate = product.Parts.SingleOrDefault(p => p.Id == item.Id);
                        partUpdate.IncompatibleParts.Add(part);
                    }
                }
            }

            return RedirectToAction("ProductPartList", new { id = product.Id });
        }
Example #3
0
        public ActionResult EditPart(int productId, int partId)
        {
            var product = _productService.GetProduct(productId);
            var part = product.Parts.SingleOrDefault(p => p.Id == partId);

            var viewModel = new PartViewModel()
            {
                ExistingParts = product.Parts.Where(p => p.Id != part.Id).Select(p => new PartModel(p) { IsIncompatible = part.IncompatibleParts.Contains(p) }).ToList(),
                ProductId = productId,
                PartDetails = new PartModel(part)
            };

            return View(viewModel);
        }
Example #4
0
        public ActionResult EditPart(PartViewModel model)
        {
            var product = _productService.GetProduct(model.ProductId);

            if (model.PartDetails.CategoryId == 0)
            {
                ModelState.Remove("PartDetails.CategoryId");
            }

            if (!ModelState.IsValid)
            {
                var viewModel = new PartViewModel()
                {
                    Categories = _productService.GetAllPartCategoriesByProduct(product)
                    .Select(c => new SelectListItem() { Value = c.Id.ToString(), Text = c.Name }),
                    ExistingParts = product.Parts.Select(p => new PartModel(p)).ToList(),
                    ProductId = product.Id
                };

                return View(model);
            }

            // Create a list of incompatible parts based on choices made in view
            var incompatibleParts = new List<Part>();

            if (model.ExistingParts != null && model.ExistingParts.Count > 0)
            {
                foreach (var item in model.ExistingParts)
                {
                    if (item.IsIncompatible)
                    {
                        incompatibleParts.Add(product.Parts.First(p => p.Id == item.Id));
                    }
                }
            }

            // Clear and Update list of incompatible parts and add same category parts to list
            var part = product.Parts.SingleOrDefault(m => m.Id == model.PartDetails.Id);
            _productService.UpdateProduct(product, part);
            var sameCategory = product.Parts.Where(p => (p.Category.Id == part.Category.Id) && (p.Id != part.Id)).Select(p => p);
            incompatibleParts.AddRange(sameCategory);

            part.ImagePath = SaveImage(model.PartDetails.Image.ImageUpload);
            part.LeadTime = model.PartDetails.LeadTime;
            part.Name = model.PartDetails.Name;
            part.Price = model.PartDetails.Price;
            part.StockKeepingUnit = model.PartDetails.StockKeepingUnit;
            part.IncompatibleParts = incompatibleParts;

            // Go through each incompatible part and add this part to its list
            foreach (var p in part.IncompatibleParts)
            {
                p.IncompatibleParts.Add(part);
            }

            _productService.UpdateProduct(product);

            return RedirectToAction("ProductPartList", new { id = product.Id });
        }
        public ActionResult AddPart(PartViewModel model)
        {
            var path="";
            var fullPath= "N/A";
            var product = _productService.GetProduct(model.ProductId);

            var partCategory = product.Parts.FirstOrDefault(p => p.Category.Id == model.PartDetails.CategoryId);

            var incompatibleParts = new List<Part>();

            if(model.PartDetails.Image.PartImageUpload !=null)
            {
                var fileName = Path.GetFileName(model.PartDetails.Image.PartImageUpload.FileName);
                path = Url.Content(Path.Combine(Server.MapPath("~/Content/Images"), fileName));
                model.PartDetails.Image.PartImageUpload.SaveAs(path);
                fullPath = @"~/Content/Images/" + fileName;
            }

            if(model.ExistingParts != null && model.ExistingParts.Count > 0)
            {
                foreach(var item in model.ExistingParts)
                {
                    if(item.IsIncompatible)
                    {
                        incompatibleParts.Add(product.Parts.First(p => p.Id == item.Id));
                    }
                }
            }

            var sameCategory = product.Parts.Where(p => p.Category.Id == model.PartDetails.CategoryId).Select(p => p);

            incompatibleParts.AddRange(sameCategory);

            var part = new Part()
            {
                Category = partCategory != null ? partCategory.Category : new PartCategory { Name = model.PartDetails.Category },
                ImagePath = fullPath,
                LeadTime = model.PartDetails.LeadTime,
                Name = model.PartDetails.Name,
                Price = model.PartDetails.Price,
                StockKeepingUnit = model.PartDetails.StockKeepingUnit,
                IncompatibleParts = incompatibleParts
            };

            product.Parts.Add(part);
            _productService.UpdateProduct(product);

            part = _productService.GetProduct(model.ProductId).Parts.LastOrDefault();

            if(model.ExistingParts != null && model.ExistingParts.Count > 0)
            {
                foreach(var item in model.ExistingParts)
                {
                    if(item.IsIncompatible)
                    {
                        var partUpdate = product.Parts.SingleOrDefault(p => p.Id == item.Id);
                        partUpdate.IncompatibleParts.Add(part);
                    }
                }
            }

            return RedirectToAction("ProductPartList", new { id = product.Id });
        }