25/lib/mse25_web/controllers/item_controller.ex
Anders Englöf Ytterström a6d34d17fc
List and show events (#17)
* Improve Directus client

- Rearrange and group functions to match
- Allow whitespace when searching
- provide category to event list

* Add event list view

* Show band information on festival-ish events

* Extract view helpers to reusable module

Code is shared between ItemHTML and PageHTML,
and they really do not belong in web anyway
since it is usable outside web scope as well.

* Rename utils to event helpers

It is a file that handles the poor design choice
of the datamodel for events, so let the module
name describe that.

* Sync event view

- Replace scaffold markup with production markup
- Send more data from Directus client
2024-10-03 22:52:40 +02:00

125 lines
2.9 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
[
page_title: heading,
heading: heading,
contents: Earmark.as_html!(contents),
published_at: published_at,
updated_at:
case updated_at do
nil -> published_at
ua -> String.slice(ua, 0..9)
end,
year: String.slice(published_at, 0..3)
]
end
defp assigns(:event, %{
"title" => heading,
"contents" => contents,
"started_at" => started_at,
"lead" => lead,
"poster" => poster,
"bands" => bands,
"mia" => mia,
"category" => category
}) do
[
page_title: heading,
heading: heading,
contents: Earmark.as_html!(contents),
lead: lead,
year: String.slice(started_at, 0..3),
poster: poster,
bands: bands,
mia: mia,
category: category
]
end
defp assigns(:link, %{
"title" => heading,
"contents" => contents,
"pubDate" => published_at,
"date_updated" => updated_at,
"source" => url,
"h1" => title
}) do
[
page_title: heading,
heading: heading,
contents: Earmark.as_html!(contents),
published_at: published_at,
url: url,
title: title,
year: String.slice(published_at, 0..3),
updated_at:
case updated_at do
nil -> published_at
ua -> String.slice(ua, 0..9)
end
]
end
defp assigns(:page, %{
"title" => heading,
"contents" => contents,
"date_updated" => updated_at
}) do
[
heading: heading,
contents: Earmark.as_html!(contents),
updated_at: String.slice(updated_at, 0..9)
]
end
end