public FlightsModule(SchedulerDatabase db, Scheduler scheduler)
            : base("/flights")
        {
            _db = db;
            _scheduler = scheduler;

            // Get all flights
            Get["/"] = _ => _db.Flights
                .Select(f => new
                {
                    f.Flight,
                    f.Arrives
                });

            // Add flights
            Post["/"] = parameters =>
            {
                throw new NotImplementedException("Delete this line to complete the first stage");

                // This API POSTs a list of flights at a time instead of individual flights.
                var flights = this.Bind<IEnumerable<FlightModel>>().ToList();
                _db.AddFlights(flights);
                // An example of a flight scheduling call. This currently does nothing.
                foreach (var flight in flights)
                {
                    _scheduler.ScheduleFlight(flight);
                }
                return null;
            };
        }
        /// <summary>
        /// Create Gates Module. The constructor is called by the IoC container
        /// instead of by our own code.
        /// </summary>
        public GatesModule(SchedulerDatabase db)
            : base("/gates")
        {
            _db = db;

            // This tells the web server to use the provided delegate (Lambda)
            // to serve a GET request at this endpoint.
            Get["/"] = _ => _db.Gates
                // This creates a new dynamic object instead of serializing
                // the gate model object. This is an easy way to map your internal 
                // model objects to the API model. 
                .Select(g => new
                {
                    g.Gate
                });

            // This delegate will be used to serve a POST request.
            Post["/"] = _ =>
            {
                // As long as the Gate model object has properties
                // matching the API model, you can take the shortcut 
                // of deserializing the request directly to the model.
                // The JSON serializer is configured to convert
                // TitleCase to camelCase.
                var gates = this.Bind<IEnumerable<GateModel>>();
                _db.AddGates(gates.ToArray());
                return null;
            };
        }
        public StatusEndpoint(Scheduler scheduler, SchedulerDatabase db)
            : base("/status")
        {
            Get["/"] = _ =>
                new StatusModel
                {
                    State = scheduler.State
                };

            Put["/"] = _ =>
            {
                var status = this.Bind<StatusModel>();

                if (status.State == SystemState.ClearData)
                {
                    db.ClearAll();
                }
                else if (scheduler.State != status.State)
                {
                    Console.WriteLine("System status set to {0}", status.State);
                    scheduler.State = status.State;
                }

                return new StatusModel
                {
                    UserKey = Program.UserKey,
                    State = scheduler.State
                };
            };
        }
        public StatusEndpoint(SchedulerDatabase db)
            : base("/status")
        {
            _db = db;

            Get["/"] = _ =>
                new StatusModel
                {
                    StartupKey = Program.StartupKey,
                    State = SystemState.Ready
                };

            Put["/"] = _ =>
            {
                var status = this.Bind<StatusModel>();
                if (status.State == SystemState.Reset)
                {
                    _db.ClearAll();
                }
                return new StatusModel
                {
                    StartupKey = Program.StartupKey,
                    State = SystemState.Ready
                };
            };
        }
        public FlightsModule(SchedulerDatabase db, Scheduler scheduler)
            : base("/flights")
        {
            _db = db;
            _scheduler = scheduler;

            // Get all flights
            Get["/"] = _ => _db.Flights
                .Select(f => new
                {
                    f.Flight,
                    f.Arrives
                });

            // Add flights
            Post["/"] = parameters =>
            {
                // This API POSTs a list of flights at a time instead of individual flights.
                var flights = this.Bind<IEnumerable<FlightModel>>().ToList();
                _db.AddFlights(flights);

                if (_db.Gates.Any())
                {
                    foreach (var flight in flights)
                    {
                        _scheduler.ScheduleFlight(flight);
                    }
                }
                return null;
            };
        }
        public ArrivalsModule(SchedulerDatabase db)
            : base("/arrivals")
        {
            _db = db;

            Get["/"] = _ => _db.Flights.Select(f =>
                new
                {
                    f.Flight,
                    f.Arrives,
                    _db.GetGateOrNullForFlight(f.Flight).Gate
                });
        }
        public ArrivalsModule(SchedulerDatabase db)
            : base("/arrivals")
        {
            _db = db;

            Get["/"] = _ => _db.Flights
                .OrderBy(f => f.Arrives)
                .Select(f =>
                    new
                    {
                        f.Flight,
                        f.Arrives,
                        f.Gate
                    });
        }
        public DeparturesEndpoint(SchedulerDatabase db)
            : base("/departures")
        {
            _db = db;

            Get["/"] = _ => _db.Flights
                .OrderBy(f => f.Departs)
                .Select(f =>
                    new
                    {
                        f.Flight,
                        f.Departs,
                        f.Gate
                    });
        }
 public Scheduler(SchedulerDatabase db)
 {
     _db = db;
 }
        public Scheduler(SchedulerDatabase db)
        {
            _db = db;

            State = SystemState.Ready;
        }