~afontaine/home

d8b7601fd55a93ed24b8281755129e81b29b4a37 — Andrew Fontaine 7 months ago 72705bd
Only show last post if there is one

`one!` throws if there is no value to return, but this should be
optional and hidden if there is not yet a post.

`one` returns `nil` if there is no value, so we use that instead.
M apps/blog/lib/blog/content.ex => apps/blog/lib/blog/content.ex +1 -1
@@ 49,7 49,7 @@ defmodule Blog.Content do
      |> preload([_, tags, posts], tags: {tags, posts: posts})
      |> Repo.one!()

  def last_post!(), do: Repo.one!(from p in Post, order_by: [desc: p.id], limit: 1)
  def last_post(), do: Repo.one(from p in Post, order_by: [desc: p.id], limit: 1)

  @doc """
  Creates a post.

M apps/home_web/lib/home_web/controllers/page_controller.ex => apps/home_web/lib/home_web/controllers/page_controller.ex +1 -1
@@ 2,7 2,7 @@ defmodule HomeWeb.PageController do
  use HomeWeb, :controller

  def index(conn, _params) do
    last_post = Blog.Content.last_post!()
    last_post = Blog.Content.last_post()

    render(conn, "index.html", last_post: last_post)
  end

M apps/home_web/lib/home_web/templates/page/index.html.eex => apps/home_web/lib/home_web/templates/page/index.html.eex +5 -4
@@ 1,10 1,11 @@
<section>
  <%= render_shared("info_card.html", class: "p-4 my-32 md:ml-32") %>

  <h2 class="text-4xl">Latest Post</h2>
  <%= if @last_post do %>
    <h2 class="text-4xl">Latest Post</h2>

  <%= render_shared("post_snippet.html", conn: @conn, post: @last_post) %>

  <%= link "Read more posts", to: Routes.post_path(@conn, :index), class: "pt-6 underline dark:text-indigo-300 text-indigo-500" %>
    <%= render_shared("post_snippet.html", conn: @conn, post: @last_post) %>

    <%= link "Read more posts", to: Routes.post_path(@conn, :index), class: "pt-6 underline dark:text-indigo-300 text-indigo-500" %>
  <%= end %>
</section>