public async Task <IActionResult> Index() { Client client = await _cm.FindByEmailAsync(User.Identity.Name); int borrowMax = client.BorrowMax; int numModels = await _identityMap.CountModelCopiesOfClient(client.clientId); int cartCount = HttpContext.Session.GetInt32("ItemsCount") ?? 0; TempData["totalBorrowed"] = cartCount + numModels; TempData["canBorrow"] = cartCount + numModels <= borrowMax; TempData["borrowMax"] = borrowMax; var modelCopies = await _identityMap.FindModelCopiesByClient(client.clientId); var returnViewModels = new List <ReturnViewModel>(modelCopies.Count); foreach (ModelCopy element in modelCopies) { string title = ""; switch (element.modelType) { case TypeEnum.Book: { title = (await _identityMap.FindBook(element.modelID)).Title; break; } case TypeEnum.Magazine: { title = (await _identityMap.FindMagazine(element.modelID)).Title; break; } case TypeEnum.Movie: { title = (await _identityMap.FindMovie(element.modelID)).Title; break; } case TypeEnum.Music: { title = (await _identityMap.FindMusic(element.modelID)).Title; break; } } returnViewModels.Add(new ReturnViewModel { BorrowDate = element.borrowedDate, ModelCopyId = element.id, ModelId = element.modelID, ModelType = element.modelType, ReturnDate = element.returnDate, Title = title }); } return(View(returnViewModels)); }
//registers modelcopies of selected items to the client public async Task <IActionResult> Borrow() { List <SessionModel> modelsToBorrow = HttpContext.Session.GetObject <List <SessionModel> >("Items") ?? new List <SessionModel>(); Client client = await _cm.FindByEmailAsync(User.Identity.Name); List <ModelCopy> alreadyBorrowed = await _identityMap.FindModelCopiesByClient(client.clientId); //Borrow all available copies of selected items Boolean successfulReservation = await _identityMap.ReserveModelCopiesToClient(modelsToBorrow, client.clientId); //if not all items were borrowed, determine which ones were not borrowed and display them to the client if (!successfulReservation) { List <ModelCopy> nowBorrowed = await _identityMap.FindModelCopiesByClient(client.clientId); HashSet <ModelCopy> borrowed = nowBorrowed.Except(alreadyBorrowed).ToHashSet(); List <SessionModel> notBorrowed = modelsToBorrow .Select(m => new { Id = m.Id, MT = m.ModelType }) .Except(borrowed .Select(m => new { Id = m.modelID, MT = m.modelType })) .Select(c => new SessionModel { Id = c.Id, ModelType = c.MT }) .ToList(); HttpContext.Session.SetObject("Items", notBorrowed); HttpContext.Session.SetInt32("ItemsCount", notBorrowed.Count); return(RedirectToAction(nameof(Index))); } //TODO Return to Home? HttpContext.Session.SetObject("Items", null); HttpContext.Session.SetInt32("ItemsCount", 0); return(RedirectToAction(nameof(Index))); }