Throughout the book, the examples deal mostly with creating data. When the quiz runs, it always persists new answers.
Let’s say I want to add UI for creating quizzes, and it has ability to edit templates for the quiz. How would you design the core API for that function? I came up with something like this:
@JEG2 I wonder what would you add as a key in that map. Let’s say I’ve added UUID to the template, and I generate one in Template.new:
def new(template_fields) do
template_fields =
template_fields
|> Keyword.merge(uuid: UUID.generate())
struct!(%__MODULE__{}, template_fields)
end
Then, if the map is %{uuid => template} it doesn’t help a lot. I still need to extract the template uuid.
Alternatively, I could try not generating UUID in the new function, then test builders would normally create the UUID as other fields, so the functions would be of shape.
def edit_template(quiz_builder, template_uuid, template_fields)
# previously, it was `template` struct instead of `temlate_uuid`
Now, there is another problem: where do I generate the UUID? I don’t think I want API user to pass the UUID every time when they create new template. Is it OK to generate the UUID in the API layer?