...

/

Building the API Layer

Building the API Layer

Let’s learn how to build the API layer and discuss the importance of incorporating this layer into our project.

Our API layer will name the concepts of the GenServer and smooth out some of the rough edges. We’ll build a lightweight API that uses the GenServer module to do the following:

  • starts

  • calls

  • casts

Declaring the module and aliases

The first step is to do the typical imports we need. In /lib/mastery.ex, we’ll delete the default implementation and set up the aliases we need:

Press + to interact
defmodule Mastery do
alias Mastery.Boundary.{QuizSession, QuizManager}
alias Mastery.Boundary.{TemplateValidator, QuizValidator}
alias Mastery.Core.Quiz

If possible, we’d build a service layer where the only functions we need are in the service layer. However, we’ll have to compromise on that and use the following headings:

  • We have to manage the validations, so we’ll add those aliases as well. Validation belongs here because we want to reduce the need for dealing with the uncertainty of the outside world ...