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:
- Open ProductsController.swift in Sources/App/Controllers.
- 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.
- 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 ...