legendary-doc-site/apps/content/lib/content/options.ex

39 lines
666 B
Elixir
Raw Normal View History

defmodule Legendary.Content.Options do
2020-07-20 22:04:04 +00:00
@moduledoc """
Query the option key-value pairs for the site.
"""
alias Legendary.Content.Option
alias Legendary.Content.Repo
2020-07-20 22:04:04 +00:00
def put(key, value) do
%Option{}
|> Option.changeset(%{
2020-07-28 15:54:24 +00:00
name: key,
value: value,
})
|> Repo.insert()
end
2020-07-28 15:54:24 +00:00
def get(key), do: Option |> Repo.get_by(name: key)
2020-07-20 22:04:04 +00:00
def get_value(key) do
case get(key) do
nil ->
nil
opt ->
opt
2020-07-28 15:54:24 +00:00
|> (&(&1.value)).()
2020-07-20 22:04:04 +00:00
end
end
def get_value_as_int(key) do
case get_value(key) do
nil ->
{nil, nil}
opt ->
opt
|> Integer.parse()
end
end
end