Пример #1
0
        public ActionResult Edit(EditPlaneViewModel plane)
        {
            if (!ModelState.IsValid)
            {
                return(View(plane));
            }


            var selectedViewerIds = new List <string>();

            if (plane.PostedViewers != null)
            {
                selectedViewerIds = plane.PostedViewers.PlaneViewerIds.ToList();
            }

            IEnumerable <string> admins     = GetAdmins().Select(a => a.Id);
            IList <string>       enumerable = admins as IList <string> ?? admins.ToList();

            selectedViewerIds.AddRange(enumerable);

            List <ApplicationUser> users = UserManager.Users.ToList();

            foreach (ApplicationUser user in users)
            {
                IEnumerable <Claim> userClaims = UserManager.GetClaims(user.Id)
                                                 .Where(c => c.Type == "PlaneAccess" && c.Value == plane.TailNumber)
                                                 .Select(c => c);
                if (selectedViewerIds.Contains(user.Id))
                {
                    if (!userClaims.Any())
                    {
                        UserManager.AddClaim(user.Id, new Claim("PlaneAccess", plane.TailNumber));
                    }
                }
                else
                {
                    if (userClaims.Any())
                    {
                        UserManager.RemoveClaim(user.Id, new Claim("PlaneAccess", plane.TailNumber));
                    }
                }
            }
            Plane editPlane = _db.Planes.Find(plane.TailNumber);

            editPlane.Color            = plane.Color;
            editPlane.Aircraft         = _db.Aircraft.Find(plane.AircraftId);
            editPlane.BlackText        = plane.BlackText;
            editPlane.Crew.Min         = 0;
            editPlane.Crew.Max         = plane.CrewMax;
            editPlane.Passengers.Max   = plane.PassengerMax;
            editPlane.Passengers.Min   = 0;
            _db.Entry(editPlane).State = EntityState.Modified;
            _db.SaveChanges();

            return(RedirectToAction("Details", new { id = editPlane.TailNumber }));
        }
Пример #2
0
        public ActionResult Edit(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Plane plane = _db.Planes.Find(id);

            if (plane == null)
            {
                return(HttpNotFound());
            }

            var model = new EditPlaneViewModel
            {
                AircraftId       = plane.Aircraft.AircraftId,
                Color            = plane.Color,
                TailNumber       = id,
                AvailibleViewers = PlaneViewerRepository.GetAllThatAreNotNotAdmin().ToList(),
                BlackText        = plane.BlackText,
                CrewMin          = plane.Crew.Min,
                CrewMax          = plane.Crew.Max,
                PassengerMax     = plane.Passengers.Max
            };


            List <ApplicationUser> admins = GetAdmins();

            List <ApplicationUser> users = UserManager.Users.ToList();

            string[] postedViewerIds = (from user in users
                                        where !admins.Contains(user)
                                        let claims =
                                            UserManager.GetClaims(user.Id)
                                            .Where(c => c.Type == "PlaneAccess" && c.Value == model.TailNumber)
                                            .Select(c => c)
                                            where claims.Any()
                                            select user.Id).ToArray();
            model.PostedViewers = new PostedViewers {
                PlaneViewerIds = postedViewerIds
            };
            if (postedViewerIds.Any())
            {
                model.SelectedViewers = PlaneViewerRepository.GetAll()
                                        .Where(x => postedViewerIds.Any(s => x.UserId.ToString(CultureInfo.InvariantCulture).Equals(s)))
                                        .ToList();
            }


            return(View(model));
        }
Пример #3
0
        public ActionResult Create(EditPlaneViewModel plane)
        {
            if (!ModelState.IsValid)
            {
                return(View(plane));
            }
            List <string> tailNumbers = _db.Planes.Select(p => p.TailNumber).ToList();
            string        tailNumber  = plane.TailNumber.ToUpper();

            if (tailNumbers.Contains(tailNumber))
            {
                ModelState.AddModelError("TailNumber", "TailNumber Already Used");
                return(View(plane));
            }
            var newPlane = new Plane
            {
                Aircraft   = _db.Aircraft.Find(plane.AircraftId),
                TailNumber = tailNumber,
                Color      = plane.Color,
                BlackText  = plane.BlackText,
                Passengers = new Range
                {
                    Min = 0,
                    Max = plane.PassengerMax
                },
                Crew = new Range
                {
                    Min = plane.CrewMin,
                    Max = plane.CrewMax
                }
            };


            List <ApplicationUser> admins = GetAdmins();

            foreach (ApplicationUser user in admins)
            {
                UserManager.AddClaim(user.Id, new Claim("PlaneAccess", tailNumber));
            }

            _db.Planes.Add(newPlane);

            _db.SaveChanges();

            return(RedirectToAction("Index"));
        }
 public EditPlaneDialog(Flight flight)
 {
     ViewModel = new(flight);
     this.InitializeComponent();
 }