Creating our routes

Like the other services, our OMS needs to be callable publicly. We do that by setting up routes that allow the user, and other services, to call this service. The routes we set up in this section will do this for us.

This service does most of its work behind the scenes and not publicly. For the end user (or consumer), the following two actions are important:

  • Submit a new order.
  • Retrieve the history of orders.

For an administrator, we need to do the following:

  • Get the list of all orders
  • Add payment to an order

The following table list the orders:

strong>Path strong>Parameters Output/Description
/*/orders (Auth required) POST Expected Total Order Items Submits an order from an end-user/consumer
/*/orders (Auth required) GET - Returns all orders of the user from the JWT
/*/orders (Auth required, admin level) GET - Returns all users
/*/orders/payment/:id POST Payment Information Submits a payment for the order

 

So, there are not a whole lot of routes. Let's get them implemented. Follow these steps:

  1. Go to Sources/App and open Routes.swift.
  2. Enter the following code:
import Fluent
import Vapor
import SimpleJWTMiddleware

func routes(_ app: Application) throws {

let currentVersion = app.grouped(.anything, "orders")

currentVersion.get("health") { req in
return "All good."
}

let protected = currentVersion.grouped(SimpleJWTMiddleware())

let orderController = OrderController()
protected.post("", use: orderController.post)
protected.get("", use: orderController.listMine)
protected.get("all", use: orderController.list)
protected.post("payment", ":id",
use: orderController.postPayment)
}

The first lines should be very familiar to you. We are then utilizing JWTMiddleware to protect our routes. In this service, we have no public routes. After that, we define the routes from the preceding table. The code will do nothing but register the routes when executed, but it will tell the Vapor Router where to direct calls that come to specific URL endpoints.

Alright, we have created the routes that allow the user to call this service. For this service to start working, we need to work on the controllers and the models. The controllers will need the models to work because the models will contain all our information. So let's work on the models.

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

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