Current section
Files
Jump to
Current section
Files
priv/templates/sims.gen.http_crud/simulator/state_server.ex.eex
defmodule <%= inspect @module %> do
@moduledoc false
use Agent
alias <%= inspect @module %>.State
def start_link(_opts \\ []) do
Agent.start_link(fn -> State.new() end)
end
def get_state(state_server) do
Agent.get(state_server, & &1)
end
def create_account(state_server) do
Agent.get_and_update(state_server, fn state ->
case State.create_account(state) do
{:ok, {account, state}} -> {{:ok, account}, state}
end
end)
end
def fetch_account_from_credentials(server, account_id, auth_token) do
server
|> get_state()
|> State.fetch_account_from_credentials(account_id, auth_token)
end
def create_<%= @model.name %>(state_server, account_id, fields) do
Agent.get_and_update(state_server, fn state ->
case State.create_<%= @model.name %>(state, account_id, fields) do
{:ok, {<%= @model.name %>, state}} -> {{:ok, <%= @model.name %>}, state}
end
end)
end
def fetch_<%= @model.name %>(server, account_id, <%= @model.name %>_id) do
server
|> get_state()
|> State.fetch_<%= @model.name %>(account_id, <%= @model.name %>_id)
end
def list_<%= @model.plural %>(server, account_id) do
server
|> get_state()
|> State.list_<%= @model.plural %>(account_id)
end
def update_<%= @model.name %>(state_server, account_id, <%= @model.name %>_id, fields) do
Agent.get_and_update(state_server, fn state ->
case State.update_<%= @model.name %>(state, account_id, <%= @model.name %>_id, fields) do
{:ok, {<%= @model.name %>, state}} -> {{:ok, <%= @model.name %>}, state}
{:error, :not_found} -> {{:error, :not_found}, state}
end
end)
end
def delete_<%= @model.name %>(state_server, account_id, <%= @model.name %>_id) do
Agent.get_and_update(state_server, fn state ->
case State.delete_<%= @model.name %>(state, account_id, <%= @model.name %>_id) do
{:ok, state} -> {:ok, state}
{:error, :not_found} -> {{:error, :not_found}, state}
end
end)
end
<%= if @simulator.options.response_stubs? do %>
def get_response_stub(server, route_id) do
server
|> get_state()
|> State.get_response_stub(route_id)
end
def stub_response(server, route_id, response_id) do
Agent.update(server, &State.stub_response(&1, route_id, response_id))
end
def clear_stubbed_responses(server) do
Agent.update(server, &State.clear_stubbed_responses(&1))
end
<% end %>
end