Example #1
0
        public IHttpActionResult Post(long id, [FromBody] CreateRouteInfo model)
        {
            var bus = DB.Buses.IncludeFilter(x => x.Routes.Where(t => !t.IsSoftDeleted)).FirstOrDefault(x => x.Id == id);

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

            if (model.DepartureTime <= DateTime.Now)
            {
                return(BadRequest("Invalid departure time. The departure time is not realistic. Date must be greater than the current time!"));
            }

            if (model.DepartureTime > model.ArrivalTime)
            {
                return(BadRequest("The departure time cannot be greater than the departure time!"));
            }

            //  Check whether bus is idle within the time range
            if (bus.Routes.Any(x => DateHelper.IsBetween(x.DepartureTime, x.ArrivalTime, model.DepartureTime)))
            {
                return(BadRequest("The route cannot be created for the bus within the specified range. Possible reasons include the bus will be active or busy during the time range given!"));
            }

            var route = new BusRoute()
            {
                ArrivalTime    = model.ArrivalTime,
                Comments       = model.Comments,
                Bus            = bus,
                Cost           = model.Cost,
                DepartureTime  = model.DepartureTime,
                Destination    = model.Destination,
                DestinationLat = model.DestinationLat,
                DestinationLng = model.DestinationLng,
                From           = model.From,
                FromLat        = model.FromLat,
                FromLng        = model.FromLng,
            };

            DB.Routes.Add(route);
            DB.SaveChanges();
            return(Create <RouteInfo>(route));
        }
Example #2
0
 public static IEndpoint CreateRoute(long busId, CreateRouteInfo routeInfo) => new ApiEndpoint($"{BaseUri}/bus/{busId}", HttpMethod.Post, routeInfo);