NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

Server Side Swift Vapor Edition: Installing Vapor 3

Forums > Books

Is it possible to install an older version of Vapor?

I got through chapter1, converting everything to run on Vapor 4 but it is a real pain as too much has changed, my proficiency with swift and the fact that there is no Leaf documentation available are just to much of an up-hill battle.

I got through chapter1 if someone wants the code.

1      

@twostraws  Site AdminHWS+

I really want to update the book, but Leaf just seems to be on fire right now 😔

1      

I tried this a year ago and gave up in frustration. Now, with a year's experience of following along with the UltimatePortfolio project as well as some tinkering on my own projects, I'm taking a second run at this. The Leaf syntax has changed quite a bit, but despite the shortcomings of the Leaf documentation, it's been pretty easy to figure out. Project 1 was not too bad, so I'll keep going.

1      

Argh, I've just run up against the limits of my understanding of the Swift compiler's error messages and Swift syntax. Right at the end of project 2, we're building the voting endpoint. This is totally clear, except that this code:

    app.post("polls", "vote", ":id", ":option") { req -> EventLoopFuture<Poll> in
        let pollID = req.parameters.get("id", as: UUID.self)
        let option = req.parameters.get("option", as: Int.self)
        return try Poll.find(pollID, on: req.db).flatMap(to: Poll.self) { poll in
            guard var poll = poll else {
                throw Abort(.notFound)
            }
            if option == 1 {
                poll.votes1 += 1
            } else {
                poll.votes2 += 1
            }
            return poll.save(on: req.db)
        }
    }

generates a compiler error on the line with the flatMap call. The error is: Cannot convert value of type 'Poll.Type' to expected argument type '(Poll?) -> EventLoopFuture<Poll>'

I've been trying different type declarations and different ways of declaring that trailing closure, but all that I've achieved is getting slightly different compiler errors and they all still complain about not being able to transform the closure's signature. Any help?

1      

Ah HA! The problem was really on the last line of the closure, since the Fluent save method returns a EventLoopFuture<Void>. Here's a vote method that actually compiles:

    app.post("polls", "vote", ":id", ":option") { req -> EventLoopFuture<Poll> in
        let pollID = req.parameters.get("id", as: UUID.self)
        let option = req.parameters.get("option", as: Int.self)
        return Poll.find(pollID, on: req.db)
            .unwrap(or: Abort(.notFound))
            .flatMap { poll in
                if option == 1 {
                    poll.votes1 += 1
                } else {
                    poll.votes2 += 1
                }
                return poll.save(on: req.db).map { return poll }
            }
    }

2      

Any plans to update this book? Would really like to learn more Vapor. There are some cool projects in that book!!!

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.