Skip to content

Commit

Permalink
SWIFT-1396 Update Vapor template for async/await (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmahar authored Jan 28, 2022
1 parent 8c4763e commit bca8ac2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -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")
]),
Expand Down
20 changes: 9 additions & 11 deletions Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,24 @@ struct Kitten: Content {
extension Request {
/// Convenience accessor for the home.kittens collection.
var kittenCollection: MongoCollection<Kitten> {
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<View> 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<Response> 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)
}
}

0 comments on commit bca8ac2

Please sign in to comment.