2019-12-20 04:41:19 +01:00
|
|
|
defmodule PhilomenaWeb.LoadPollPlug do
|
|
|
|
alias Philomena.Polls.Poll
|
|
|
|
alias Philomena.Repo
|
|
|
|
|
|
|
|
import Ecto.Query
|
|
|
|
|
2024-08-08 14:24:43 +02:00
|
|
|
def init(opts), do: opts
|
2019-12-20 04:41:19 +01:00
|
|
|
|
2024-08-08 14:24:43 +02:00
|
|
|
def call(%{assigns: %{topic: topic}} = conn, _opts) do
|
2019-12-20 04:41:19 +01:00
|
|
|
Poll
|
|
|
|
|> where(topic_id: ^topic.id)
|
|
|
|
|> Repo.one()
|
2024-08-08 14:24:43 +02:00
|
|
|
|> case do
|
|
|
|
nil ->
|
|
|
|
PhilomenaWeb.NotFoundPlug.call(conn)
|
2019-12-20 04:41:19 +01:00
|
|
|
|
2024-08-08 14:24:43 +02:00
|
|
|
poll ->
|
|
|
|
Plug.Conn.assign(conn, :poll, poll)
|
2019-12-20 04:41:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|