Server Side Swift with Vapor - Part 11: Challenge: | Ray Wenderlich

Now it's time to put all of your new knowledge to the test and create your own models and controllers.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/11

This seems like boilerplate code where we should be able to make a CrudController struct to inherit from. If I define this though:

struct CrudController<T : SQLiteUUIDModel & Parameter> : RouteCollection {
    func getHandler(_ req: Request) throws -> Future<T> {
        return try req.parameter(T.self)
    }
}

then I get an error that it can’t convert T.ResolvedParameter to Future. Can you explain why the parameter call is suddenly returning a different type?

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

@gargoyle T.ResolvedParameter is actually Future<Model> for a model due to the way the parameter system works. So the code should look like:

struct CrudController<T : SQLiteUUIDModel & Parameter> : RouteCollection {
    func getHandler(_ req: Request) throws -> T.ResolvedParameter {
        return try req.parameter(T.self)
    }
}

There is a RestController implementation coming that will implement all of the REST methods for you, it just hasn’t been implemented yet in the beta

@0xtim Why did you use flatMap in the User and Category create handlers instead of await?

@dpenny await is a bit of a stop-gap before you learn about Futures. It isn’t very reliable and can cause your app to get stuck in an endless loop - though this may all change when NIO hits. So really you should deal with Futures properly but I used await to start with to not confuse people too much!