From bca8ac2bf3a4a306e1fef1c0ac26231c6340eaf9 Mon Sep 17 00:00:00 2001 From: Kaitlin Mahar Date: Fri, 28 Jan 2022 16:48:50 -0500 Subject: [PATCH] SWIFT-1396 Update Vapor template for async/await (#7) --- Package.swift | 10 +++++----- Sources/App/routes.swift | 20 +++++++++----------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Package.swift b/Package.swift index c484cff..f1b2e12 100644 --- a/Package.swift +++ b/Package.swift @@ -1,14 +1,14 @@ -// swift-tools-version:5.2 +// swift-tools-version:5.5 import PackageDescription let package = Package( name: "VaporExample", platforms: [ - .macOS(.v10_15) + .macOS(.v12) ], dependencies: [ - .package(url: "https://github.com/vapor/vapor", .upToNextMajor(from: "4.7.0")), - .package(url: "https://github.com/mongodb/mongodb-vapor", .upToNextMajor(from: "1.0.0")){{#leaf}}, + .package(url: "https://github.com/vapor/vapor", .upToNextMajor(from: "4.50.0")), + .package(url: "https://github.com/mongodb/mongodb-vapor", .exact("1.1.0-alpha.1")){{#leaf}}, .package(url: "https://github.com/vapor/leaf", .upToNextMajor(from: "4.0.0")){{/leaf}} ], targets: [ @@ -26,7 +26,7 @@ let package = Package( .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)) ] ), - .target(name: "Run", dependencies: [ + .executableTarget(name: "Run", dependencies: [ .target(name: "App"), .product(name: "MongoDBVapor", package: "mongodb-vapor") ]), diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index d2ee781..4f9e221 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -12,26 +12,24 @@ struct Kitten: Content { extension Request { /// Convenience accessor for the home.kittens collection. var kittenCollection: MongoCollection { - self.mongoDB.client.db("home").collection("kittens", withType: Kitten.self) + self.application.mongoDB.client.db("home").collection("kittens", withType: Kitten.self) } } func routes(_ app: Application) throws { // A GET request will return a list of all kittens in the database.{{#leaf}} - app.get { req -> EventLoopFuture in{{/leaf}}{{^leaf}} - app.get { req -> EventLoopFuture<[Kitten]> in{{/leaf}} - req.kittenCollection.find().flatMap { cursor in - cursor.toArray() - }{{#leaf}}.flatMap { kittens in - req.view.render("index.leaf", ["kittens": kittens]) - }{{/leaf}} + app.get { req async throws -> View in{{/leaf}}{{^leaf}} + app.get { req async throws -> [Kitten] in{{/leaf}}{{#leaf}} + let kittens = try await req.kittenCollection.find().toArray() + return try await req.view.render("index.leaf", ["kittens": kittens]){{/leaf}}{{^leaf}} + try await req.kittenCollection.find().toArray(){{/leaf}} } // A POST request will create a new kitten in the database. - app.post { req -> EventLoopFuture in + app.post { req async throws -> Response in var newKitten = try req.content.decode(Kitten.self) newKitten.createdAt = Date() - return req.kittenCollection.insertOne(newKitten) - .map { _ in Response(status: .created) } + try await req.kittenCollection.insertOne(newKitten) + return Response(status: .created) } }