public IActionResult PostAddBusStopToRoute(int routeId, int busStopId) { try { var newRouteBusStop = new RouteBusStop { BusStopID = busStopId, RouteID = routeId, Order = 0 }; if (this._RouteBusStopRepository.SaveRouteBusStop(newRouteBusStop) > 0) { return(Ok(true)); } return(BadRequest(new BadRequestMessage { Message = new string[] { "The bus stop fails to add to the route.", "Tip: The bus stop is already in the route.", "The bus must already exist.", "The route must already exist." } })); } catch (Exception) { return(StatusCode(500, "Internal server error")); } }
public IActionResult PostOrderOfBusStop(int routeId, int busStopId, int order) { try { var newRouteBusStop = new RouteBusStop { BusStopID = busStopId, RouteID = routeId, Order = order }; if (this._RouteBusStopRepository.SaveRouteBusStop(newRouteBusStop) > 0) { return(Ok(true)); } return(BadRequest(new BadRequestMessage { Message = new string[] { "Sequence number fails to update.", "Tip: The bus must be already in the route.", "The bus must already exist.", "The route must already exist." } })); } catch (Exception) { return(StatusCode(500, "Internal server error")); } }
public int SaveRouteBusStop(RouteBusStop routeBusStop) { //Is the bus stop already in the route. //if ( // this._Context.RouteBusStops.Where(o => o.BusStopID == routeBusStop.BusStopID && o.RouteID == routeBusStop.RouteID).Any() // ) //{ // return 0; //} //Is bus and route already exist. if ( (!this._Context.BusStops.Where(o => o.ID == routeBusStop.BusStopID).Any()) || (!this._Context.Routes.Where(o => o.ID == routeBusStop.RouteID).Any()) ) { return(0); } var isNew = true; var routeBusStops = this._Context.RouteBusStops.Where(o => o.BusStopID == routeBusStop.BusStopID && o.RouteID == routeBusStop.RouteID).ToList(); if (routeBusStops.Count > 0) { isNew = false; } if (isNew) { this._Context.RouteBusStops.Add(routeBusStop); this._Context.SaveChanges(); return(1); } else { var dbEntry = routeBusStops.First(); if (dbEntry != null) { dbEntry.BusStopID = routeBusStop.BusStopID; dbEntry.RouteID = routeBusStop.RouteID; dbEntry.Order = routeBusStop.Order; this._Context.SaveChanges(); return(1); } } return(0); }