We now have a working API route for the Items Model. To complete our API server, we need to create a controller for ShoppingList, which will be resourceful, just like ItemController. To create a controller for ShoppingList, we need to perform the following steps:
- Create a new Swift file in the Controllers folder and name it ShoppingListController.swift
- Add the following code to the empty file:
import Vaporimport HTTPfinal class ShoppingListController: ResourceRepresentable { func index(_ req: Request) throws -> ResponseRepresentable { return try ShoppingList.all().makeJSON() } func store(_ req: Request) throws -> ResponseRepresentable { let shoppingList = try req.shoppingList() try shoppingList.save() return shoppingList ...