コード例 #1
0
        public async Task <IActionResult> Index(RegisterProductViewModel registerModel, 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);
                        }
                    }
                }
                registerModel.AddModel.Attributes = attributes;

                //product images
                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));
                        }
                    }
                    registerModel.AddModel.Images = imagesList;
                }

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

                        registerModel.AddModel.RepresentativeImage = new FileContent(memoryStream.ToArray(), representativeImage.ContentType);
                    }
                }
                else
                {
                    errors.Add("Representative image is required");
                }

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

                eCommerce.RegisterProduct(int.Parse(seller.Id), registerModel.AddModel, out errors);

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

                    return(RedirectToAction("Product", "Seller"));
                }
            }
            return(View(registerModel));
        }