示例#1
0
        public PartModel(Part part)
        {
            Id = part.Id;
            Name = part.Name;
            Price = part.Price;
            LeadTime = part.LeadTime;
            StockKeepingUnit = part.StockKeepingUnit;
            Image = part.ImagePath != null ? new Image(part.ImagePath) : new Image();
            CategoryId = part.Category.Id;
            Category = part.Category.Name;

            IsIncompatible = false;
            IsSelected = false;
        }
        public bool UpdateProduct(Product product, Part part)
        {
            var prod = _context.Products.FirstOrDefault(x => x.Id == product.Id);

            if (prod != null)
            {
                prod.Parts.SingleOrDefault(p => p.Id == part.Id).IncompatibleParts.Clear();

                prod = product;
                _context.SaveChanges();
                return true;
            }
            return false;
        }
 public bool UpdateProduct(Product product, Part part)
 {
     return _repository.UpdateProduct(product, part);
 }
示例#4
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 });
        }
        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 });
        }