Exemple #1
0
        private static ManagePilotBindingViewModel ManagePilotBindingViewModel(ApplicationUser user)
        {
            Pilot        userPilotBinding = null;
            List <Pilot> otherPilots      = new List <Pilot>();

            using (var context = new FlightContext())
            {
                if (user.BoundToPilotId != null)
                {
                    userPilotBinding = context.Pilots.Find(Convert.ToInt32(user.BoundToPilotId));
                    // Load club reference information
                    if (userPilotBinding != null)
                    {
                        context.Entry(userPilotBinding).Reference(p => p.Club).Load();
                    }
                }

                if (user.EmailConfirmed)
                {
                    otherPilots.AddRange(context.Pilots.Where(p => p.Email == user.Email).Include(c => c.Club).ToList());
                }

                if (user.PhoneNumberConfirmed)
                {
                    foreach (var pilot in context.Pilots.Where(p => p.MobilNumber == user.PhoneNumber).Include(c => c.Club).ToList())
                    {
                        if (!otherPilots.Exists(o => o.PilotId == pilot.PilotId))
                        {
                            otherPilots.Add(pilot);
                        }
                    }
                }

                // Remove the existing pilot
                if (userPilotBinding != null && otherPilots.Any())
                {
                    otherPilots = otherPilots.Where(p => p.PilotId != userPilotBinding.PilotId).ToList();
                }

                // Auto set primary pilot binding information
                if (string.IsNullOrEmpty(user.BoundToPilotId) && otherPilots.Count == 1)
                {
                    user.BoundToPilotId = otherPilots.First().PilotId.ToString();
                    // Save to database
                    using (var appcontext = new ApplicationDbContext())
                    {
                        var appUser = appcontext.Users.Find(user.Id);
                        if (appUser != null)
                        {
                            appUser.BoundToPilotId = user.BoundToPilotId;
                            appcontext.SaveChanges();
                        }
                    }
                    userPilotBinding = otherPilots.First();
                    otherPilots      = new List <Pilot>();
                }
            }

            var result = new ManagePilotBindingViewModel
            {
                CurrentPilotBinding    = userPilotBinding,
                PotentialPilotBindings = otherPilots
            };

            return(result);
        }
        private static ManagePilotBindingViewModel ManagePilotBindingViewModel(ApplicationUser user)
        {
            Pilot userPilotBinding = null;
            List<Pilot> otherPilots = new List<Pilot>();
            using (var context = new FlightContext())
            {
                if (user.BoundToPilotId != null)
                {
                    userPilotBinding = context.Pilots.Find(Convert.ToInt32(user.BoundToPilotId));
                    // Load club reference information 
                    if (userPilotBinding != null)
                    {
                        context.Entry(userPilotBinding).Reference(p => p.Club).Load();
                    }
                }

                if (user.EmailConfirmed)
                {
                    otherPilots.AddRange(context.Pilots.Where(p => p.Email == user.Email).Include(c => c.Club).ToList());
                }

                if (user.PhoneNumberConfirmed)
                {
                    foreach (var pilot in context.Pilots.Where(p => p.MobilNumber == user.PhoneNumber).Include(c => c.Club).ToList())
                    {
                        if (!otherPilots.Exists(o => o.PilotId == pilot.PilotId))
                        {
                            otherPilots.Add(pilot);
                        }
                    }
                }

                // Remove the existing pilot
                if (userPilotBinding != null && otherPilots.Any())
                {
                    otherPilots = otherPilots.Where(p => p.PilotId != userPilotBinding.PilotId).ToList();
                }

                // Auto set primary pilot binding information
                if (string.IsNullOrEmpty(user.BoundToPilotId) && otherPilots.Count == 1)
                {
                    user.BoundToPilotId = otherPilots.First().PilotId.ToString();
                    // Save to database
                    using (var appcontext = new ApplicationDbContext())
                    {
                        var appUser = appcontext.Users.Find(user.Id);
                        if (appUser != null)
                        {
                            appUser.BoundToPilotId = user.BoundToPilotId;
                            appcontext.SaveChanges();
                        }
                    }
                    userPilotBinding = otherPilots.First();
                    otherPilots = new List<Pilot>();
                }

            }

            var result = new ManagePilotBindingViewModel
            {
                CurrentPilotBinding = userPilotBinding,
                PotentialPilotBindings = otherPilots
            };

            return result;
        }