我有两个模型,行程和位置。我将返回一个自定义响应,其中包含一些行程字段以及具有tripID等于Trip的id的Location的数量。有我的代码(不起作用)。字段locationCount始终为空。
func getList(_ request: Request)throws -> Future<Response> {
let deviceIdReq = request.parameters.values[0].value
let queryTrips = Trip.query(on: request).filter(\.deviceId == deviceIdReq).all()
var tripsR = [TripCustomContent]()
var trips = [Trip]()
return queryTrips.flatMap { (result) -> (Future<Response>) in
trips = result
var count = 0
for t in trips {
let tripIdString = String(t.id!)
let v = Location.query(on: request).filter(\.tripID == tripIdString).count().map({ (res) -> Int in
return res
})/*.map{ (result) -> (Int) in
count = result
return result
}*/
let tripCustomContent = TripCustomContent.init(startTimestamp: t.startTimestamp, endTimestamp: t.endTimestamp, deviceId: t.deviceId, locationCount: v)
tripsR.append(tripCustomContent)
}
let jsonEncoder = JSONEncoder()
let data = try jsonEncoder.encode(tripsR)
let response = HTTPResponse.init(status: .ok, version: HTTPVersion.init(major: x, minor: y), headers: HTTPHeaders.init(), body: data)
let finalResponse = Response.init(http: response, using: request)
return try g.encode(for: request)
}
}
这是我的自定义内容结构:
struct TripCustomContent: Encodable {
var startTimestamp: String?
var endTimestamp: String?
var deviceId: String
var locationCount: Future<Int>
}
有什么建议吗?
答案 0 :(得分:2)
您正在尝试使用尚不可用的值。返回Future
时,没有返回其中的值。
因此,您希望您的TripCustomContent
像这样(用蒸气Content
代替Codable
:
struct TripCustomContent: Content {
var startTimestamp: String?
var endTimestamp: String?
var deviceId: String
var locationCount: Int
}
您正确查询了Trip
,但没有查询Location
。您也许可以尝试这样的事情:
return queryTrips.flatMap { trips -> Future<[TripCustomContent]> in
let tripIds = trips.map({ String($0.id!) })
return Location.query(on: request).filter(\.tripID ~~ tripIds).all().map { locations in
return trips.map { trip in
let locationCount = locations.filter({ $0.tripId == String(trip.id!) }).count
return TripCustomContent(... locationCount: locationCount)
}
}
}
我在这里做什么?
TripCustomContent
过滤的数据库位置,将每次旅行映射到tripId
的实例最后,您不需要自己编码JSON,只需返回符合Content
的对象即可:
func getList(_ request: Request) throws -> Future<[TripCustomContent]>
以上内容可能是您的策略的解决方案。但是也许您可以看看relations是不是它们可以更有效,更轻松,更快捷地进行。