2024-09-06 22:41:12 +02:00
|
|
|
defmodule Mse25Web.ItemController do
|
|
|
|
|
use Mse25Web, :controller
|
|
|
|
|
alias Mse25.Directus
|
2024-10-04 13:09:50 +02:00
|
|
|
alias Mse25.Timeline
|
2024-09-06 22:41:12 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2024-10-04 13:15:27 +02:00
|
|
|
defp fetch([_year, album_id], :album) do
|
|
|
|
|
case Directus.get_album(album_id) do
|
|
|
|
|
{:ok, response} -> {:ok, :album, response}
|
|
|
|
|
not_found -> not_found
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-02-11 13:15:43 +01:00
|
|
|
defp fetch([_year, album_id], :note) do
|
|
|
|
|
case Directus.get_note(album_id) do
|
|
|
|
|
{:ok, response} -> {:ok, :note, response}
|
|
|
|
|
not_found -> not_found
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-04 13:15:27 +02:00
|
|
|
defp fetch([year, "brutal-legend-" <> external_id]) do
|
|
|
|
|
fetch([year, external_id], :album)
|
|
|
|
|
end
|
|
|
|
|
|
2024-09-06 22:41:12 +02:00
|
|
|
defp fetch([year, slug]) do
|
|
|
|
|
fetch([year, slug], :article)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp fetch([slug]) do
|
2024-10-04 13:09:50 +02:00
|
|
|
case Integer.parse(slug) do
|
2024-10-06 22:01:16 +02:00
|
|
|
:error ->
|
2024-10-04 13:09:50 +02:00
|
|
|
case Directus.get_page(slug) do
|
|
|
|
|
{:ok, response} -> {:ok, :page, response}
|
|
|
|
|
error -> error
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
{year, _} ->
|
|
|
|
|
case Timeline.annual(year) do
|
|
|
|
|
{:ok, response} -> {:ok, :annual, response}
|
|
|
|
|
error -> error
|
|
|
|
|
end
|
2024-09-06 22:41:12 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-04 13:09:50 +02:00
|
|
|
defp assigns(:annual, %{
|
|
|
|
|
timeline: timeline,
|
|
|
|
|
year: year,
|
|
|
|
|
counts: counts
|
|
|
|
|
}),
|
|
|
|
|
do: [
|
|
|
|
|
year: year,
|
|
|
|
|
page_title: "Innehåll från " <> to_string(year),
|
2024-10-16 15:40:53 +02:00
|
|
|
breadcrumbs: [{year, year}],
|
2024-10-04 13:09:50 +02:00
|
|
|
timeline: timeline,
|
|
|
|
|
brutal_legends_count: Map.get(counts, :albums, 0),
|
|
|
|
|
article_count: Map.get(counts, :articles, 0),
|
|
|
|
|
event_count: Map.get(counts, :events, 0),
|
|
|
|
|
link_count: Map.get(counts, :links, 0)
|
|
|
|
|
]
|
|
|
|
|
|
2024-09-06 22:41:12 +02:00
|
|
|
defp assigns(:article, %{
|
|
|
|
|
"title" => heading,
|
|
|
|
|
"contents" => contents,
|
|
|
|
|
"pubDate" => published_at,
|
|
|
|
|
"date_updated" => updated_at
|
|
|
|
|
}) do
|
2024-10-16 15:40:53 +02:00
|
|
|
year = String.slice(published_at, 0..3)
|
|
|
|
|
|
2024-09-06 22:41:12 +02:00
|
|
|
[
|
2024-10-03 14:38:56 +02:00
|
|
|
page_title: heading,
|
2024-10-16 15:40:53 +02:00
|
|
|
breadcrumbs: [{"webblogg", "Webblogg"}, {year, year, ""}],
|
2024-09-06 22:41:12 +02:00
|
|
|
heading: heading,
|
|
|
|
|
contents: Earmark.as_html!(contents),
|
|
|
|
|
published_at: published_at,
|
2024-10-03 14:38:56 +02:00
|
|
|
updated_at:
|
|
|
|
|
case updated_at do
|
|
|
|
|
nil -> published_at
|
|
|
|
|
ua -> String.slice(ua, 0..9)
|
|
|
|
|
end,
|
2024-10-16 15:40:53 +02:00
|
|
|
year: year
|
2024-09-06 22:41:12 +02:00
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp assigns(:event, %{
|
|
|
|
|
"title" => heading,
|
|
|
|
|
"contents" => contents,
|
2024-10-03 22:52:40 +02:00
|
|
|
"started_at" => started_at,
|
2024-10-16 15:40:53 +02:00
|
|
|
"ended_at" => ended_at,
|
2024-10-03 22:52:40 +02:00
|
|
|
"lead" => lead,
|
|
|
|
|
"poster" => poster,
|
|
|
|
|
"bands" => bands,
|
|
|
|
|
"mia" => mia,
|
|
|
|
|
"category" => category
|
2024-09-06 22:41:12 +02:00
|
|
|
}) do
|
2024-10-16 15:40:53 +02:00
|
|
|
year = String.slice(started_at, 0..3)
|
|
|
|
|
|
2024-09-06 22:41:12 +02:00
|
|
|
[
|
2024-10-03 14:38:56 +02:00
|
|
|
page_title: heading,
|
2024-10-16 15:40:53 +02:00
|
|
|
breadcrumbs: [{"evenemang", "Evenemang"}, {year, year, ""}],
|
2024-09-06 22:41:12 +02:00
|
|
|
heading: heading,
|
|
|
|
|
contents: Earmark.as_html!(contents),
|
2024-10-01 16:44:34 +02:00
|
|
|
lead: lead,
|
2024-10-16 15:40:53 +02:00
|
|
|
year: year,
|
2024-10-03 22:52:40 +02:00
|
|
|
poster: poster,
|
|
|
|
|
bands: bands,
|
|
|
|
|
mia: mia,
|
2024-10-16 15:40:53 +02:00
|
|
|
category: category,
|
|
|
|
|
started_at: started_at,
|
|
|
|
|
ended_at: ended_at
|
2024-09-06 22:41:12 +02:00
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp assigns(:link, %{
|
|
|
|
|
"title" => heading,
|
|
|
|
|
"contents" => contents,
|
|
|
|
|
"pubDate" => published_at,
|
2024-10-03 14:38:56 +02:00
|
|
|
"date_updated" => updated_at,
|
2024-09-06 22:41:12 +02:00
|
|
|
"source" => url,
|
|
|
|
|
"h1" => title
|
|
|
|
|
}) do
|
2024-10-16 15:40:53 +02:00
|
|
|
year = String.slice(published_at, 0..3)
|
|
|
|
|
|
2024-09-06 22:41:12 +02:00
|
|
|
[
|
2024-10-03 14:38:56 +02:00
|
|
|
page_title: heading,
|
2024-10-16 15:40:53 +02:00
|
|
|
breadcrumbs: [{"delningar", "Delningar"}, {year, year, ""}],
|
2024-09-06 22:41:12 +02:00
|
|
|
heading: heading,
|
|
|
|
|
contents: Earmark.as_html!(contents),
|
|
|
|
|
published_at: published_at,
|
|
|
|
|
url: url,
|
2024-10-03 14:38:56 +02:00
|
|
|
title: title,
|
2024-10-16 15:40:53 +02:00
|
|
|
year: year,
|
2024-10-03 14:38:56 +02:00
|
|
|
updated_at:
|
|
|
|
|
case updated_at do
|
|
|
|
|
nil -> published_at
|
|
|
|
|
ua -> String.slice(ua, 0..9)
|
|
|
|
|
end
|
2024-09-06 22:41:12 +02:00
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp assigns(:page, %{
|
|
|
|
|
"title" => heading,
|
|
|
|
|
"contents" => contents,
|
|
|
|
|
"date_updated" => updated_at
|
|
|
|
|
}) do
|
|
|
|
|
[
|
2024-10-16 15:40:53 +02:00
|
|
|
page_title: heading,
|
|
|
|
|
breadcrumbs: [],
|
2024-09-06 22:41:12 +02:00
|
|
|
heading: heading,
|
|
|
|
|
contents: Earmark.as_html!(contents),
|
2024-10-03 10:00:57 +02:00
|
|
|
updated_at: String.slice(updated_at, 0..9)
|
2024-09-06 22:41:12 +02:00
|
|
|
]
|
|
|
|
|
end
|
2024-10-04 13:15:27 +02:00
|
|
|
|
2025-02-11 13:15:43 +01:00
|
|
|
defp assigns(:note, %{
|
|
|
|
|
"contents" => text,
|
|
|
|
|
"images" => images,
|
|
|
|
|
"date_created" => published_at
|
|
|
|
|
}) do
|
|
|
|
|
year = String.slice(published_at, 0..3)
|
|
|
|
|
|
|
|
|
|
[
|
|
|
|
|
text: Earmark.as_html!(text),
|
|
|
|
|
breadcrumbs: [{"anteckningar", "Anteckningar"}, {year, year, ""}],
|
|
|
|
|
date_created: String.slice(published_at, 0..9),
|
|
|
|
|
images: images
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-04 13:15:27 +02:00
|
|
|
defp assigns(:album, %{
|
|
|
|
|
"year" => year,
|
|
|
|
|
"album" => album,
|
|
|
|
|
"contents" => contents,
|
|
|
|
|
"cover" => cover,
|
|
|
|
|
"purchased_at" => purchased_at,
|
|
|
|
|
"externalId" => count,
|
2024-10-16 15:40:53 +02:00
|
|
|
"songs" => songs,
|
|
|
|
|
"summary" => summary,
|
|
|
|
|
"artist" => artist
|
2024-10-04 13:15:27 +02:00
|
|
|
}) do
|
2024-10-16 15:40:53 +02:00
|
|
|
purchase_year = String.slice(purchased_at, 0..3)
|
|
|
|
|
|
2024-10-04 13:15:27 +02:00
|
|
|
[
|
2024-10-16 15:40:53 +02:00
|
|
|
page_title: summary,
|
|
|
|
|
breadcrumbs: [{purchase_year, purchase_year}],
|
2024-10-04 13:15:27 +02:00
|
|
|
count: count,
|
|
|
|
|
album: album,
|
|
|
|
|
cover: cover,
|
|
|
|
|
year: to_string(year),
|
2024-10-16 15:40:53 +02:00
|
|
|
purchase_year: purchase_year,
|
2024-10-04 13:15:27 +02:00
|
|
|
contents: Earmark.as_html!(contents),
|
|
|
|
|
songs: Enum.map(songs, fn %{"title" => name} -> "\"" <> name <> "\"" end),
|
2024-10-16 15:40:53 +02:00
|
|
|
artist: artist,
|
|
|
|
|
summary: summary
|
2024-10-04 13:15:27 +02:00
|
|
|
]
|
|
|
|
|
end
|
2024-09-06 22:41:12 +02:00
|
|
|
end
|