Beispiel #1
0
        public static CategoryCompositeViewModel ConvertCategoryToViewModel(Category category)
        {
            CategoryCompositeViewModel categoryCompositeViewModel = new CategoryCompositeViewModel()
            {

                CategoryId = category.CategoryId,
                text = category.Name,
                Prefix = category.Prefix,
                type = "category",
                IdParentCategory = category.IdParentCategory,
                IdTranslationName = category.IdTranslationName,
                Image = category.Image,
                IsFinal = category.IsFinal,
                IsFirst = category.IsFirst,
                Name = category.Name,
                Orden = category.Orden,
                Comment = category.Comment,
                Pending = category.Pending,
                Active = category.Active,
                nextsibling = "category",
                Translation = category.Translation
            };
        
            return categoryCompositeViewModel;
        }
        private List<CategoryCompositeViewModel> CreateSubCategories(CategoryCompositeViewModel p, bool withproducts, bool activecategory, int hotelId)
        {
            CategoryRepository categoryRepo = new CategoryRepository(this.Context);

            List<Category> categories = categoryRepo.GetByParentId(p.CategoryId);
            List<CategoryCompositeViewModel> categoriesVm = new List<CategoryCompositeViewModel>();
            categoriesVm.Add(p);
            foreach (Category c in categories)
            {
                CategoryCompositeViewModel categoryCompositeViewModel = Helper.ConvertCategoryToViewModel(c);
                if (p.Children == null)
                    p.Children = new List<ICatalogChildren>();

                if (activecategory)
                {
                    //   categoryCompositeViewModel.IsChecked = c.ActiveHotelCategory.Contains(new ActiveHotelCategory() { IdCategory = c.CategoryId, IdHotel = hotelId, Active = true, Category = c});
                    c.ActiveHotelCategory.ForEach(delegate(ActiveHotelCategory hotelCategory)
                    {
                        if (hotelCategory.IdHotel == hotelId && hotelCategory.Category.IdParentCategory == p.CategoryId)
                        {
                            categoryCompositeViewModel.IsChecked = true;
                        }
                    });

                    categoryCompositeViewModel.ActiveCheckbox = true;
                }

                if (withproducts)
                {
                    categoryCompositeViewModel.Children = new List<ICatalogChildren>();
                    foreach (CategoryProduct cp in c.CategoryProducts)
                    {
                        Product product = productRepo.GetById(cp.IdProduct);
                            ProductCompositeViewModel productViewModel = new ProductCompositeViewModel()
                            {
                                ProductId = product.Id,
                                text = product.Name,
                                ActiveCheckbox = true
                            };

                            categoryCompositeViewModel.Children.Add(productViewModel);

                            product.ActiveHotelProduct.ForEach(delegate(ActiveHotelProduct productHotelAct)
                            {
                                if (productHotelAct.IdHotel == hotelId && cp.IdProduct == productHotelAct.IdProduct )
                                {
                                    productViewModel.IsChecked = true;
                                }
                            });

                  
                    }
                }

                p.Children.Add(categoryCompositeViewModel);
                if (categories != null)
                    CreateSubCategories(categoryCompositeViewModel, withproducts, activecategory, hotelId);

            }

            return categoriesVm;
        }