sloanelybutsurely.com/lib/web/controllers/post_controller.ex
2025-03-25 21:54:52 -04:00

21 lines
624 B
Elixir

defmodule Web.PostController do
use Web, :controller
def show(conn, %{"year" => year, "month" => month, "day" => day, "slug" => slug}) do
with {year, ""} <- Integer.parse(year),
{month, ""} <- Integer.parse(month),
{day, ""} <- Integer.parse(day),
{:ok, publish_date} <- Date.new(year, month, day) do
post = Core.Posts.get_by_publish_date_and_slug!(publish_date, slug)
conn
|> render_post(post)
else
_ -> raise Ecto.NoResultsError
end
end
defp render_post(conn, %Schema.Post{kind: :blog} = blog) do
render(conn, :show_blog, blog: blog)
end
end