Scaffold Our Shopping Cart Channel (Part I)
Start developing a shopping cart for our application.
We'll cover the following
Shopping cart
We’ll start developing our shopping cart by writing code that the Channel will use—our functional core. After we build our functional core, we’ll use it to develop a ShoppingCartChannel
. We’ll start small and work our way up to a complete cart by the end of this section.
It’s essential to build a functional core that contains logic, data structures, or other parts of a program independent of the user interface. This helps increase the maintainability of our code because the separation between interface and logic means that either can be changed without a complete rewrite of the application. We’ll have an easier time adapting to change and adding new features when our application is split into separate parts this way.
Earlier, our ProductChannel
accessed inventory data through an Inventory
context—this was our functional core. We’re going to build something very similar in this section—we’ll write a ShoppingCart
data structure that holds cart data. We’ll add this code to a Checkout
context so that our ShoppingCartChannel
can use it without reaching into the context.
Let’s jump into the Checkout
context, followed by our ProductController
. We’ll build the ShoppingCartChannel
in the next lesson.
Build a functional core
When coding a new feature, most people find it helpful to start with the most central part. For us, our entire feature revolves around the concept of a shopping cart, so this is a great place to start writing code. According to our requirements, a shopping cart is a collection of items that can be added to and removed from. Let’s represent this as an Elixir struct.
Create the Checkout.ShoppingCart
module and add the following code:
Get hands-on with 1400+ tech skills courses.