Build The Survey Context

Let's learn how to build backend services in Phoenix.

Before we can create the LiveView and components that represent the survey feature, we need to build out the backend services that will support them. We’ll design a Survey context, with schemas for Demographic and Rating. Then, we’ll be able to use the Survey context in our LiveView.

We’ll take a slightly different approach to build the context and schemas than the one we saw in the previous lessons. We’ll still rely on code generation, this time reaching for the phx.gen.context generator to build just a context and schemas, rather than the Phoenix Live generator that also creates LiveViews and routes. This is because we’ll be creating our own custom LiveView and components to handle the survey functionality later on. We’re building a LiveView frontend with specific behaviors and features that the Phoenix Live generator just won’t accommodate.

We’ll begin by running the generator, but we’ll need to do a little bit of customization on top of the generated code in order to get our data into the correct shape. When we’re done with this section, we’ll know how to strategically deploy the Phoenix Context generator to build the foundation of a custom feature set. We’ll also be comfortable adding our own code on top of the generated code, and we’ll be prepared to use our new context in LiveView to build-out the dynamic, interactive survey.

Get hands-on with 1200+ tech skills courses.