parent
499c5e5cc2
commit
83bf6ce558
@ -0,0 +1,39 @@
|
||||
defmodule LinkShortener.Factories.LinkFactory do
|
||||
use ExMachina
|
||||
|
||||
alias LinkShortener.Links
|
||||
alias LinkShortener.Factories.UserFactory
|
||||
|
||||
def link_attrs_factory() do
|
||||
%{
|
||||
name: sequence(:name, &"name-#{&1}"),
|
||||
url: "https://gitlab.com/KKlochko/link_shortener",
|
||||
shorten: sequence(:shorten, &"shorten#{&1}"),
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Create a link that has a static url, a random name and a random shorten.
|
||||
If there are no user_id in attrs, then a new user will be created.
|
||||
"""
|
||||
def create_link(attrs \\ %{}) do
|
||||
{:ok, link} =
|
||||
link_attrs_factory()
|
||||
|> put_new_user_id()
|
||||
|> Map.merge(attrs)
|
||||
|> Links.create_one()
|
||||
|
||||
link
|
||||
end
|
||||
|
||||
defp put_new_user_id(attrs) do
|
||||
if Map.has_key?(attrs, :user_id) do
|
||||
attrs
|
||||
else
|
||||
user = UserFactory.create_user()
|
||||
|
||||
attrs
|
||||
|> Map.put(:user_id, user.id)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue