Created
June 10, 2016 08:17
-
-
Save gogogarrett/09cebaeeabafb4bf41e88994fc070f8f to your computer and use it in GitHub Desktop.
Hopscotch like service and workflows in elixir.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Service.AwardAcorns do | |
alias ExSeeds.{Repo, Student} | |
def call(student, amount \\ 3) do | |
student | |
|> ExSeeds.Student.changeset(%{acorns: student.acorns + amount}) | |
|> Repo.update | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Service.EnsureEnoughAcorns do | |
def call(acorns, cost) when acorns >= cost do | |
{:ok, ""} | |
end | |
def call(_acorns, _cost) do | |
{:error, "not enough acorns"} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Service.CreateStudentEvent do | |
def call(map) do | |
case create_student_event(map) do | |
_event -> | |
{:ok, "event created."} | |
_ -> | |
{:error, "event could not be created."} | |
end | |
end | |
defp create_student_event(map) do | |
# ie. | |
# %{ | |
# student_id: student.id, | |
# precinct: 'arcade', | |
# event_type: 'ArcadeGame' | |
# } | |
# BlakeProgress.Service.create_student_event(map) | |
%{id: 1} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Workflow.Arcade.PlayGame do | |
alias ExSeeds.{Repo, Student} | |
def call(student, game) do | |
steps(student, game) | |
|> Workflow.Transaction.call | |
end | |
defp steps(student, game) do | |
fn -> | |
with {:ok, _} <- Service.EnsureEnoughAcorns.call(student.acorns, game.cost), | |
{:ok, _} <- deduct_acorns(student, game.cost), | |
{:ok, _} <- Service.CreateStudentEvent.call(%{id: 1}), | |
do: {:ok, "workflow successful"} | |
end | |
end | |
defp deduct_acorns(student, cost) do | |
Service.AwardAcorns.call(student, -cost) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Workflow.Transaction do | |
def call(function) do | |
ExSeeds.Repo.transaction(fn() -> | |
case function.() do | |
{:ok, result} -> result | |
{:error, error} -> ExSeeds.Repo.rollback(error) | |
end | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This approach highlights how this could possibly be done with an OTP application that could be included into a phoenix application.