Skip to content

Commit

Permalink
stateful server 3 (fixed)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 26, 2022
1 parent 00e1f06 commit f578de4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
15 changes: 6 additions & 9 deletions lib/pledge_controller.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
defmodule Servy.PledgeController do
alias Servy.Conv
alias Servy.PledgeServer

def create(%Conv{} = conv, %{"name" => name, "amount" => amount}) do
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))
Servy.PledgeServer.create_pledge(name, String.to_integer(amount))

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

def index(%Conv{} = conv) do
def index(conv) do
# get recent pledges from cache
pledges = PledgeServer.recent_pledges()
pledges = Servy.PledgeServer.recent_pledges()

%{Conv | status: 200, resp_body: inspect(pledges)}
%{conv | status: 200, resp_body: inspect(pledges)}
end
end
18 changes: 9 additions & 9 deletions lib/pledge_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ defmodule Servy.PledgeServer do
# code to send pledge to an external service
{:ok, "pledge-#{:rand.uniform(1000)}"}
end
end

# alias Servy.PledgeServer
alias Servy.PledgeServer

# pid = PledgeServer.start()
pid = PledgeServer.start()

# IO.inspect(PledgeServer.create_pledge("larry", 10))
# IO.inspect(PledgeServer.create_pledge("moe", 20))
# IO.inspect(PledgeServer.create_pledge("curly", 30))
# IO.inspect(PledgeServer.create_pledge("daisy", 40))
# IO.inspect(PledgeServer.create_pledge("grace", 50))
IO.inspect(PledgeServer.create_pledge("larry", 10))
IO.inspect(PledgeServer.create_pledge("moe", 20))
IO.inspect(PledgeServer.create_pledge("curly", 30))
IO.inspect(PledgeServer.create_pledge("daisy", 40))
IO.inspect(PledgeServer.create_pledge("grace", 50))

# IO.inspect(PledgeServer.recent_pledges())
end
IO.inspect(PledgeServer.recent_pledges())

0 comments on commit f578de4

Please sign in to comment.