Пример #1
0
        public async Task <IActionResult> OnPostNextAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var wizardEvent = HttpContext.Session.GetJson <RideEvent>("WizardEvent");

            wizardEvent.MaxSignup     = EventWizard2.MaxSignup;
            wizardEvent.MaxSignUpType = EnumExtensionMethods.GetValueFromDescription <MaxSignUpType>(Input.SelectedMaxSignUpType);
            wizardEvent.Status        = EnumExtensionMethods.GetValueFromDescription <Status>(Input.SelectedStatus);
            HttpContext.Session.SetJson("WizardEvent", null);
            _context.RideEvent.Add(wizardEvent);
            foreach (var user in Input.SelectedUser)
            {
                // finds single selected user in list by full name
                // will change this so it searches by id instead
                var selectedUser = await _context.Users
                                   .AsNoTracking()
                                   .FirstOrDefaultAsync(m => m.FullName == user);

                RideLeaderAssignment RideLeaderAssignment = new RideLeaderAssignment
                {
                    InTandemUserID = selectedUser.Id,
                    RideEventID    = wizardEvent.ID
                };
                _context.RideLeaderAssignment.Add(RideLeaderAssignment);
            }
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            _context.RideEvent.Add(RideEvents);
            foreach (var user in Input.SelectedUser)
            {
                // finds single selected user in list by full name
                // will change this so it searches by id instead
                var selectedUser = await _context.Users
                                   .AsNoTracking()
                                   .FirstOrDefaultAsync(m => m.FullName == user);

                RideLeaderAssignment = new RideLeaderAssignment
                {
                    InTandemUserID = selectedUser.Id,
                    RideEventID    = RideEvents.ID
                };
                _context.RideLeaderAssignment.Add(RideLeaderAssignment);
            }
            await _context.SaveChangesAsync();


            return(RedirectToPage("./Index"));
        }
        } // OnGetAsync

        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            List <InTandemUser> SelectedInTandemUsers = new List <InTandemUser> {
            };

            foreach (var user in Input.SelectedUsers)
            {
                SelectedInTandemUsers.Add(_context.Users
                                          .FirstOrDefault(u => u.FullName == user));
            }


            RideEvent RideEventToUpdate = await _context.RideEvent
                                          .Include(r => r.RideLeaderAssignments)
                                          .ThenInclude(r => r.InTandemUser)
                                          .FirstOrDefaultAsync(m => m.ID == id);


            // takes list of names of ride leaders selected and adds them to a list of ride leader assignments
            var assignedLeaders = RideEventToUpdate.RideLeaderAssignments
                                  .Select(u => u.InTandemUser)
                                  .ToList();

            foreach (var leaderToBeAssigned in SelectedInTandemUsers)
            {
                // if the leader is not already assigned to the ride, assign them to the ride
                if (!assignedLeaders.Any(u => u.FullName.Equals(leaderToBeAssigned.FullName)))
                {
                    var NewLeaderAdded = new RideLeaderAssignment
                    {
                        InTandemUser   = leaderToBeAssigned,
                        InTandemUserID = leaderToBeAssigned.Id
                    };
                    RideEventToUpdate.RideLeaderAssignments.Add(NewLeaderAdded);
                }
            }
            // to indicate ride leader needs to be deleted, compare both lists
            // if the length of selected users is less than the number of assigned leaders
            // find which one is not in the selected users list and delete that assignment
            foreach (var assignedLeader in assignedLeaders)
            {
                // if a user is in the assignedUsers list but not in the selectedUsers list
                // remove the ride leader assignment containing that user
                if (!SelectedInTandemUsers.Any(u => u.FullName.Equals(assignedLeader.FullName)))
                {
                    RideLeaderAssignment RideLeaderAssignmentToBeRemoved = RideEventToUpdate.RideLeaderAssignments
                                                                           .SingleOrDefault(u => u.InTandemUser.FullName.Equals(assignedLeader.FullName));

                    RideEventToUpdate.RideLeaderAssignments.Remove(RideLeaderAssignmentToBeRemoved);
                }
            }


            RideEvent.Status        = EnumExtensionMethods.GetValueFromDescription <Status>(Input.SelectedStatus);
            RideEvent.EventType     = EnumExtensionMethods.GetValueFromDescription <EventType>(Input.SelectedEventType);
            RideEvent.MaxSignUpType = EnumExtensionMethods.GetValueFromDescription <MaxSignUpType>(Input.SelectedMaxSignUpType);
            // default entity tracking does not include navigation properties

            if (RideEventToUpdate == null)
            {
                return(NotFound());
            }
            // TruUpdateModelAsync is used to prevent overposting
            if (await TryUpdateModelAsync <RideEvent>(
                    RideEventToUpdate,
                    "RideEvent",
                    i => i.EventName, i => i.EventDate,
                    i => i.EventType, i => i.Description,
                    i => i.Location, i => i.Distance,
                    i => i.RideLeaderAssignments,
                    i => i.MaxSignup, i => i.Status
                    ))
            {
                _context.Update(RideEventToUpdate);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }

            return(RedirectToPage("./Index"));
        } // OnPostAsync