Add Endpoints behaviour and refactor router and related methods

This commit is contained in:
mitchell 2020-01-05 14:54:55 -05:00
parent 1502d10cdc
commit 98684feb2d
8 changed files with 98 additions and 62 deletions

View file

@ -2,10 +2,11 @@ defmodule Shortnr.URL.Repo.ETS do
@moduledoc """
This module is an implemention of the Repo behaviour using the Erlang ETS library.
"""
alias Shortnr.URL.Repo
@behaviour Shortnr.URL.Repo
@behaviour Repo
@impl true
@impl Repo
def get(key) do
case ets().lookup(:urls, key) |> List.first() do
{_, url} -> {:ok, url}
@ -13,25 +14,25 @@ defmodule Shortnr.URL.Repo.ETS do
end
end
@impl true
@impl Repo
def put(url) do
:ok = ets().insert(:urls, {url.id, url})
:ok
end
@impl true
@impl Repo
def list do
resp = ets().select(:urls, [{:"$1", [], [:"$1"]}])
{:ok, resp |> Enum.map(&elem(&1, 1))}
end
@impl true
@impl Repo
def delete(key) do
:ok = ets().delete(:urls, key)
:ok
end
@impl true
@impl Repo
def reset do
:ok = ets().delete_all_objects(:urls)
end