Skip to content

Commit

Permalink
stateful server 1
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 26, 2022
1 parent 41eea20 commit 0509fe9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/pledge_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Servy.PledgeController do
alias Servy.Conv
alias Servy.PledgeServer

def create(conv, %{"name" => name, "amount" => amount}) do
# sends pledge to the external service and caches it
PledgeServer.create_pledge(name, String.to_integer(amount))

%{conv | status: 201, resp_body: "#{name} pledged #{amount}!"}
end

def index(conv) do
# get recent pledges from cache
pledges = PledgeServer.recent_pledges()

%{Conv | status: 200, resp_body: inspect(pledges)}
end
end
10 changes: 10 additions & 0 deletions lib/pledge_server.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Servy.PledgeServer do
def create_pledge(name, amount) do
{:ok, id} = send_pledge_to_service(name, amount)
end

defp send_pledge_to_service(_name, _amount) do
# CODE GOES HERE TO SEND PLEDGE TO EXTERNAL SERVICE
{:ok, "pledge-#{:rand.uniform(1000)}"}
end
end

0 comments on commit 0509fe9

Please sign in to comment.