mirror of
https://github.com/mitchell/shortnr.git
synced 2025-12-19 14:07:22 +00:00
Impl. config based dep. injection for ets;
- Check for extant url ids - Various minor refactors
This commit is contained in:
parent
fe413d2a5e
commit
50de5607d6
12 changed files with 90 additions and 58 deletions
|
|
@ -1,29 +0,0 @@
|
|||
defmodule Shortnr.URL.Repo.DETS do
|
||||
@behaviour Shortnr.URL.Repo
|
||||
|
||||
@impl true
|
||||
def get(key) do
|
||||
case :dets.lookup(:urls, key) |> List.first() do
|
||||
{_, url} -> {:ok, url}
|
||||
nil -> {:ok, nil}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def put(url) do
|
||||
:ok = :dets.insert(:urls, {url.id, url})
|
||||
:ok
|
||||
end
|
||||
|
||||
@impl true
|
||||
def list() do
|
||||
resp = :dets.select(:urls, [{:"$1", [], [:"$1"]}])
|
||||
{:ok, resp |> Enum.map(&elem(&1, 1))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def delete(key) do
|
||||
:ok = :dets.delete(:urls, key)
|
||||
:ok
|
||||
end
|
||||
end
|
||||
36
lib/url/repo/ets.ex
Normal file
36
lib/url/repo/ets.ex
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
defmodule Shortnr.URL.Repo.ETS do
|
||||
@behaviour Shortnr.URL.Repo
|
||||
|
||||
@impl true
|
||||
def get(key) do
|
||||
case ets().lookup(:urls, key) |> List.first() do
|
||||
{_, url} -> {:ok, url}
|
||||
nil -> {:ok, nil}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def put(url) do
|
||||
:ok = ets().insert(:urls, {url.id, url})
|
||||
:ok
|
||||
end
|
||||
|
||||
@impl true
|
||||
def list() do
|
||||
resp = ets().select(:urls, [{:"$1", [], [:"$1"]}])
|
||||
{:ok, resp |> Enum.map(&elem(&1, 1))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def delete(key) do
|
||||
:ok = ets().delete(:urls, key)
|
||||
:ok
|
||||
end
|
||||
|
||||
@impl true
|
||||
def reset do
|
||||
:ok = ets().delete_all_objects(:urls)
|
||||
end
|
||||
|
||||
defp ets, do: Application.fetch_env!(:shortnr, :ets_implementation)
|
||||
end
|
||||
|
|
@ -6,4 +6,5 @@ defmodule Shortnr.URL.Repo do
|
|||
@callback get(String.t()) :: {:ok, URL.t()} | Transport.error()
|
||||
@callback delete(String.t()) :: :ok | Transport.error()
|
||||
@callback list() :: {:ok, list(URL.t())} | Transport.error()
|
||||
@callback reset() :: :ok | Transport.error()
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,16 +10,23 @@ defmodule Shortnr.URL do
|
|||
|
||||
@type t :: %__MODULE__{
|
||||
id: String.t(),
|
||||
url: URI.t(),
|
||||
created_at: DateTime.t(),
|
||||
updated_at: DateTime.t()
|
||||
updated_at: DateTime.t(),
|
||||
url: URI.t()
|
||||
}
|
||||
|
||||
@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)}
|
||||
:ok = repo.put(url_struct)
|
||||
{:ok, url_struct.id}
|
||||
|
||||
{:ok, extant_url} = repo.get(url_struct.id)
|
||||
|
||||
if is_nil(extant_url) do
|
||||
:ok = repo.put(url_struct)
|
||||
{:ok, url_struct.id}
|
||||
else
|
||||
create(url, repo)
|
||||
end
|
||||
end
|
||||
|
||||
@spec get(String.t(), module()) :: {:ok, URL.t()} | Transport.error()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue