简体   繁体   English

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

[英]Swift & Vapor/Fluent : How to pull values from another table during a post router request

I'm writing a backend app in Xcode 11.2 using Vapor3 and Fluent with SQLite .我正在Xcode 11.2使用Vapor3Fluent with SQLite编写后端应用程序。 I have a route for a POST request to update a record in Table1, but before I save the record, I need to pull values from another table with a key that's in one of the fields I'm passing in. The part that I'm getting stuck on is accessing that data from the other table.我有一个POST请求的路由来更新表 1 中的记录,但在保存记录之前,我需要从另一个表中提取值,该表的键位于我传入的字段之一中。我的部分我陷入困境是从另一个表访问该数据。

What I have so far:到目前为止我所拥有的:

Table for staff员工桌

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

Table for assignments作业表

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

Route for incoming POST request to update existing Assignment:用于更新现有分配的传入 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)

The route I have works, incoming data is modified and written to the SQLite database, however there are a couple of fields that I need to perform calculations on or set to values stored in the Staff table.我的路线有效,传入的数据被修改并写入 SQLite 数据库,但是我需要对几个字段执行计算或设置为存储在 Staff 表中的值。 IE: IE:


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

What I've tried so far到目前为止我尝试过的

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)  

-- this method throws off the whole request, the values of the incoming request are just lost. - 此方法抛出关闭整个请求,传入请求的值刚刚失去。

Ideally, if I could call a query and save as a dictionary in the calculations, that would be ideal, I just can't seem to "figure it out".理想情况下,如果我可以在计算中调用查询并另存为字典,那将是理想的,我似乎无法“弄清楚”。

You need to put it inside your route, so that it is part of the chain, rather than disjointed as you had it:您需要将它放在您的路线中,以便它成为链条的一部分,而不是像您一样脱节:

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
            return Staff.query(on: req).filter(\.staffID == staffID).first().flatMap {
                staffData in 
                /* Code to update 'updatedAssignment' */
                return WorkOrder.save(on: req)
            }
        }
    }
}

You will most likely get compiler errors and need to put explicit return types in, but I can't predict/test these.您很可能会遇到编译器错误并需要输入显式返回类型,但我无法预测/测试这些。 I've left it in, but I don't think it is a good idea that you are explicitly stating a Future<Assignments> is being returned when you end up with the return from WorkOrder.save... .我已经把它留在了,但我认为当你最终从WorkOrder.save...返回时明确声明正在返回Future<Assignments>并不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM