Shopping List controller

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:

  1. Create a new Swift file in the Controllers folder and name it ShoppingListController.swift
  2. 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 ...

Get Hands-On Full-Stack Development with Swift 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.