Last active
July 11, 2018 07:51
-
-
Save Ninigi/6d820235be97b0fc0388cd13f3c288b3 to your computer and use it in GitHub Desktop.
This file contains 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
@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