Пример #1
0
        private HomePageViewModel BuildHomePageViewModel(string searchTerm, int?parentCategoryId, int?pageNumber)
        {
            HomePageViewModel viewModel = new HomePageViewModel();

            viewModel.RootCategories = _productCategoriesBL.GetCategories(null).Select(productCategory => new HorizontalCategoryItemViewModel
            {
                ProductCategory = productCategory,
                IsSelected      = false
            }).ToList();

            viewModel.SelectedCategory = null;
            ProductsFilter productsFilter = new ProductsFilter
            {
                Text     = searchTerm,
                Publish  = true,
                ParentId = parentCategoryId
            };

            productsFilter.PaginationFilter.PageNumber = pageNumber == null ? 1 : pageNumber.Value;
            productsFilter.PaginationFilter.PageSize   = ApplicationSettings.Instance.AppSettings.DefaultPageSize;

            PagedProductListResult pagedProductListResult = _productsBL.Get(productsFilter);

            viewModel.Products = pagedProductListResult.Entities;
            viewModel.PaginationFilterViewModel = new HomePagePaginationFilterViewModel
            {
                PageNumber       = productsFilter.PaginationFilter.PageNumber,
                TotalRecords     = pagedProductListResult.TotalRecords,
                PageSize         = productsFilter.PaginationFilter.PageSize,
                SearchTerm       = searchTerm,
                ParentCategoryId = parentCategoryId
            };

            if (parentCategoryId.HasValue)
            {
                viewModel.ParentCategories = _productCategoriesBL.GetParentCategories(parentCategoryId.Value);
                viewModel.SelectedCategory = _productCategoriesBL.GetById(parentCategoryId.Value);
                viewModel.ParentCategories.Add(viewModel.SelectedCategory);
                viewModel.ChildCategories = _productCategoriesBL.SearchByFilter(new ProductCategoriesFilter
                {
                    ParentId       = parentCategoryId,
                    IncludeDeleted = false,
                    Publish        = true
                }).Entities;
            }
            else
            {
                viewModel.ParentCategories = new List <ProductCategory>();
                viewModel.ChildCategories  = new List <ProductCategory>();
            }

            return(viewModel);
        }
Пример #2
0
        public ActionResult Save(ProductCategoryCreateOrEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (_productCategoriesBL.GetCategories(model.ParentId).Any(productCategory => productCategory.Name == model.Name && productCategory.Id != model.Id))
                {
                    ModelState.AddModelError("Name", "Така категорія вже існує");
                }

                if (ModelState.IsValid)
                {
                    ProductCategory productCategory;

                    if (model.Id.HasValue)
                    {
                        productCategory             = _productCategoriesBL.GetById(model.Id.Value);
                        productCategory.Name        = model.Name;
                        productCategory.Description = model.Description;

                        if (model.PostedPhoto != null)
                        {
                            productCategory.Photo = _photosBL.UpdateOrAdd(productCategory.Photo, model.PostedPhoto);
                        }

                        _productCategoriesBL.Update(productCategory);

                        if (productCategory.Publish != model.Publish)
                        {
                            _productCategoriesBL.SetPublish(productCategory.Id, model.Publish);
                        }
                    }
                    else
                    {
                        productCategory = new ProductCategory
                        {
                            ParentId    = model.ParentId,
                            Name        = model.Name,
                            Description = model.Description,
                            Publish     = model.Publish,
                        };

                        if (model.PostedPhoto != null)
                        {
                            productCategory.Photo = _photosBL.UpdateOrAdd(null, model.PostedPhoto);
                        }

                        _productCategoriesBL.Create(productCategory);
                    }

                    ProductCategoriesFilterViewModel filterViewModel = new ProductCategoriesFilterViewModel();
                    filterViewModel.ParentCategory.Id = model.ParentId;

                    if (model.ParentId.HasValue)
                    {
                        filterViewModel.ParentCategory.Name = _productCategoriesBL.GetById(model.ParentId.Value).Name;
                    }

                    TempData[Constants.TempDataKeys.PRODUCT_CATEGORIES_PARENT_ID] = model.ParentId;
                    return(RedirectToAction("Index"));
                }
            }

            return(View("Edit", model));
        }
Пример #3
0
 public IList <ProductCategory> GetRootCategories()
 {
     return(_productCategoriesBL.GetCategories(null));
 }