shortnr/lib/url/url.ex

49 lines
1.2 KiB
Elixir
Raw Normal View History

2019-12-08 18:44:04 +00:00
defmodule Shortnr.URL do
alias Shortnr.Transport
alias Shortnr.URL
alias Shortnr.URL.Util
2019-12-08 18:44:04 +00:00
defstruct id: "",
2019-12-08 18:44:04 +00:00
created_at: DateTime.utc_now(),
updated_at: DateTime.utc_now(),
url: %URI{}
@type t :: %__MODULE__{
id: String.t(),
url: URI.t(),
created_at: DateTime.t(),
updated_at: DateTime.t()
}
2019-12-08 18:44:04 +00:00
@spec create(String.t(), module()) :: {:ok, String.t()} | Transport.error()
def create(url, repo) do
url_struct = %URL{id: Util.gen_id(), url: URI.parse(url)}
2019-12-08 18:44:04 +00:00
:ok = repo.put(url_struct)
{:ok, url_struct.id}
end
@spec get(String.t(), module()) :: {:ok, URL.t()} | Transport.error()
def get(key, repo) do
{:ok, _} = repo.get(key)
end
@spec list(module()) :: {:ok, list(URL.t())} | Transport.error()
def list(repo) do
{:ok, _} = repo.list
end
@spec delete(String.t(), module()) :: {:ok, :ignore} | Tranpsport.error()
def delete(key, repo) do
:ok = repo.delete(key)
{:ok, "Success"}
end
2019-12-08 18:44:04 +00:00
defimpl String.Chars do
alias Shortnr.URL
@spec to_string(URL.t()) :: String.t()
def to_string(url) do
"id=#{url.id} created=#{url.created_at} updated=#{url.updated_at} url=#{url.url}"
end
end
end