Connecting response models to the database model

Now that we have our response models ready, we can connect them to the models behind them.

Create the models by following these steps:

  1. Open UserResponse.swift and add the following function to the class:
init(user: User, addresses: [AddressResponse]? = nil) {
self.id = user.id
self.firstname = user.firstname
self.lastname = user.lastname
self.email = user.email
self.addresses = addresses
}

The function takes in a user and, optionally, addresses to assign them to its properties.

  1. Now, include the following function in AddressResponse.swift:
init(_ address: Address) {
self.id = address.id
self.street = address.street
self.city = address.city
self.zip = address.zip
self.userId = address.userId
self.createdAt = address.createdAt
self.updatedAt = address.updatedAt
self.deletedAt = address.deletedAt
}

The function takes an address and assigns its values to the response's values.

We have now created and connected all models, so let's start writing the controllers to make our service functional!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.157.248