Swift&Vapor / Fluent:如何在发布路由器请求期间从另一个表中提取值

问题描述 投票:1回答:1

我正在使用Xcode 11.2Vapor3FluentSQLite中编写后端应用程序。我有一个POST请求来更新Table1中的记录的路由,但是在保存记录之前,我需要使用另一个键(位于我要传递的字段之一中)从另一个表中提取值。我被困在从另一个表访问该数据。

到目前为止我所拥有的:

职员表

final class Staff: Content, SQLiteUUIDModel, Migration, Parameter { 
  var id: UUID? 
  var staffID: String (value that's in the other table to match)
  var value1: Int (one of the values I'm trying to fetch)
  var value2: Int (another value I'm trying to fetch)
  ...(additional variables and Init

分配表

final class Assignments: Content, SQLiteUUIDModel, Migration, Parameter {
  var id: UUID?
  var staffID: String 
  ...(additional variables and Init

传入POST请求以更新现有分配的路由:

router.post("update", Assignments.parameter) { req -> Future<Assignments> in
  return try req.parameters.next(Assignments.self).flatMap { Assignment in
  return try req.content.decode(Assignments.self).flatMap { updatedAssignment in

  (Code where I perform calculations on the incoming 'Assignment' and save to 'updatedAssignment' before calling:)

  return WorkOrder.save(on: req)

我拥有的路由有效,修改了传入数据并将其写入SQLite数据库,但是我需要对几个字段进行计算或将其设置为Staff表中存储的值。 IE ::>


editedAssignment.variable = (variable) * (value1 from staff table for matching staffID)
editedAssignment.variable = (value2 from staff table for matching staffID)

到目前为止我尝试过的事情

let staffData = Staff.query(on: req).filter(\.staffID == staffID).all() before the calculations
(adding as a Future as:)
router.post(...) { req -> Future<Staff> in return try Staff.query(on: req).filter ...(rest of query)  

-此方法抛出整个请求,传入请求的值刚刚丢失。

理想情况下,如果我可以在计算中调用查询并将其另存为字典,那将是理想的选择,我似乎无法将其“数字化”。

我正在使用Vapor3和Fluent和SQLite在Xcode 11.2中编写一个后端应用程序。我有一条用于POST请求的路由,用于更新Table1中的记录,但是在保存记录之前,我需要从...

swift sqlite vapor vapor-fluent
1个回答
2
投票

您需要将其放入路线内,以便它成为链的一部分,而不是像您所遇到的那样脱节:

© www.soinside.com 2019 - 2024. All rights reserved.