Пример #1
0
 public static bool Validate(this ProductAddition product)
 {
     return(product != null &&
            !string.IsNullOrEmpty(product.Title) &&
            !string.IsNullOrEmpty(product.ShortDescription) &&
            !string.IsNullOrEmpty(product.LongDescription) &&
            product.Price > 0);
 }
        public IActionResult Index(ProductAddition product)
        {
            if (!product.Validate())
            {
                return(View());
            }

            Product newProduct = new Product
            {
                Title            = product.Title,
                ShortDescription = product.ShortDescription,
                LongDescription  = product.LongDescription,
                Date             = DateTime.Now,
                LastInteraction  = DateTime.Now,
                Price            = product.Price,
                Images           = null,
                State            = Product.States.Available,
                SellerId         = _userRepository.GetUserList.FirstOrDefault(u => u.UserName.Equals(User.Identity.Name)).Id,
                BuyerId          = null,
                Seller           = _userRepository.GetUserList.FirstOrDefault(u => u.UserName.Equals(User.Identity.Name)),
                Buyer            = null
            };

            if (product.Images == null || product.Images.Count < 1)
            {
                newProduct.Images = null;
            }

            else
            {
                List <Image> imageList = new List <Image>();
                foreach (var formFile in product.Images)
                {
                    Image generated;
                    bool  success = _imageConverter.ToDatabaseImage(formFile, out generated);

                    if (success)
                    {
                        generated.Product = newProduct;
                        imageList.Add(generated);
                    }
                }

                newProduct.Images = imageList;
            }

            if (_productRepository.AddProduct(newProduct))
            {
                return(RedirectToAction("Index"));
            }
            return(RedirectToAction("Home", "Error"));
        }