public override void OnActionExecuting(HttpActionContext context) { CategoryRepository categoryRepository = new CategoryRepository(new MyRoomDbContext()); List<Category> categories = categoryRepository.GetByParentId((int)context.ActionArguments["key"]); if (categories.Count > 0) throw new HttpResponseException(context.Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "Please, delete the categories children")); }
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; }