public ActionResult Create(FlightScheduleViewModel vmFlightSchedule)
        {
            vmFlightSchedule.ListFlight = (from c in db.tblFlights.ToList()
                                           select new SelectListItem
            {
                Text = c.FlightNo,
                Value = c.ID.ToString()
            }).ToList();
            vmFlightSchedule.ListRoute = (from c in db.tblRoutes.ToList()
                                          select new SelectListItem
            {
                Text = c.Departure + '-' + c.Arrival,
                Value = c.ID.ToString()
            }).ToList();

            if (ModelState.IsValid)
            {
                if (IsValid(vmFlightSchedule))
                {
                    tblFlightSchedule schedule = new tblFlightSchedule();
                    schedule.FlightID          = vmFlightSchedule.FlightID;
                    schedule.RouteID           = vmFlightSchedule.RouteID;
                    schedule.ArrivalDateTime   = vmFlightSchedule.ArrivalDateTime.Value;
                    schedule.DepartureDateTime = vmFlightSchedule.DepartureDateTime.Value;

                    db.tblFlightSchedules.Add(schedule);
                    db.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            return(View(vmFlightSchedule));
        }
        public BuyFlightPage()
        {
            InitializeComponent();
            flightViewModel           = new FlightScheduleViewModel();
            FlightList.BindingContext = flightViewModel;

            placesViewModel       = new PlacesViewModel();
            SPlace.BindingContext = placesViewModel;
            DPlace.BindingContext = placesViewModel;
        }
        public ActionResult Create()
        {
            FlightScheduleViewModel vmFlightSchedule = new FlightScheduleViewModel();

            vmFlightSchedule.ListFlight = (from c in db.tblFlights.ToList()
                                           select new SelectListItem
            {
                Text = c.FlightNo,
                Value = c.ID.ToString()
            }).ToList();
            vmFlightSchedule.ListRoute = (from c in db.tblRoutes.ToList()
                                          select new SelectListItem
            {
                Text = c.Departure + '-' + c.Arrival,
                Value = c.ID.ToString()
            }).ToList();
            return(View(vmFlightSchedule));
        }
        public bool IsValid(FlightScheduleViewModel vmFlightSchedule)
        {
            if (vmFlightSchedule.DepartureDateTime <= DateTime.Now)
            {
                ModelState.AddModelError("", "Departure Date cannot be less than today");
                return(false);
            }

            if (vmFlightSchedule.DepartureDateTime > vmFlightSchedule.ArrivalDateTime)
            {
                ModelState.AddModelError("", "Departure Date cannot be greater than arrival date");
                return(false);
            }

            var scheduleFlight = db.tblFlightSchedules.ToList().Where(c => c.FlightID == vmFlightSchedule.FlightID && c.DepartureDateTime.ToString("MM/dd/yyyy") == vmFlightSchedule.DepartureDateTime.Value.ToString("MM/dd/yyyy")).ToList();

            if (scheduleFlight.Count > 0)
            {
                ModelState.AddModelError("", "Selected flight already scheduled for this date");
                return(false);
            }

            return(true);
        }
Esempio n. 5
0
 /// <summary>
 /// Calculates the price of the specified flight.
 /// </summary>
 /// <param name="flight">The flight.</param>
 /// <returns>
 /// Price
 /// </returns>
 public decimal Calculate(FlightScheduleViewModel flight)
 {
     return(200 + _randomizer.Next(200));
 }