public async Task<IHttpActionResult> InsertWaypoints(int id, Waypoint newWp)
        {
            //Use a transaction because we have to make possibly 2 commits to the database. If
            //either fails, we need to roll back
            using (var trans = db.Database.BeginTransaction())
            {
                try
                {
                    Mission mission = await db.Missions.FindAsync(id);
                    //If the mission doesn't exist, or the input parameter doesn't match the stored mission id, then return bad request
                    //The auto-generated WebApi2 
                    if (mission == null && newWp.MissionId != id)
                    {
                        return BadRequest("The Mission was not found and the waypoint was null");
                    }

                    newWp = db.Waypoints.Add(newWp);
                    await db.SaveChangesAsync();
                    var wps = mission.Waypoints;
                    //Try to get the nextWaypoint id
                    var nextPointId = newWp.NextWaypointId;
                    //If nextPointId is not null, fix the reference of the former previous waypoint
                    if (nextPointId != null)
                    {
                        //Find previous waypoint
                        Waypoint prevWp = (from wp in mission.Waypoints
                                           //Make sure we don't accidentally get the wp we just inserted
                                           where wp.NextWaypointId == nextPointId && wp.Id != newWp.Id && wp.IsActive
                                           select wp).FirstOrDefault();
                        if (prevWp != null) //If this new WP is not at the beginning of the list
                        {
                            prevWp.NextWaypointId = newWp.Id;
                            //This waypoint is now modified. Let the context know.
                            db.Entry(prevWp).State = System.Data.Entity.EntityState.Modified;
                            await db.SaveChangesAsync();
                        }
                    }
                    //We finished with the transactions, make sure to commit them.
                    trans.Commit();
                    var hub = GlobalHost.ConnectionManager.GetHubContext<VehicleHub>();
                    hub.Clients.All.WaypointInserted(id);

                    return Ok(newWp);
                }
                catch (DbUpdateException e)
                {
                    //Something went wrong. Rollback any changes, if any.
                    trans.Rollback();
                    return BadRequest();
                }

            }
        }
        public async Task<IHttpActionResult> Put(int id, Waypoint wp)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != wp.Id)
            {
                return BadRequest();
            }

            db.Entry(wp).State = System.Data.Entity.EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                bool exists = WaypointExists(id);
                if (!exists)
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            var hub = GlobalHost.ConnectionManager.GetHubContext<VehicleHub>();
            hub.Clients.All.WaypointUpdate(wp);
            return StatusCode(HttpStatusCode.NoContent);
        }
        public async Task<IHttpActionResult> AppendWaypoints(int id, Waypoint[] wps)
        {
            if (wps.Count() == 0)
            {
                return Ok(wps);
            }
            using (var trans = db.Database.BeginTransaction())
            {
                var wp = await getLastWaypoint(id);
                await this.addWaypointsToMission(id, wps);
                wp.NextWaypointId = wps[0].Id;
                db.Entry(wp).State = System.Data.Entity.EntityState.Modified;
                await db.SaveChangesAsync();

                var hub = GlobalHost.ConnectionManager.GetHubContext<VehicleHub>();
                hub.Clients.All.newRouteForMission(id);

                return Ok(wps);
            }
        }
        private async Task addWaypointsToMission(int missionId, Waypoint[] wps)
        {

            var wpLast = wps.Last();
            wpLast.MissionId = missionId;
            wpLast = db.Waypoints.Add(wpLast);
            wpLast.IsActive = true;
            await db.SaveChangesAsync();
            for (int i = wps.Count() - 2; i >= 0; i--)
            {
                var curWp = wps.ElementAt(i);
                curWp.NextWaypointId = wpLast.Id;
                curWp.MissionId = missionId;
                curWp.IsActive = true;
                wpLast = db.Waypoints.Add(curWp);
                await db.SaveChangesAsync();
            }
        }
        public async Task<IHttpActionResult> NewRouteForMission(int id, Waypoint[] wps)
        {
            using (var trans = db.Database.BeginTransaction())
            {
                Mission mission = await db.Missions.FindAsync(id);
                if (mission == null)
                {
                    return NotFound();
                }
                if (wps == null || wps.Count() < 1)
                {
                    return BadRequest();
                }
                foreach (var wp in mission.Waypoints)
                {
                    wp.IsActive = false;
                    db.Entry(wp).State = System.Data.Entity.EntityState.Modified;
                }
                await db.SaveChangesAsync();
                await this.addWaypointsToMission(id, wps);

                trans.Commit();
                var hub = GlobalHost.ConnectionManager.GetHubContext<VehicleHub>();
                hub.Clients.All.newRouteForMission(mission.id);

                return Ok(wps);
            }
        }