private async Task <SelectList> GetAllUsers() { //Get the list of users from the database List <CreateForViewModel> customerList = new List <CreateForViewModel>(); List <Int32> customerIDList = new List <Int32>(); Int32 count = 0; foreach (AppUser user in _userManager.Users) { if (await _userManager.IsInRoleAsync(user, "Customer") == true) //user is in the role { //add user to list of members CreateForViewModel newcus = new CreateForViewModel(); newcus.SelectCustomerName = user.Email; newcus.SelectCustomerID = count; customerIDList.Add(count); customerList.Add(newcus); count += 1; } } //convert the list to a SelectList by calling SelectList constructor //MonthID and MonthName are the names of the properties on the Month class //MonthID is the primary key SelectList customerSelectList = new SelectList(customerList, "SelectCustomerID", "SelectCustomerName"); //return the electList return(customerSelectList); }
public async Task <IActionResult> CreateFor([Bind("SelectedCustomerID, SelectShowingID")] CreateForViewModel cfvm) { List <CreateForViewModel> customerList = new List <CreateForViewModel>(); foreach (AppUser user in _userManager.Users) { if (await _userManager.IsInRoleAsync(user, "Customer") == true) //user is in the role { //add user to list of members CreateForViewModel newcus = new CreateForViewModel(); newcus.SelectCustomerName = user.Email; customerList.Add(newcus); } } // find the orders that match the user selected; we have to first a) get the int idx selected b) map that idx to the Email in customerList // c) match that to the Emails of Customers // finds the user that matches the selected ID AppUser customer = _userManager.Users.Where(u => u.Email == customerList[cfvm.SelectedCustomerID].SelectCustomerName).First(); // get list of orders belonging to that user List <Order> orders = _context.Orders.Include(o => o.Tickets) .ThenInclude(t => t.Showing) .ThenInclude(s => s.Movie) .Include(o => o.Tickets) .ThenInclude(t => t.Showing) .ThenInclude(s => s.Price).Where(o => o.Customer.UserName == customer.Email).ToList(); // instantiate an order Order current_order = new Order(); // see if any of that user's orders are active; if yes, set the instantiated order to that. bool check = false; foreach (Order order in orders) { if (order.OrderHistory == OrderHistory.Future) { current_order = order; check = true; break; } } if (check == false) { current_order.OrderNumber = Utilities.GenerateOrderNumber.GetNextOrderNumber(_context); current_order.OrderHistory = OrderHistory.Future; current_order.PopcornPointsUsed = false; current_order.GiftOrder = false; current_order.Customer = customer; if ((DateTime.Now.Date - current_order.Customer.Birthdate).TotalDays >= 21900) { current_order.Discount = _context.Prices.Where(p => p.PriceType == PType.SeniorCitizen).FirstOrDefault(); } _context.Orders.Add(current_order); _context.SaveChanges(); } return(RedirectToAction("Create", "Tickets", new { showingID = cfvm.SelectShowingID, orderID = current_order.OrderID })); }
public async Task <IActionResult> CreateFor(Int32 showingID) { ViewBag.AllCustomers = await GetAllUsers(); CreateForViewModel cfvm = new CreateForViewModel(); cfvm.SelectShowingID = showingID; return(View(cfvm)); }