public async Task <IActionResult> Desserts(string guestCode) { if (guestCode == null) { return(RedirectToAction("ErrorView")); } await DBCreationTask; //desserts contains all Desserts defined in the CLASS DISHTYPE //The (var test) fills the Entity Framework cache with subdishtypes, //or else Entity Framework throws NullReference var test = ctx.SubDishTypes.ToList(); List <DishType> desserts = ctx.DishTypes.Where(x => x.Course == CourseType.DESSERT).ToList(); var subDesserts = desserts.Where(x => x.SubDishType != null).Select(x => x.SubDishType).ToList(); List <SubDishType> uniqueDesserts = subDesserts.GroupBy(x => x.SubType).Select(x => x.FirstOrDefault()).ToList(); Dictionary <DishType, int> output = new Dictionary <DishType, int>(); foreach (DishType d in desserts) { output.Add(d, 0); } ctx.Orders.ToList(); Order order = ctx.Orders.Where(x => x.Owner.Code == guestCode).FirstOrDefault(); ctx.Dishes.ToList(); if (order.Selected != null) { ctx.DishTypes.ToList(); List <Dish> a = order.Selected.Where(x => x.Course.Course == CourseType.DESSERT).ToList(); var b = a.GroupBy(x => x.Course.Name).Select(x => new { type = x.FirstOrDefault().Course, quantity = x.Count() }).ToList(); foreach (var item in b) { output[item.type] = item.quantity; } } DishTypeViewModel dishTypeViewModel = new DishTypeViewModel() { DishTypes = desserts, SubDishTypes = uniqueDesserts, quantityDictionary = output }; //var desserts = ctx.DishTypes.ToList(); //var dessert = DishType.getAllDesserts(); //var result = dessert.ToList(); return(View(new GuestCodeWithModel <DishTypeViewModel>(dishTypeViewModel, guestCode))); }
public async Task <IActionResult> Mains(string guestCode) { if (guestCode == null) { return(RedirectToAction("ErrorView")); } await DBCreationTask; //The (var test) fills the Entity Framework cache with subdishtypes, //or else Entity Framework throws NullReference var test = ctx.SubDishTypes.ToList(); List <DishType> mains = ctx.DishTypes.Where(x => x.Course == CourseType.MAINCOURSE).ToList(); var subMains = mains.Where(x => x.SubDishType != null).Select(x => x.SubDishType).ToList(); var uniqueSubMains = subMains.GroupBy(x => x.SubType).Select(x => x.FirstOrDefault()).ToList(); Dictionary <DishType, int> output = new Dictionary <DishType, int>(); foreach (DishType d in mains) { output.Add(d, 0); } ctx.Orders.ToList(); Order order = ctx.Orders.Where(x => x.Owner.Code == guestCode).FirstOrDefault(); ctx.Dishes.ToList(); if (order.Selected != null) { ctx.DishTypes.ToList(); List <Dish> a = order.Selected.Where(x => x.Course.Course == CourseType.MAINCOURSE).ToList(); var b = a.GroupBy(x => x.Course.Name).Select(x => new { type = x.FirstOrDefault().Course, quantity = x.Count() }).ToList(); foreach (var item in b) { output[item.type] = item.quantity; } } DishTypeViewModel dishTypeViewModel = new DishTypeViewModel() { DishTypes = mains, SubDishTypes = uniqueSubMains, quantityDictionary = output }; return(View(new GuestCodeWithModel <DishTypeViewModel>(dishTypeViewModel, guestCode))); }
public async Task <IActionResult> Drinks(string guestCode) { if (guestCode == null) { return(RedirectToAction("ErrorView")); } await DBCreationTask; var test = ctx.SubDishTypes.ToList(); //collects all drinks DishType List <DishType> drinks = ctx.DishTypes.Where(x => x.Course == CourseType.DRINK).ToList(); //collects the drinks SubDishType var subDrinks = drinks.Where(x => x.SubDishType != null).Select(x => x.SubDishType).ToList(); //collects unique drinks from all drinks SubDishType List <SubDishType> uniqueSubDrinks = subDrinks.GroupBy(x => x.SubType).Select(x => x.FirstOrDefault()).ToList(); //Dictionary that maps DishType and the quantity (DishType->int) Dictionary <DishType, int> output = new Dictionary <DishType, int>(); //assigning all the drinks keyvalues to zero first foreach (DishType d in drinks) { output.Add(d, 0); } //Loading the orders table because of lazy entity framework ctx.Orders.ToList(); //Selects all orders belongs to unique guest Order order = ctx.Orders.Where(x => x.Owner.Code == guestCode).FirstOrDefault(); //Loading the Dishes table because of lazy entity framework ctx.Dishes.ToList(); //If the selected orders by the guest is not null, then assign each drink value to guests order quantity using dictionary if (order.Selected != null) { //Loading the DisheTypes table because of lazy entity framework ctx.DishTypes.ToList(); List <Dish> a = order.Selected.Where(x => x.Course.Course == CourseType.DRINK).ToList(); var b = a.GroupBy(x => x.Course.Name).Select(x => new { type = x.FirstOrDefault().Course, quantity = x.Count() }).ToList(); //assigning the drinks values (this replaces previous assigned zero values by guests order quantity for each drink) foreach (var item in b) { output[item.type] = item.quantity; } } var test1 = output; //drinks contains all drinks defined in the CLASS DISHTYPE DishTypeViewModel dishTypeViewModel = new DishTypeViewModel() { DishTypes = drinks, SubDishTypes = uniqueSubDrinks, quantityDictionary = output }; return(View(new GuestCodeWithModel <DishTypeViewModel>(dishTypeViewModel, guestCode))); }