Skip to content

Instantly share code, notes, and snippets.

@Ninigi
Last active July 11, 2018 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ninigi/6d820235be97b0fc0388cd13f3c288b3 to your computer and use it in GitHub Desktop.
Save Ninigi/6d820235be97b0fc0388cd13f3c288b3 to your computer and use it in GitHub Desktop.
@doc """
Creates a new shop if no shop with the given shopify_domain exists, or updates
the existing shop
## Examples
iex> create_or_update_shop(%{shopify_domain: "shop@my_shopify.com", shopify_token: "a-token"})
{:ok, %Shop{}}
iex> create_or_update_shop(%{shopify_domain: nil, shopify_token: "a-token"})
{:error, %Ecto.Changeset{}}
"""
def create_or_update_shop(%{shopify_domain: shopify_domain, shopify_token: _} = args) do
case find_shop_by(%{shopify_domain: shopify_domain}) do
nil -> create_shop(args)
shop -> update_shop(shop, args)
end
end
def create_or_update_shop(args) do
create_shop(args)
end
alias Useless.EctoQueries.ShopQuery
@doc """
Finds a shop by given arguments.
Returns nil if non found.
## Examples
iex> find_shop_by(%{shopify_domain: "an-existing-domain@myshopify.com"})
%Shop{}
iex> find_shop_by(%{shopify_domain: "does-not-exist"})
nil
"""
def find_shop_by(args) do
Shop
|> ShopQuery.query_shopify_domain(args)
|> Repo.one()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment