Item Model

Great, now we have a working ShoppingList Model, but it won't compile yet since it depends on the Item Model as our ShoppingList has many items. To fix this, we need to create an Item Model similar to the ShoppingList Model. To do so, we need to follow these steps:

  1. Just like ShoppingList.swift, create a new file called Item.swift inside of the Models folder and make sure  App target is checked otherwise you might get a compile error.
  2. Copy the following code for the Item.swift class:
import Vaporimport FluentProviderimport HTTPfinal class Item: Model {  let storage = Storage()    var name: String  var isChecked: Bool = false  var shoppingListId: Identifier  var list: Parent<Item, ShoppingList> {    return parent(id: shoppingListId)  }

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.