Esempio n. 1
0
        public async Task <ActionResult> Edit(NewProductModel product, List <HttpPostedFileBase> files)
        {
            var _data = await data.Products.FindAsync(product.Id);

            _data.ProductName = product.Id;
            _data.Amount      = product.Amount;
            _data.Description = product.Description;
            _data.ProductName = product.Name;

            //many to many insert mapping Categories
            List <string> tmpCategoryInt = product.Categories
                                           .Where(i => i.IsSelected).Select(i => i.Id).ToList();

            var tmpCategory = await(from p in data.ProductCategories
                                    where tmpCategoryInt.Contains(p.Id.ToString())
                                    select p).ToListAsync();


            foreach (var x in data.ProductCategories)
            {
                if (!tmpCategory.Contains(x))
                {
                    _data.ProductCategories.Remove(x);
                }
                else
                {
                    if (!_data.ProductCategories.Contains(x))
                    {
                        _data.ProductCategories.Add(x);
                    }
                }
            }


            foreach (var o in tmpCategory)
            {
                if (!_data.ProductCategories.Contains(o) && tmpCategoryInt.Contains(o.Id.ToString()))
                {
                    _data.ProductCategories.Add(o);
                }
                else if (_data.ProductCategories.Contains(o) && !tmpCategoryInt.Contains(o.Id.ToString()))
                {
                }
            }


            //many to many insert mapping Genders
            List <string> tmpGenderName = product.Genders
                                          .Where(i => i.IsSelected).Select(i => i.Id).ToList();


            foreach (var x in common.GetGenders())
            {
                if (!tmpGenderName.Contains(x) && _data.ProductsInGenders.Where(i => i.Gender == x).Any())
                {
                    _data.ProductsInGenders.Remove(_data.ProductsInGenders.Where(i => i.Gender == x).Single());
                }
                else if (tmpGenderName.Contains(x) && !_data.ProductsInGenders.Where(i => i.Gender == x).Any())
                {
                    _data.ProductsInGenders.Add(new ProductsInGender()
                    {
                        Gender = x, ProductId = product.Id
                    });
                }
            }

            //Product details
            foreach (var x in _data.ProductDetails)
            {
                x.ProductColorId = product.ColordId;
                x.Length         = product.Lenght;
                x.Width          = product.Width;
                x.Height         = product.Height;
                x.Weight         = product.Weight;
            }

            //Add new images
            if (files.Count() >= 1 && files[0] != null)
            {
                foreach (var x in files)
                {
                    var id = Guid.NewGuid().ToString();

                    NewIMageModel img = new Models.NewIMageModel()
                    {
                        Id            = id,
                        FileExtention = x.ContentType,
                        File          = x,
                    };

                    var res = await azureBlob.UploadImageAsync(img);


                    ProductDetailImage dbProductImage = new ProductDetailImage()
                    {
                        Id = img.Id,
                        ProductDetailsId = _data.ProductDetails.First().Id,
                        ImageUrl         = res.URL,
                        FileName         = res.FileName
                    };


                    data.ProductDetailImages.Add(dbProductImage);
                }
            }

            //remove images
            if (product.ForDeleteImages != null)
            {
                string[] imgs = product.ForDeleteImages.Split(',');


                foreach (var x in imgs)
                {
                    var y = await data.ProductDetailImages.FindAsync(x);

                    data.ProductDetailImages.Remove(y);
                }

                await azureBlob.DeleteBlobs(product.ForDeleteImages.Split(','));
            }


            data.Entry(_data).State = EntityState.Modified;
            await data.SaveChangesAsync();

            return(RedirectToAction("Products"));
        }