Exemplo n.º 1
0
        public virtual async Task <ActionResult> Create(AddProductViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var product = new Product
                {
                    Name = viewModel.Name,
                    ApplyCategoryDiscount = viewModel.ApplyCategoryDiscount,
                    CategoryId            = viewModel.CategoryId,
                    Deleted                  = viewModel.Deleted,
                    IsFreeShipping           = viewModel.IsFreeShipping,
                    Description              = viewModel.Description,
                    DiscountPercent          = viewModel.DiscountPercent,
                    MetaDescription          = viewModel.MetaDescription,
                    MetaKeyWords             = viewModel.MetaKeyWords,
                    Price                    = viewModel.Price,
                    NotificationStockMinimum = viewModel.NotificationStockMinimum,
                    PrincipleImagePath       = viewModel.PrincipleImagePath,
                    Ratio                    = viewModel.Ratio,
                    SellCount                = 0,
                    ViewCount                = 0,
                    Reserve                  = 0,
                    Stock                    = viewModel.Stock,
                    Type      = viewModel.Type,
                    Rate      = new Rate(),
                    AddedDate = DateTime.Now
                };
                _productService.Insert(product);
                var attributes = _attributeService.GetAttributesByCategoryId(viewModel.CategoryId);
                _valueService.AddCategoryAttributesToProduct(attributes, product.Id);
                await _unitOfWork.SaveChangesAsync();

                LuceneProducts.AddIndex(new ProductLuceneViewModel
                {
                    Name        = product.Name,
                    Id          = product.Id,
                    ImagePath   = product.PrincipleImagePath,
                    Description = product.Description
                });
                CacheManager.InvalidateChildActionsCache();
                return(RedirectToAction(MVC.Admin.Product.ActionNames.Create, MVC.Admin.Product.Name));
            }
            PopulateCategoriesDropDownList(viewModel.CategoryId);
            if (!ModelState.IsValidField("CategoryId"))
            {
                ModelState.AddModelError("", "گروه محصول را مشخص  کنید");
            }
            return(View(viewModel));
        }