예제 #1
0
        public ActionResult Create(ProductDTO productdto, FormCollection collection)
        {
            try
            {
                using (var uow = _unitOfWorkFactory.UnitOfWork)
                {
                    productdto.product.Category = _categoryRepository.GetById(Int32.Parse(collection["CategoryId"]));
                    HttpPostedFileBase file = Request.Files["ProductImage"];
                    // Verify that the user selected a file
                    if (file != null && file.ContentLength > 0)
                    {
                        if (file.ContentType == "image/jpeg" ||
                            file.ContentType == "image/jpg" ||
                            file.ContentType == "image/png")
                        {
                            // extract only the fielname
                            var fileExtension = Path.GetExtension(file.FileName);
                            // store the file inside ~/App_Data/uploads folder
                            var path = "pics/" + productdto.product.UPC + fileExtension;
                            string dirPath = System.Web.HttpContext.Current.Server.MapPath("~/") + path;
                            file.SaveAs(dirPath);
                            productdto.product.ImagePath = path;
                        }

                    }

                    productdto.product.LastUpdated = DateTime.Now;

                    string[] cnames = collection["cform[cname][]"].Split(',');
                    string[] cpweights = collection["cform[pweight][]"].Split(',');
                    string[] cType = collection["cform[Type][]"].Split(',');
                    string[] cclassifications = collection["cform[classification][]"].Split(',');

                    for (int i = 0; i < cnames.Length; i++)
                    {
                        Constituent constituent = _constituentRepository.Query.Where(x => x.ConstituentName.Equals(cnames[i])).FirstOrDefault();
                        if (constituent == null)
                        {
                            constituent = new Constituent
                            {
                                ConstituentName = cnames[i]
                            };
                        };

                        var newPhasC = new ProductHasConstituent
                        {
                            Product = productdto.product,
                            PartWeight = Double.Parse(cpweights[i]),
                            Constituent = constituent,

                        };

                        var newLocatedIn = new LocatedIn
                        {
                            Constituent = constituent,
                            Region = _regionRepository.Query.Where(x => x.RegionName == productdto.region.RegionName).FirstOrDefault(),
                            Classification = cclassifications[i],
                        };

                        _productRepository.SaveOrUpdate(productdto.product);
                        _constituentRepository.SaveOrUpdate(constituent);
                        _productHasConstituentRepository.SaveOrUpdate(newPhasC);
                        _locatedInRepository.SaveOrUpdate(newLocatedIn);
                    }

                    uow.SaveChanges();
                    return RedirectToAction("Index");

                }

            }
            catch
            {
                return View();
            }
        }
예제 #2
0
        // GET: Product/Edit/5
        public ActionResult Edit(int id)
        {
            var Categories = _categoryRepository.GetAll().ToList().Select(x => new SelectListItem
            {
                Value = x.Id.ToString(),
                Text = x.CategoryName
            });
            ViewData["Category"] = Categories.OrderBy(x => x.Text);
            var productdb = _productRepository.GetById(id);
            var productHasConstituents = _productHasConstituentRepository.Query.Where(x => x.Product == productdb).ToList();
            var constituentsdb = new List<Constituent>();
            foreach (var pHc in productHasConstituents)
            {
                constituentsdb.Add(_constituentRepository.GetById(pHc.Constituent.Id));
            }

            var productDto = new ProductDTO
            {
                product = productdb,
                constituents = constituentsdb,
                pHasCs = productHasConstituents
            };
            return View(productDto);
        }
예제 #3
0
        public ActionResult Edit(ProductDTO productdto, FormCollection collection)
        {
            try
            {
                using (var uow = _unitOfWorkFactory.UnitOfWork)
                {
                    productdto.product.Category = _categoryRepository.GetById(Int32.Parse(collection["CategoryId"]));
                    HttpPostedFileBase file = Request.Files["ProductImage"];
                    var productdb = _productRepository.GetById(productdto.product.Id);
                    // Verify that the user selected a file
                    if (file != null && file.ContentLength > 0)
                    {
                        if (file.ContentType == "image/jpeg" ||
                            file.ContentType == "image/jpg" ||
                            file.ContentType == "image/png")
                        {
                            // extract only the fielname
                            var fileExtension = Path.GetExtension(file.FileName);
                            // store the file inside ~/App_Data/uploads folder
                            var path = "pics/" + productdto.product.UPC + fileExtension;
                            string dirPath = System.Web.HttpContext.Current.Server.MapPath("~/") + path;
                            if (System.IO.File.Exists(dirPath))
                            {
                                System.IO.File.Delete(dirPath);
                                file.SaveAs(path);
                            }
                            else
                            {
                                file.SaveAs(dirPath);
                            }

                            productdto.product.ImagePath = path;
                        }
                        productdto.product.LastUpdated = DateTime.Now;
                    }
                    else if (productdb.ImagePath != null)
                    {
                        productdto.product.ImagePath = productdb.ImagePath;
                    }

                    AutoMapper.Mapper.Map(productdto.product, productdb);
                    _productRepository.SaveOrUpdate(productdb);

                    uow.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
예제 #4
0
        public ActionResult Details(int id)
        {
            var productHasconstituents = _productHasConstituentRepository.Query.Where(x => x.Product.Id == id).ToList();
            var region = _regionRepository.Query.Where(x => x.RegionName == Session["regionName"].ToString()).FirstOrDefault();
            var locatedIn = new List<LocatedIn> { };

            foreach (var constituent in productHasconstituents)
            {
                var recyclability = _locatedInRepository.Query.Where(x => x.Constituent.Id == constituent.Id && x.Region.Id == region.Id).FirstOrDefault();
                locatedIn.Add(recyclability);
            }

            var productdto = new ProductDTO
            {
                pHasCs = productHasconstituents,
                recyclabilities = locatedIn,
                product = productHasconstituents.FirstOrDefault().Product
            };

            return View(productdto);
        }