public AddTripResponse AddTrip(AddTripRequest rq) { AddTripResponse rsp = new AddTripResponse(); try { var usr = Database.GetUserByApiKey(rq.ApiKey); Database.CreateTrip(usr.Id, rq.TripName); rsp.Description = "Created"; rsp.ResponseCode = (int)ResponseConstants.SUCCES; } catch { rsp.Description = "Key provided is invalid"; rsp.ResponseCode = (int)ResponseConstants.SUCCES; } return(rsp); }
public async Task <IActionResult> Trip([FromRoute] int userId, AddTripRequest trip) { if (ModelState.IsValid) { var user = _rideShareContext.Users.FirstOrDefault(u => u.Id == userId); if (user == null) { throw new Exception("User Not found"); } var newTrip = new Trip() { Capacity = trip.Capacity, Explanation = trip.Explanation, IsActive = trip.IsActive, Owner = user, TripDate = trip.TripDate, }; _rideShareContext.Trips.Add(newTrip); await _rideShareContext.SaveChangesAsync(); int index = 0; foreach (var route in trip.RouteList) { var item = new Trip_City(); item.CityId = route; item.TripId = newTrip.Id; item.Index = index++; _rideShareContext.Trip_Cities.Add(item); } await _rideShareContext.SaveChangesAsync(); } return(Created(string.Empty, null)); }