private void SetupEditDropdowns(EditModel viewModel)
 {
     viewModel.PossibleCountry = countryRepository.All;
     viewModel.PossibleCategory = new List<SelectListItem>() { new SelectListItem() { Text = "A", Value = "A" }, new SelectListItem() { Text = "B", Value = "B" }, new SelectListItem() { Text = "C", Value = "C" } };
 }
        private ActionResult GenerateAirportView(EditModel viewModel)
        {
            EditModel editedViewModel = new EditModel();
            editedViewModel.CurrentVersion = airportVersionRepository.FindIncluding(viewModel.CurrentVersion.ID, t => t.Arrival.CreatedByUser, t => t.Departure.CreatedByUser, t => t.Detail.CreatedByUser, t => t.GroundOp.CreatedByUser, t => t.Hazard.CreatedByUser, t => t.Terrain.CreatedByUser);
            editedViewModel.CurrentVersion.Departure.Text = viewModel.CurrentVersion.Departure.Text;
            editedViewModel.CurrentVersion.Arrival.Text = viewModel.CurrentVersion.Arrival.Text;
            editedViewModel.CurrentVersion.GroundOp.Text = viewModel.CurrentVersion.GroundOp.Text;
            editedViewModel.CurrentVersion.Terrain.Text = viewModel.CurrentVersion.Terrain.Text;
            editedViewModel.CurrentVersion.Hazard.Text = viewModel.CurrentVersion.Hazard.Text;
            editedViewModel.CurrentVersion.Detail.Elevation = viewModel.CurrentVersion.Detail.Elevation;
            editedViewModel.CurrentVersion.Detail.MagVar = viewModel.CurrentVersion.Detail.MagVar;
            editedViewModel.CurrentVersion.Detail.Category = viewModel.CurrentVersion.Detail.Category;
            editedViewModel.CurrentVersion.Detail.LongestRunway = viewModel.CurrentVersion.Detail.LongestRunway;

            SetupEditDropdowns(editedViewModel);

            return View("Details", editedViewModel);
        }
        public ActionResult Save(EditModel viewModel)
        {
            if (ModelState.IsValid) {

                var updatedDetail = airportDetailRepository.Find(viewModel.CurrentVersion.Detail.ID);
                updatedDetail.Elevation = viewModel.CurrentVersion.Detail.Elevation;
                updatedDetail.MagVar = viewModel.CurrentVersion.Detail.MagVar;
                updatedDetail.Category = viewModel.CurrentVersion.Detail.Category;
                updatedDetail.LongestRunway = viewModel.CurrentVersion.Detail.LongestRunway;

                if (airportRepository.Update(UserContext.GetCurrent().ID, viewModel.CurrentVersion.ID, viewModel.CurrentVersion.Terrain, viewModel.CurrentVersion.Arrival, viewModel.CurrentVersion.Departure, updatedDetail, viewModel.CurrentVersion.GroundOp, viewModel.CurrentVersion.Hazard)) {
                    airportRepository.Save();
                    return RedirectToAction("Details", new { id = viewModel.CurrentVersion.AirportICAO, message = "ChangesSaved" });
                } else {
                    return RedirectToAction("Details", new { id = viewModel.CurrentVersion.AirportICAO, message = "NoChange" });
                }

            } else {

                return GenerateAirportView(viewModel);
            }
        }
        public ActionResult Preview(EditModel viewModel)
        {
            ViewBag.IsPreview = true;
            ViewBag.ShowNoChangesMessage = false;
            ViewBag.ShowChangesSaved = false;

            var original = airportVersionRepository.FindCurrentIncluding(viewModel.CurrentVersion.AirportICAO, t => t.Arrival.CreatedByUser, t => t.Departure.CreatedByUser, t => t.Detail.CreatedByUser, t => t.GroundOp.CreatedByUser, t => t.Hazard.CreatedByUser, t => t.Terrain.CreatedByUser);

            if (!viewModel.CurrentVersion.Detail.Equals(original.Detail)) {
                original.Detail.Elevation = viewModel.CurrentVersion.Detail.Elevation;
                original.Detail.MagVar = viewModel.CurrentVersion.Detail.MagVar;
                original.Detail.Category = viewModel.CurrentVersion.Detail.Category;
                original.Detail.LongestRunway = viewModel.CurrentVersion.Detail.LongestRunway;
                original.Detail.CreatedOn = DateTime.UtcNow;
                original.Detail.CreatedByUser = UserContext.GetCurrent().User;
            }

            if (!string.Equals(viewModel.CurrentVersion.Departure.Text, original.Departure.Text)) {
                original.Departure.Text = viewModel.CurrentVersion.Departure.Text;
                original.Departure.CreatedOn = DateTime.UtcNow;
                original.Departure.CreatedByUser = UserContext.GetCurrent().User;
            }

            if (!string.Equals(viewModel.CurrentVersion.Arrival.Text, original.Arrival.Text)) {
                original.Arrival.Text = viewModel.CurrentVersion.Arrival.Text;
                original.Arrival.CreatedOn = DateTime.UtcNow;
                original.Arrival.CreatedByUser = UserContext.GetCurrent().User;
            }

            if (!string.Equals(viewModel.CurrentVersion.GroundOp.Text, original.GroundOp.Text)) {
                original.GroundOp.Text = viewModel.CurrentVersion.GroundOp.Text;
                original.GroundOp.CreatedOn = DateTime.UtcNow;
                original.GroundOp.CreatedByUser = UserContext.GetCurrent().User;
            }

            if (!string.Equals(viewModel.CurrentVersion.Terrain.Text, original.Terrain.Text)) {
                original.Terrain.Text = viewModel.CurrentVersion.Terrain.Text;
                original.Terrain.CreatedOn = DateTime.UtcNow;
                original.Terrain.CreatedByUser = UserContext.GetCurrent().User;
            }

            if (!string.Equals(viewModel.CurrentVersion.Hazard.Text, original.Hazard.Text)) {
                original.Hazard.Text = viewModel.CurrentVersion.Hazard.Text;
                original.Hazard.CreatedOn = DateTime.UtcNow;
                original.Hazard.CreatedByUser = UserContext.GetCurrent().User;
            }

            return View("Details", original);
        }
        public ActionResult Edit(EditModel viewModel)
        {
            if (ModelState.IsValid) {

                var updatedDetail = airportDetailRepository.Find(viewModel.CurrentVersion.Detail.ID);
                updatedDetail.Elevation = viewModel.CurrentVersion.Detail.Elevation;
                updatedDetail.MagVar = viewModel.CurrentVersion.Detail.MagVar;
                updatedDetail.Category = viewModel.CurrentVersion.Detail.Category;
                updatedDetail.LongestRunway = viewModel.CurrentVersion.Detail.LongestRunway;

                if (airportRepository.Update(UserContext.GetCurrent().ID, viewModel.CurrentVersion.ID, viewModel.CurrentVersion.Terrain, viewModel.CurrentVersion.Arrival, viewModel.CurrentVersion.Departure, updatedDetail, viewModel.CurrentVersion.GroundOp, viewModel.CurrentVersion.Hazard)) {
                    return Preview(viewModel);
                } else {
                    return GenerateAirportView(viewModel);
                }

            } else {

                return GenerateAirportView(viewModel);
            }
        }
        public ActionResult Edit(int id)
        {
            EditModel viewModel = new EditModel();
            viewModel.CurrentVersion = airportVersionRepository.FindIncluding(id, t => t.Arrival.CreatedByUser, t => t.Departure.CreatedByUser, t => t.Detail.CreatedByUser, t => t.GroundOp.CreatedByUser, t => t.Hazard.CreatedByUser, t => t.Terrain.CreatedByUser);
            SetupEditDropdowns(viewModel);

            return View(viewModel);
        }