예제 #1
0
        public async Task <IActionResult> UpdateProduct(UpdateProductViewModel updateViewModel, IFormFile representativeImage,
                                                        IList <string> keys, IList <string> values, IEnumerable <IFormFile> images)
        {
            SellerView seller = loginPersistence.PersistLogin();

            ViewData[GlobalViewBagKeys.ECommerceService] = eCommerce;
            if (ModelState.IsValid)
            {
                ICollection <string> errors = new List <string>();

                //product attributes
                var attributes = new Dictionary <string, HashSet <string> >();
                for (short i = 0; i < keys.Count; i++)
                {
                    if (!string.IsNullOrEmpty(keys[i]) && !attributes.Any(a => a.Key == keys[i]) && values[i] != null)
                    {
                        HashSet <string> separatedValues = values[i]
                                                           .Split(',', StringSplitOptions.RemoveEmptyEntries)
                                                           .ToHashSet();
                        if (separatedValues.Any())
                        {
                            attributes.Add(keys[i], separatedValues);
                        }
                    }
                }
                updateViewModel.UpdateModel.Attributes = attributes;

                //product images
                if (updateViewModel.UpdateImages)
                {
                    if (images.Any())
                    {
                        var imagesList = new List <FileContent>();
                        foreach (IFormFile image in images)
                        {
                            using (MemoryStream memoryStream = new MemoryStream())
                            {
                                await image.CopyToAsync(memoryStream);

                                imagesList.Add(new FileContent(memoryStream.ToArray(), image.ContentType));
                            }
                        }
                        updateViewModel.UpdateModel.Images = imagesList;
                    }
                }
                else                //keep old images
                {
                    updateViewModel.UpdateModel.Images = eCommerce
                                                         .GetProductImages(int.Parse(seller.Id), updateViewModel.ProductTypeId);
                }

                //product representative image
                if (updateViewModel.UpdateRepresentativeImage)
                {
                    if (representativeImage != null)
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            await representativeImage.CopyToAsync(memoryStream);

                            updateViewModel.UpdateModel.RepresentativeImage = new FileContent(memoryStream.ToArray(), representativeImage.ContentType);
                        }
                    }
                    else
                    {
                        errors.Add("Representative image is required");
                    }
                }
                else                //keep old representative image
                {
                    updateViewModel.UpdateModel.RepresentativeImage = eCommerce.GetProductBy(int.Parse(seller.Id), updateViewModel.ProductTypeId).RepresentativeImage;
                }

                if (errors.Any())
                {
                    return(View(updateViewModel));
                }

                eCommerce.UpdateProduct(int.Parse(seller.Id), updateViewModel.ProductTypeId, updateViewModel.UpdateModel, out errors);

                if (errors.Any())
                {
                    ViewData[GlobalViewBagKeys.Errors] = errors;
                }
                else
                {
                    ICollection <string> messages = new List <string>();
                    messages.Add("Update product succeed");
                    ViewData[GlobalViewBagKeys.Messages] = messages;

                    return(RedirectToAction("Product"));
                }
            }
            return(View(updateViewModel));
        }