Пример #1
0
        public ActionResult Index(string searchTerm, int?parentCategoryId, int?pageNumber)
        {
            Product product = _productsBL.GetByCode(searchTerm);

            if (product != null)
            {
                return(RedirectToAction("Details", new
                {
                    productId = product.Id,
                    categoryId = product.Categories.First().Id
                }));
            }

            HomePageViewModel viewModel = BuildHomePageViewModel(searchTerm, parentCategoryId, pageNumber);

            return(View(viewModel));
        }
Пример #2
0
        public ActionResult Save(ProductCreateOrEditViewModel model)
        {
            if (!model.CategorySelectorViewModel.Id.HasValue)
            {
                ModelState.AddModelError("CategorySelectorViewModel.Name", "Вказана неіснуюча категорія");
            }

            Product alreadyExistedProduct = _productsBL.GetByName(model.Name);

            if (alreadyExistedProduct != null && model.Id.HasValue && model.Id.Value != alreadyExistedProduct.Id)
            {
                if (!alreadyExistedProduct.IsDeleted)
                {
                    ModelState.AddModelError("Name", "Продукт з таким іменем вже існує");
                }
            }

            if (ModelState.IsValid)
            {
                if (alreadyExistedProduct != null && model.Id.HasValue && model.Id.Value != alreadyExistedProduct.Id)
                {
                    if (alreadyExistedProduct.IsDeleted)
                    {
                        _productsBL.DeletePermanently(alreadyExistedProduct.Id);
                    }
                }

                Product target = GetProductToBeSaved(model);

                Product byCode = _productsBL.GetByCode(model.Code);

                if (byCode != null && byCode.Id != target.Id)
                {
                    ModelState.AddModelError("Code", "Введений код належить іншому продукту");
                    return(View("Edit", model));
                }

                target.Categories.Clear();
                target.Categories.Add(_productCategoriesBL.GetById(model.CategorySelectorViewModel.Id.Value));

                target.Name             = model.Name;
                target.Description      = model.Description;
                target.ShortDescription = model.ShortDescription;
                decimal price = decimal.Parse(model.Price);
                target.Price   = price;
                target.Publish = model.Publish;
                target.Code    = model.Code;
                target.PriceInTheMainCurrency = _productsBL.CalculatePriceInTheMainCurrency(model.CurrencyIdOfThePrice.Value, price);
                target.CurrencyIdOfThePrice   = model.CurrencyIdOfThePrice.Value;

                ProcessBrand(model, target);
                ProcessCountry(model, target);
                ProcessPhotos(model, target);
                ProcessMetadata(model.MetaDataViewModel, target);

                if (target.Id == 0)
                {
                    _productsBL.Create(target);
                }
                else
                {
                    _productsBL.Update(target);
                }

                if (ModelState.IsValid)
                {
                    TempData[Constants.TempDataKeys.PRODUCTS_FILTER_VIEW_MODEL] = new ProductsFilterViewModel(model.CategorySelectorViewModel.Id.Value);
                    return(RedirectToAction("Index"));
                }
            }
            if (model.CategorySelectorViewModel.Id.HasValue)
            {
                model.CategorySelectorViewModel.ParentCategories =
                    _productCategoriesBL.GetParentCategories(model.CategorySelectorViewModel.Id.Value).Select(parentCategory => parentCategory.Name).ToArray();
            }

            return(View("Edit", model));
        }