~afontaine/home

91a2c85c130514e0f6baca97693e973d56a74e4e — Andrew Fontaine 8 months ago 3a55eac
Fetch Posts via Slug

Makes for some fancier urls.
M apps/blog/lib/blog/mail/post.ex => apps/blog/lib/blog/mail/post.ex +0 -1
@@ 25,7 25,6 @@ defmodule Blog.Mail.Post do
      |> String.trim()
      |> String.split("\r\n-- \r\n")
      |> Enum.drop(-1)
      |> Enum.map(&String.trim/1)
      |> Enum.join("\n")
      |> String.trim()
      |> String.replace("\r\n", "\n")

A apps/blog/priv/repo/migrations/20210327205729_change_text_type_to_text.exs => apps/blog/priv/repo/migrations/20210327205729_change_text_type_to_text.exs +10 -0
@@ 0,0 1,10 @@
defmodule Blog.Repo.Migrations.ChangeTextTypeToText do
  use Ecto.Migration

  def change do
    alter table(:posts) do
      modify :text, :text
      modify :subject, :text
    end
  end
end

M apps/home_web/lib/home_web/controllers/post_controller.ex => apps/home_web/lib/home_web/controllers/post_controller.ex +4 -4
@@ 1,15 1,15 @@
defmodule HomeWeb.PostController do
  use HomeWeb, :controller

  alias Blog.Blog
  alias Blog.Content

  def index(conn, _params) do
    posts = Blog.list_posts()
    posts = Content.list_posts()
    render(conn, "index.html", posts: posts)
  end

  def show(conn, %{"id" => id}) do
    post = Blog.get_post!(id)
  def show(conn, %{"slug" => slug}) do
    post = Content.get_post!(slug)
    render(conn, "show.html", post: post)
  end
end

M apps/home_web/lib/home_web/router.ex => apps/home_web/lib/home_web/router.ex +1 -1
@@ 18,7 18,7 @@ defmodule HomeWeb.Router do

    get "/", PageController, :index

    resources "/blog", PostController, only: [:index, :show]
    resources "/blog", PostController, only: [:index, :show], param: "slug"
  end

  # Other scopes may use custom stacks.

M apps/home_web/lib/home_web/templates/post/index.html.eex => apps/home_web/lib/home_web/templates/post/index.html.eex +1 -1
@@ 18,7 18,7 @@
      <td><%= post.message_id %></td>

      <td>
        <span><%= link "Show", to: Routes.post_path(@conn, :show, post) %></span>
        <span><%= link "Show", to: Routes.post_path(@conn, :show, post.slug) %></span>
      </td>
    </tr>
<% end %>

M apps/home_web/test/home_web/controllers/post_controller_test.exs => apps/home_web/test/home_web/controllers/post_controller_test.exs +10 -8
@@ 1,18 1,12 @@
defmodule HomeWeb.PostControllerTest do
  use HomeWeb.ConnCase

  alias Blog.Blog
  alias Blog.Content

  @create_attrs %{message_id: "some message_id", subject: "some subject", text: "some text"}
  @update_attrs %{
    message_id: "some updated message_id",
    subject: "some updated subject",
    text: "some updated text"
  }
  @invalid_attrs %{message_id: nil, subject: nil, text: nil}

  def fixture(:post) do
    {:ok, post} = Blog.create_post(@create_attrs)
    {:ok, post} = Content.create_post(@create_attrs)
    post
  end



@@ 22,4 16,12 @@ defmodule HomeWeb.PostControllerTest do
      assert html_response(conn, 200) =~ "Listing Posts"
    end
  end

  describe "show" do
    test "show post", %{conn: conn} do
      post = fixture(:post)
      conn = get(conn, Routes.post_path(conn, :show, post.slug))
      assert html_response(conn, 200) =~ post.subject
    end
  end
end