Writing ProductsController

The ProductsController is similar in nature to the CategoriesController but has a very different function to return a list of products. We want to be able to filter products based on their name, category, or ID. But let's get started with the basics, as follows:

  1. Open ProductsController.swift in Sources/App/Controllers.
  2. Enter the empty class into that file, as follows:
import Fluentimport Vaporfinal class ProductsController {}

So far, so good. Now, let's get started with the main function.

  1. Enter the following function into the class:
func get(_ request: Request)throws -> EventLoopFuture<[ProductResponse]> {        let querybuilder = Product.query(on: request.db)                return querybuilder.all().map { products in return products.map ...

Get Hands-On Swift 5 Microservices Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.