Skip to content

Commit

Permalink
Add function for get_card to return 404 on DELETE with bad ID
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Jun 25, 2019
1 parent 7da8c9b commit e73f8aa
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/stripe_mock/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule StripeMock.API do

# Cards
defdelegate list_cards(customer), to: Ops.Card
defdelegate get_card(id), to: Ops.Card
defdelegate get_card!(id), to: Ops.Card
defdelegate create_card(customer, attrs \\ %{}), to: Ops.Card
defdelegate create_customer_card_from_source(customer, source, metadata \\ %{}), to: Ops.Card
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe_mock/api/operations/card.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule StripeMock.API.Operations.Card do
|> Enum.filter(&(&1.customer_id == customer.id))
end

def get_card(id), do: Repo.get(Card, id)
def get_card(id), do: Repo.fetch(Card, id)
def get_card!(id), do: Repo.get!(Card, id)

def create_card(customer, attrs) do
Expand Down
8 changes: 1 addition & 7 deletions lib/stripe_mock/api/operations/customer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ defmodule StripeMock.API.Operations.Customer do
Repo.all(Customer)
end

def get_customer(id) do
case Repo.get(Customer, id) do
nil -> {:error, :not_found}
customer -> {:ok, customer}
end
end

def get_customer(id), do: Repo.fetch(Customer, id)
def get_customer!(id), do: Repo.get!(Customer, id)

def create_customer(attrs \\ %{}) do
Expand Down
5 changes: 2 additions & 3 deletions lib/stripe_mock_web/controllers/source_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ defmodule StripeMockWeb.SourceController do
end

def delete(conn, %{"id" => id}) do
card = API.get_card!(id)

with {:ok, source} <- API.delete_card(card) do
with {:ok, card} <- API.get_card(id),
{:ok, source} <- API.delete_card(card) do
render(conn, "show.json", source: source)
end
end
Expand Down

0 comments on commit e73f8aa

Please sign in to comment.