Group Group Group Group Group Group Group Group Group

Fluent querying problem


#1

I’m making a simple ride-sharing app and have the following scenario:

When a user makes a request to /api/trip/new to create a new trip, I want to retrieve the first driver from the database and assign their ID to a Trip object. Then I want to save both the trip and trip request in the database.

I have the following code:

func newTripHandler(_ req: Request, data: TripRequestCreateData) throws -> Future<TripRequest> {
let passenger = try req.requireAuthenticated(Passenger.self)
let tripRequest = try TripRequest(pickupLat: data.pickupLat,
                                  pickupLng: data.pickupLng,
                                  destLat: data.destLat,
                                  destLng: data.destLng,
                                  pickupAddress: data.pickupAddress,
                                  destAddress: data.destAddress,
                                  passengerId: passenger.requireID())

//Passenger's location
let lat = tripRequest.pickupLat
let lng = tripRequest.pickupLng

//Passenger's destination
let toLat = tripRequest.destLat
let toLng = tripRequest.destLng

let trip = Trip(passengerId: tripRequest.passengerId!,
                pickupLat: lat,
                pickupLng: lng,
                destLat: toLat,
                destLng: toLng,
                pickupAddress: tripRequest.pickupAddress,
                destAddress: tripRequest.destAddress)

let driver = Driver.query(on: req).first().unwrap(or: Abort(.notFound))

let driverId = driver.map(to: Driver.self) { driver in
  return driver.id
}

trip.driverId = driverId

print(trip)

return tripRequest.save(on: req)

}

struct TripRequestCreateData: Content {
  let pickupLat: Double
  let pickupLng: Double
  let destLat: Double
  let destLng: Double
  let pickupAddress: String
  let destAddress: String
}

I’m not sure how to save two models and how to properly retrieve the driver from the database. The driverId constant is of type EventLoopFuture instead of Driver.

What am I doing wrong?


#2

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]


#3

Figured it out

return Driver.query(on: req).first().unwrap(or: Abort(.notFound)).flatMap { driver in
  trip.driverId = driver.id
  
  return trip.save(on: req).flatMap { trip in
    return tripRequest.save(on: req)
  }
}

#4

@mfilipowicz Glad you fixed it! Cheers! :]