private ICollection <CategorizedGamesViewModel> buildGameViewModels(IQueryable <Game> games, int?gameTypeId) { var gameTypes = _context.GameTypes.OrderBy(g => g.Order).ToList(); List <CategorizedGamesViewModel> categorizedGames = new List <CategorizedGamesViewModel>(); foreach (var gameType in gameTypes) { if (gameTypeId != null) { if (gameType.Id == gameTypeId) { categorizedGames.Add( new CategorizedGamesViewModel { GamesType = gameType.Name, Games = games.Where(g => g.GameTypeId == gameType.Id).Select(g => new GameViewModel { Game = g, DayOfWeek = DateService.GetDayOfWeek(g.Date), DayOfMonth = DateService.GetDayOfMonth(g.Date), TimeOfDay = DateService.GetTimeOfDay(g.Date), Venue = _context.Arenas.Where(a => a.Id == g.HomeTeam.ArenaId).First().Name }).ToList() }); break; } } else { categorizedGames.Add( new CategorizedGamesViewModel { GamesType = gameType.Name, Games = games.Where(g => g.GameTypeId == gameType.Id).Select(g => new GameViewModel { Game = g, DayOfWeek = DateService.GetDayOfWeek(g.Date), DayOfMonth = DateService.GetDayOfMonth(g.Date), TimeOfDay = DateService.GetTimeOfDay(g.Date), Venue = _context.Arenas.Where(a => a.Id == g.HomeTeam.ArenaId).First().Name }).ToList() }); } } return(categorizedGames); }
public async Task <IActionResult> Book(int?gameId) { if (gameId == null) { return(NotFound()); } var game = _context.Games.Where(g => g.Id == gameId) .Include(g => g.GameType) .Include(g => g.HomeTeam) .Include(g => g.AwayTeam) .Include(g => g.Tickets) .Include(g => g.Tickets) .Include(g => g.HomeTeam.Arena).First(); ViewData["GameDate"] = DateService.GetDayOfWeek(game.Date) + ", " + DateService.GetDayOfMonth(game.Date) + " - " + DateService.GetTimeOfDay(game.Date); var viewModel = new TicketsBookingViewModel() { Game = game, AvailableTickets = new List <Ticket>(), BookedTickets = new List <Ticket>() }; var user = await _userManager.GetUserAsync(HttpContext.User); foreach (Ticket ticket in game.Tickets) { if (ticket.OrderId != null) { continue; } if (ticket.ShoppingCartId == null) { viewModel.AvailableTickets.Add(ticket); } else if (ticket.ShoppingCart.UserId == user.Id) { viewModel.BookedTickets.Add(ticket); } } return(View(viewModel)); }