public async Task <IActionResult> Buy(BundelDetailsViewModel viewModel) { if (viewModel.IsLogged) { foreach (BundelInhoud cursus in viewModel.Bundel.BundelInhoud) { AccountCatalogus newItem = new AccountCatalogus { CursusID = cursus.Cursus.CursusID, Voortgang = 1, AccountID = viewModel.CurrentUser.Id, VerloopTijd = DateTime.Now.AddMonths(cursus.Cursus.BeschikbaarheidInMaanden) }; _context.Add(newItem); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } throw new Exception(); }
public async Task <IActionResult> Bundel(int?id) { BundelDetailsViewModel viewModel = new BundelDetailsViewModel(); if (id == null) { return(NotFound()); } var bundel = await _context.Bundels .Include(c => c.BundelBeschrijving) .Include(c => c.BundelInhoud) .ThenInclude(c => c.Cursus) .ThenInclude(c => c.CursusBeschrijving) .FirstOrDefaultAsync(m => m.BundelID == id); if (bundel == null) { return(NotFound()); } viewModel.Bundel = bundel; viewModel.IsLogged = User.Identity.IsAuthenticated; viewModel.CurrentUser = _context.Account.Where(x => x.UserName == User.Identity.Name).FirstOrDefault(); if (viewModel.IsLogged) { List <AccountCatalogus> accountCatalogus = await _context.AccountCatalogus .Where(c => c.Account.UserName == User.Identity.Name) .Include(c => c.Cursus) .ToListAsync(); viewModel.IsOwned = (accountCatalogus.Where(x => viewModel.Bundel.BundelInhoud.Any(c => c.CursusID == x.CursusID)).Count() > 0); } else { viewModel.IsOwned = false; } return(View(viewModel)); }