Code review needed: creating multiple records sharing some values on one API call

You might want to separate the Multi related code to a different function for code reuse. For example:

def validations(record_changeset, book_id, borrower_id) do
  Multi.new()
      |> Mango.Records.Validators.valid_book_exists_multi(book_id)
      |> Mango.Records.Validators.valid_book_belongs_to_current_user_and_borrower_multi(book_id, user, borrower_id)
      |> Mango.Records.Validators.valid_last_record_closed_multi(book_id)
      |> Multi.insert(:record, record_changeset)
end

def create_record(%{"user_object" => user, "borrower_id" => borrower_id} = attrs, book_list) do
  book_list = String.split(book_list, ",")
  result = []
  for book_id <- book_list do
    book_id = book_id |> String.to_integer
    record_changeset = Mango.Records.Record.changeset(%Record{user_id: user.id, book_id: book_id}, attrs)
    case Repo.transaction(validations(record_changeset, book_id, borrower_id)) do
      {:ok, :first_record} -> {:ok}
      {:ok, %{record: record}} -> result = result ++ record
      {:error, :book_belongs_to_current_user_and_borrower, reason, _changes} -> result = result ++ reason
      {:error, :book_exists, reason, _changes} -> result = result ++ reason
      {:error, :last_record_closed, reason, _changes} -> result = result ++ reason
    end
  end
end