UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

4      

@twostraws  Site AdminHWS+

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

4      

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.

4      

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?

4      

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 }
            }
    }

5      

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

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.