Preparations

The preparations here are straightforward. All we need is a class that will register the correct routes and connects them to our internal functions.

Go through the following steps:

  1. Write down the following code in the App/Sources/Controllers/UsersController.swift file:
import Fluent
import Vapor

final class UsersController: RouteCollection {

func boot(routes: RoutesBuilder) {
}

func profile(_ request: Request) throws ->
EventLoopFuture<UserSuccessResponse> {
}

func save(_ request: Request) throws ->
EventLoopFuture<UserSuccessResponse> {
}

func delete(_ request: Request) throws ->
EventLoopFuture<HTTPStatus> {
}
}

The class is implementing RouteCollection as well so that we can register the routes accordingly. The three empty functions are representing the three routes we are about to register. Note that we are returning UserSuccessResponse whenever we are dealing with the user data. We have to return something else with the delete function as the user won't exist anymore; the HTTP status is a great way of confirming the deletion.

  1. Register the three routes in the boot function, as follows:
routes.get("profile", use: profile)
routes.post("profile", use: save)
routes.delete("user", use: delete)

Now, we are prepared and are ready to start writing the controller functions. Let's start with a profile.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.186.214