コード例 #1
0
        public ActionResult Edit(int id)
        {
            Flight flight = _context.Flights.Find(id);

            if (flight == null)
            {
                return(NotFound());
            }

            FlightEditViewModel model = new FlightEditViewModel
            {
                Id            = flight.Id,
                LocationFrom  = flight.LocationFrom,
                LocationTo    = flight.LocationTo,
                DepartureTime = flight.DepartureTime,
                LandingTime   = flight.LandingTime,
                PlaneType     = flight.PlaneType,
                PlaneNumber   = flight.PlaneNumber,
                PilotName     = flight.PilotName,
                RegularSeats  = flight.RegularSeats,
                BusinessSeats = flight.BusinessSeats
            };

            return(View(model));
        }
コード例 #2
0
 public ActionResult Edit(FlightEditViewModel editModel)
 {
     if (ModelState.IsValid)
     {
         Flight flight = new Flight()
         {
             Id            = editModel.Id,
             LocationFrom  = editModel.LocationFrom,
             LocationTo    = editModel.LocationTo,
             DepartureTime = editModel.DepartureTime,
             LandingTime   = editModel.LandingTime,
             PlaneType     = editModel.PlaneType,
             PlaneNumber   = editModel.PlaneNumber,
             PilotName     = editModel.PilotName,
             RegularSeats  = editModel.RegularSeats,
             BusinessSeats = editModel.BusinessSeats
         };
         try
         {
             _context.Flights.Update(flight);
             _context.SaveChanges();
         }
         catch (DbUpdateConcurrencyException)
         {
         }
         return(RedirectToAction(nameof(AdminList)));
     }
     ;
     return(View(editModel));
 }
コード例 #3
0
        async public Task PostNew_ShouldAddFlightAndRedirect()
        {
            // Arrange
            var controller          = new FlightsController(this.flightApiService, this.airportApiService, this.aircraftApiService);
            var flightEditViewModel = new FlightEditViewModel
            {
                DepartureAirportId   = "SRC",
                DestinationAirportId = "DST",
                AircraftId           = "AIR"
            };

            // Act
            var result = await controller.New(flightEditViewModel);

            // Assert
            var viewResult = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal(nameof(Tui.WebApplication.Controllers.FlightsController.Index), viewResult.ActionName);

            // The new flight is in the repository ?
            var newFlight = (await this.flightApiService.GetAll()).SingleOrDefault(f =>
                                                                                   f.Aircraft.Id == flightEditViewModel.AircraftId &&
                                                                                   f.DepartureAirport.Id == flightEditViewModel.DepartureAirportId &&
                                                                                   f.DestinationAirport.Id == flightEditViewModel.DestinationAirportId);

            Assert.NotNull(flightEditViewModel);
        }
コード例 #4
0
        public async Task <IActionResult> EditFlight(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Flight flight = await _context.Flights.FindAsync(id);

            if (flight == null)
            {
                return(NotFound());
            }

            FlightEditViewModel model = new FlightEditViewModel
            {
                Id        = flight.Id,
                From      = flight.From,
                To        = flight.To,
                TakeOff   = flight.TakeOff,
                Landing   = flight.Landing,
                TypePlane = flight.TypePlane,
                PlaneId   = flight.PlaneId,
                PilotName = flight.PilotName,
                AvailablePassengerSeats     = flight.AvailablePassengerSeats,
                AvailableBusinessClassSeats = flight.AvailableBusinessClassSeats
            };

            return(View(model));
        }
コード例 #5
0
        async public Task <IActionResult> Edit(FlightEditViewModel flightEditViewModel)
        {
            this.EnforceCustomValidation(flightEditViewModel);

            if (this.ModelState.IsValid)
            {
                var flightDto = new FlightDto
                {
                    Id = flightEditViewModel.Id,
                    DepartureAirport = new AirportDto {
                        Id = flightEditViewModel.DepartureAirportId
                    },
                    DestinationAirport = new AirportDto {
                        Id = flightEditViewModel.DestinationAirportId
                    },
                    Aircraft = new AircraftDto {
                        Id = flightEditViewModel.AircraftId
                    }
                };

                await this.flightApiService.Update(flightDto);

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                await this.PopulateLists(flightEditViewModel);

                return(View(flightEditViewModel));
            }
        }
コード例 #6
0
 private void EnforceCustomValidation(FlightEditViewModel flightEditViewModel)
 {
     if (flightEditViewModel.DepartureAirportId == flightEditViewModel.DestinationAirportId)
     {
         this.ModelState.AddModelError("DepartureAirportId", "The departure and destination airports must differ");
         this.ModelState.AddModelError("DestinationAirportId", "The departure and destination airports must differ");
     }
 }
コード例 #7
0
        async public Task <IActionResult> New()
        {
            var flightViewModel = new FlightEditViewModel();

            await this.PopulateLists(flightViewModel);

            return(View(flightViewModel));
        }
コード例 #8
0
        async Task PopulateLists(FlightEditViewModel flightEditViewModel)
        {
            var airportsDto = await airportApiService.GetAll();

            var aircraftsDto = await aircraftApiService.GetAll();

            flightEditViewModel.Airports  = new SelectList(airportsDto.OrderBy(a => a.Name), "Id", "Name");
            flightEditViewModel.Aircrafts = new SelectList(aircraftsDto.OrderBy(a => a.Name), "Id", "Name");
        }
コード例 #9
0
        public async Task <ActionResult> Edit(FlightEditViewModel model)
        {
            var operation = await _flightService.EditFlightAsync(model.ToDto());

            if (operation.Success)
            {
                Session["Edit"] = new AlertViewModel(operation.Message, AlertType.Success);

                return(RedirectToAction("Index", "Home"));
            }

            return(new HttpOperationStatusResult(operation));
        }
コード例 #10
0
        async public Task <IActionResult> Edit(Guid id)
        {
            var flightDto = await flightApiService.GetById(id);

            var flightEditViewModel = new FlightEditViewModel
            {
                Id = flightDto.Id,
                DepartureAirportId   = flightDto.DepartureAirport.Id,
                DestinationAirportId = flightDto.DestinationAirport.Id,
                AircraftId           = flightDto.Aircraft.Id
            };

            await this.PopulateLists(flightEditViewModel);

            return(View(flightEditViewModel));
        }
コード例 #11
0
        public IActionResult Edit(int id)
        {
            if (!this.User.IsAuthenticated)
            {
                return(RedirectToLogin());
            }

            Flight flight;
            var    isAdmin = false;

            using (this.Context)
            {
                flight  = this.Context.Flights.Find(id);
                isAdmin = this.Context.Users.First(u => u.Name == this.User.Name).IsAdmin;
            }

            if (!isAdmin)
            {
                this.Model.Data["error"] = "Only admin can add flight";
                return(RedirectToHome());
            }

            if (flight is null)
            {
                return(RedirectToHome());
            }

            var model = new FlightEditViewModel
            {
                Id          = flight.Id,
                ImageUrl    = flight.ImageUrl,
                Date        = flight.Date,
                Origin      = flight.Origin,
                Time        = flight.Time,
                Destination = flight.Destination
            };

            this.Model.Data["Id"]          = model.Id.ToString();
            this.Model.Data["Destination"] = model.Destination;
            this.Model.Data["Origin"]      = model.Origin;
            this.Model.Data["Date"]        = model.Date.ToString();
            this.Model.Data["Time"]        = model.Time.ToString();
            this.Model.Data["ImageUrl"]    = model.ImageUrl;
            return(this.View());
        }
コード例 #12
0
        public IActionResult Edit(Guid id)
        {
            Flight flight             = flightService.GetFlightById(id);
            FlightEditViewModel model = new FlightEditViewModel()
            {
                ArrivalTime           = flight.ArrivalTime,
                BusinessClassCapacity = flight.BusinessClassCapacity,
                CaptainName           = flight.CaptainName,
                DepartureCity         = flight.DepartureCity,
                DepartureTime         = flight.DepartureTime,
                DestinationCity       = flight.DestinationCity,
                PlaneCapacity         = flight.PlaneCapacity,
                PlaneID   = flight.PlaneID,
                PlaneType = flight.PlaneType
            };

            return(View(model));
        }
コード例 #13
0
        public IActionResult Edit(FlightEditViewModel model)
        {
            Flight flight = new Flight()
            {
                ArrivalTime           = model.ArrivalTime,
                BusinessClassCapacity = model.BusinessClassCapacity,
                CaptainName           = model.CaptainName,
                DepartureCity         = model.DepartureCity,
                DepartureTime         = model.DepartureTime,
                DestinationCity       = model.DestinationCity,
                PlaneCapacity         = model.PlaneCapacity,
                PlaneID   = model.PlaneID,
                PlaneType = model.PlaneType
            };

            flightService.UpdateFlight(model);

            return(RedirectToAction("Details"));
        }
コード例 #14
0
ファイル: FlightService.cs プロジェクト: KalinD/FlightManager
        public Flight UpdateFlight(FlightEditViewModel model)
        {
            Flight dbFlight = dBContext.Flights.Where(f => f.FlightID == model.FlightId).First();

            dbFlight.ArrivalTime           = model.ArrivalTime;
            dbFlight.BusinessClassCapacity = model.BusinessClassCapacity;
            dbFlight.CaptainName           = model.CaptainName;
            dbFlight.DepartureCity         = model.DepartureCity;
            dbFlight.DepartureTime         = model.DepartureTime;
            dbFlight.DestinationCity       = model.DestinationCity;
            dbFlight.PlaneCapacity         = model.PlaneCapacity;
            dbFlight.PlaneID   = model.PlaneID;
            dbFlight.PlaneType = model.PlaneType;

            dBContext.Flights.Update(dbFlight);
            dBContext.SaveChanges();

            return(dbFlight);
        }
コード例 #15
0
        async public Task PostNew_WhenInvalid_ShouldReturnBackToEdit()
        {
            // Arrange
            var controller          = new FlightsController(this.flightApiService, this.airportApiService, this.aircraftApiService);
            var flightEditViewModel = new FlightEditViewModel
            {
                DepartureAirportId   = "SRC",
                DestinationAirportId = "DST",
                AircraftId           = null // Invalid data
            };

            controller.ModelState.AddModelError("AircraftId", "Can not be null");

            // Act
            var result = await controller.New(flightEditViewModel);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.IsAssignableFrom <FlightEditViewModel>(viewResult.Model); // We are back with the Edit model
        }
コード例 #16
0
        public async Task <IActionResult> EditFlight(FlightEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                Flight flight = new Flight
                {
                    Id        = model.Id,
                    From      = model.From,
                    To        = model.To,
                    TakeOff   = model.TakeOff,
                    Landing   = model.Landing,
                    TypePlane = model.TypePlane,
                    PlaneId   = model.PlaneId,
                    PilotName = model.PilotName,
                    AvailablePassengerSeats     = model.AvailablePassengerSeats,
                    AvailableBusinessClassSeats = model.AvailableBusinessClassSeats
                };

                try
                {
                    _context.Update(flight);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeExists(flight.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction(nameof(ListFlights)));
            }

            return(View(model));
        }
コード例 #17
0
        public IActionResult Edit(FlightEditViewModel model)
        {
            if (!this.User.IsAuthenticated)
            {
                return(RedirectToLogin());
            }

            var isAdmin = false;

            using (this.Context)
            {
                isAdmin = this.Context.Users.First(u => u.Name == this.User.Name).IsAdmin;

                if (!isAdmin)
                {
                    this.Model.Data["error"] = "Only admin can edit flight";
                    return(RedirectToHome());
                }

                if (!this.IsValidModel(model))
                {
                    this.Model.Data["error"] = GetErrorMessageFromInvalidProp(model);
                    return(this.View());
                }


                var flightDb = this.Context.Flights.Find(model.Id);
                flightDb.ImageUrl    = model.ImageUrl;
                flightDb.Date        = model.Date;
                flightDb.Destination = model.Destination;
                flightDb.Origin      = model.Origin;
                flightDb.Time        = model.Time;


                this.Context.SaveChanges();
            }

            return(RedirectToHome());
        }
コード例 #18
0
 public static FlightDto ToDto(this FlightEditViewModel model)
 {
     return(MapperWEB.Map <FlightEditViewModel, FlightDto>(model));
 }