25/lib/mse25_web/controllers/item_controller.ex
Anders Englöf Ytterström 8bbe8a1b24
Item views (#7)
* Add Earmark dependency

For markdown to HTML conversion

* Display items by slug: articles, pages

* Handle 401 Forbidden responses in Directus

* Add item controller as pokemon route

* Strip out generated layouts

* Plan future routes

* Look for item in order: article, link, event, album

* Add item templates
2024-09-06 22:41:12 +02:00

100 lines
2.3 KiB
Elixir

defmodule Mse25Web.ItemController do
use Mse25Web, :controller
alias Mse25.Directus
def index(conn, _params) do
case conn.path_info |> fetch do
{:ok, item_type, item_data} ->
render(conn, item_type, assigns(item_type, item_data))
{:not_found, message} ->
render(conn, message)
end
end
defp fetch([year, slug], :article) do
case Directus.get_article(year <> "/" <> slug) do
{:ok, response} -> {:ok, :article, response}
_ -> fetch([year, slug], :link)
end
end
defp fetch([year, slug], :link) do
case Directus.get_link(year <> "/" <> slug) do
{:ok, response} -> {:ok, :link, response}
_ -> fetch([year, slug], :event)
end
end
defp fetch([year, slug], :event) do
case Directus.get_event(year <> "/" <> slug) do
{:ok, response} -> {:ok, :event, response}
not_found -> not_found
end
end
defp fetch([year, slug]) do
fetch([year, slug], :article)
end
defp fetch([slug]) do
case Directus.get_page(slug) do
{:ok, response} -> {:ok, :page, response}
not_found -> not_found
end
end
defp assigns(:article, %{
"title" => heading,
"contents" => contents,
"pubDate" => published_at,
"date_updated" => updated_at
}) do
[
heading: heading,
contents: Earmark.as_html!(contents),
published_at: published_at,
updated_at: updated_at
]
end
defp assigns(:event, %{
"title" => heading,
"contents" => contents,
"started_at" => published_at
}) do
[
heading: heading,
contents: Earmark.as_html!(contents),
published_at: published_at
]
end
defp assigns(:link, %{
"title" => heading,
"contents" => contents,
"pubDate" => published_at,
"source" => url,
"h1" => title
}) do
[
heading: heading,
contents: Earmark.as_html!(contents),
published_at: published_at,
url: url,
title: title
]
end
defp assigns(:page, %{
"title" => heading,
"contents" => contents,
"date_updated" => updated_at
}) do
[
heading: heading,
contents: Earmark.as_html!(contents),
updated_at: updated_at
]
end
end