~afontaine/home

4e6d71b5132ca77a5521370b22292ae2d1d6b354 — Andrew Fontaine 8 months ago 5d6569a
Generate Post Context

A bunch will be removed and changed, but this is what phoenix generated
to start.
A apps/blog/lib/blog/blog.ex => apps/blog/lib/blog/blog.ex +104 -0
@@ 0,0 1,104 @@
defmodule Blog.Blog do
  @moduledoc """
  The Blog context.
  """

  import Ecto.Query, warn: false
  alias Blog.Repo

  alias Blog.Blog.Post

  @doc """
  Returns the list of posts.

  ## Examples

      iex> list_posts()
      [%Post{}, ...]

  """
  def list_posts do
    Repo.all(Post)
  end

  @doc """
  Gets a single post.

  Raises `Ecto.NoResultsError` if the Post does not exist.

  ## Examples

      iex> get_post!(123)
      %Post{}

      iex> get_post!(456)
      ** (Ecto.NoResultsError)

  """
  def get_post!(id), do: Repo.get!(Post, id)

  @doc """
  Creates a post.

  ## Examples

      iex> create_post(%{field: value})
      {:ok, %Post{}}

      iex> create_post(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def create_post(attrs \\ %{}) do
    %Post{}
    |> Post.changeset(attrs)
    |> Repo.insert()
  end

  @doc """
  Updates a post.

  ## Examples

      iex> update_post(post, %{field: new_value})
      {:ok, %Post{}}

      iex> update_post(post, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_post(%Post{} = post, attrs) do
    post
    |> Post.changeset(attrs)
    |> Repo.update()
  end

  @doc """
  Deletes a post.

  ## Examples

      iex> delete_post(post)
      {:ok, %Post{}}

      iex> delete_post(post)
      {:error, %Ecto.Changeset{}}

  """
  def delete_post(%Post{} = post) do
    Repo.delete(post)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking post changes.

  ## Examples

      iex> change_post(post)
      %Ecto.Changeset{data: %Post{}}

  """
  def change_post(%Post{} = post, attrs \\ %{}) do
    Post.changeset(post, attrs)
  end
end

A apps/blog/lib/blog/blog/post.ex => apps/blog/lib/blog/blog/post.ex +19 -0
@@ 0,0 1,19 @@
defmodule Blog.Blog.Post do
  use Ecto.Schema
  import Ecto.Changeset

  schema "posts" do
    field :message_id, :string
    field :subject, :string
    field :text, :string

    timestamps()
  end

  @doc false
  def changeset(post, attrs) do
    post
    |> cast(attrs, [:subject, :text, :message_id])
    |> validate_required([:subject, :text, :message_id])
  end
end

A apps/blog/priv/repo/migrations/20210326030419_create_posts.exs => apps/blog/priv/repo/migrations/20210326030419_create_posts.exs +15 -0
@@ 0,0 1,15 @@
defmodule Blog.Repo.Migrations.CreatePosts do
  use Ecto.Migration

  def change do
    create table(:posts) do
      add :subject, :string
      add :text, :string
      add :message_id, :string

      timestamps()
    end

    create unique_index(:posts, [:message_id])
  end
end

A apps/blog/test/blog/blog_test.exs => apps/blog/test/blog/blog_test.exs +72 -0
@@ 0,0 1,72 @@
defmodule Blog.BlogTest do
  use Blog.DataCase

  alias Blog.Blog

  describe "posts" do
    alias Blog.Post

    @valid_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 post_fixture(attrs \\ %{}) do
      {:ok, post} =
        attrs
        |> Enum.into(@valid_attrs)
        |> Blog.create_post()

      post
    end

    test "list_posts/0 returns all posts" do
      post = post_fixture()
      assert Blog.list_posts() == [post]
    end

    test "get_post!/1 returns the post with given id" do
      post = post_fixture()
      assert Blog.get_post!(post.id) == post
    end

    test "create_post/1 with valid data creates a post" do
      assert {:ok, %Post{} = post} = Blog.create_post(@valid_attrs)
      assert post.message_id == "some message_id"
      assert post.subject == "some subject"
      assert post.text == "some text"
    end

    test "create_post/1 with invalid data returns error changeset" do
      assert {:error, %Ecto.Changeset{}} = Blog.create_post(@invalid_attrs)
    end

    test "update_post/2 with valid data updates the post" do
      post = post_fixture()
      assert {:ok, %Post{} = post} = Blog.update_post(post, @update_attrs)
      assert post.message_id == "some updated message_id"
      assert post.subject == "some updated subject"
      assert post.text == "some updated text"
    end

    test "update_post/2 with invalid data returns error changeset" do
      post = post_fixture()
      assert {:error, %Ecto.Changeset{}} = Blog.update_post(post, @invalid_attrs)
      assert post == Blog.get_post!(post.id)
    end

    test "delete_post/1 deletes the post" do
      post = post_fixture()
      assert {:ok, %Post{}} = Blog.delete_post(post)
      assert_raise Ecto.NoResultsError, fn -> Blog.get_post!(post.id) end
    end

    test "change_post/1 returns a post changeset" do
      post = post_fixture()
      assert %Ecto.Changeset{} = Blog.change_post(post)
    end
  end
end

A apps/home_web/lib/home_web/controllers/post_controller.ex => apps/home_web/lib/home_web/controllers/post_controller.ex +62 -0
@@ 0,0 1,62 @@
defmodule HomeWeb.PostController do
  use HomeWeb, :controller

  alias Blog.Blog
  alias Blog.Blog.Post

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

  def new(conn, _params) do
    changeset = Blog.change_post(%Post{})
    render(conn, "new.html", changeset: changeset)
  end

  def create(conn, %{"post" => post_params}) do
    case Blog.create_post(post_params) do
      {:ok, post} ->
        conn
        |> put_flash(:info, "Post created successfully.")
        |> redirect(to: Routes.post_path(conn, :show, post))

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

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

  def edit(conn, %{"id" => id}) do
    post = Blog.get_post!(id)
    changeset = Blog.change_post(post)
    render(conn, "edit.html", post: post, changeset: changeset)
  end

  def update(conn, %{"id" => id, "post" => post_params}) do
    post = Blog.get_post!(id)

    case Blog.update_post(post, post_params) do
      {:ok, post} ->
        conn
        |> put_flash(:info, "Post updated successfully.")
        |> redirect(to: Routes.post_path(conn, :show, post))

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "edit.html", post: post, changeset: changeset)
    end
  end

  def delete(conn, %{"id" => id}) do
    post = Blog.get_post!(id)
    {:ok, _post} = Blog.delete_post(post)

    conn
    |> put_flash(:info, "Post deleted successfully.")
    |> redirect(to: Routes.post_path(conn, :index))
  end
end

A apps/home_web/lib/home_web/templates/post/edit.html.eex => apps/home_web/lib/home_web/templates/post/edit.html.eex +5 -0
@@ 0,0 1,5 @@
<h1>Edit Post</h1>

<%= render "form.html", Map.put(assigns, :action, Routes.post_path(@conn, :update, @post)) %>

<span><%= link "Back", to: Routes.post_path(@conn, :index) %></span>

A apps/home_web/lib/home_web/templates/post/form.html.eex => apps/home_web/lib/home_web/templates/post/form.html.eex +23 -0
@@ 0,0 1,23 @@
<%= form_for @changeset, @action, fn f -> %>
  <%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>
    </div>
  <% end %>

  <%= label f, :subject %>
  <%= text_input f, :subject %>
  <%= error_tag f, :subject %>

  <%= label f, :text %>
  <%= text_input f, :text %>
  <%= error_tag f, :text %>

  <%= label f, :message_id %>
  <%= text_input f, :message_id %>
  <%= error_tag f, :message_id %>

  <div>
    <%= submit "Save" %>
  </div>
<% end %>

A apps/home_web/lib/home_web/templates/post/index.html.eex => apps/home_web/lib/home_web/templates/post/index.html.eex +30 -0
@@ 0,0 1,30 @@
<h1>Listing Posts</h1>

<table>
  <thead>
    <tr>
      <th>Subject</th>
      <th>Text</th>
      <th>Message</th>

      <th></th>
    </tr>
  </thead>
  <tbody>
<%= for post <- @posts do %>
    <tr>
      <td><%= post.subject %></td>
      <td><%= post.text %></td>
      <td><%= post.message_id %></td>

      <td>
        <span><%= link "Show", to: Routes.post_path(@conn, :show, post) %></span>
        <span><%= link "Edit", to: Routes.post_path(@conn, :edit, post) %></span>
        <span><%= link "Delete", to: Routes.post_path(@conn, :delete, post), method: :delete, data: [confirm: "Are you sure?"] %></span>
      </td>
    </tr>
<% end %>
  </tbody>
</table>

<span><%= link "New Post", to: Routes.post_path(@conn, :new) %></span>

A apps/home_web/lib/home_web/templates/post/new.html.eex => apps/home_web/lib/home_web/templates/post/new.html.eex +5 -0
@@ 0,0 1,5 @@
<h1>New Post</h1>

<%= render "form.html", Map.put(assigns, :action, Routes.post_path(@conn, :create)) %>

<span><%= link "Back", to: Routes.post_path(@conn, :index) %></span>

A apps/home_web/lib/home_web/templates/post/show.html.eex => apps/home_web/lib/home_web/templates/post/show.html.eex +23 -0
@@ 0,0 1,23 @@
<h1>Show Post</h1>

<ul>

  <li>
    <strong>Subject:</strong>
    <%= @post.subject %>
  </li>

  <li>
    <strong>Text:</strong>
    <%= @post.text %>
  </li>

  <li>
    <strong>Message:</strong>
    <%= @post.message_id %>
  </li>

</ul>

<span><%= link "Edit", to: Routes.post_path(@conn, :edit, @post) %></span>
<span><%= link "Back", to: Routes.post_path(@conn, :index) %></span>

A apps/home_web/lib/home_web/views/post_view.ex => apps/home_web/lib/home_web/views/post_view.ex +3 -0
@@ 0,0 1,3 @@
defmodule HomeWeb.PostView do
  use HomeWeb, :view
end

M apps/home_web/mix.exs => apps/home_web/mix.exs +2 -1
@@ 46,7 46,8 @@ defmodule HomeWeb.MixProject do
      {:gettext, "~> 0.11"},
      {:home, in_umbrella: true},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"}
      {:plug_cowboy, "~> 2.0"},
      {:blog, in_umbrella: true}
    ]
  end


A apps/home_web/test/home_web/controllers/post_controller_test.exs => apps/home_web/test/home_web/controllers/post_controller_test.exs +88 -0
@@ 0,0 1,88 @@
defmodule HomeWeb.PostControllerTest do
  use HomeWeb.ConnCase

  alias Blog.Blog

  @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)
    post
  end

  describe "index" do
    test "lists all posts", %{conn: conn} do
      conn = get(conn, Routes.post_path(conn, :index))
      assert html_response(conn, 200) =~ "Listing Posts"
    end
  end

  describe "new post" do
    test "renders form", %{conn: conn} do
      conn = get(conn, Routes.post_path(conn, :new))
      assert html_response(conn, 200) =~ "New Post"
    end
  end

  describe "create post" do
    test "redirects to show when data is valid", %{conn: conn} do
      conn = post(conn, Routes.post_path(conn, :create), post: @create_attrs)

      assert %{id: id} = redirected_params(conn)
      assert redirected_to(conn) == Routes.post_path(conn, :show, id)

      conn = get(conn, Routes.post_path(conn, :show, id))
      assert html_response(conn, 200) =~ "Show Post"
    end

    test "renders errors when data is invalid", %{conn: conn} do
      conn = post(conn, Routes.post_path(conn, :create), post: @invalid_attrs)
      assert html_response(conn, 200) =~ "New Post"
    end
  end

  describe "edit post" do
    setup [:create_post]

    test "renders form for editing chosen post", %{conn: conn, post: post} do
      conn = get(conn, Routes.post_path(conn, :edit, post))
      assert html_response(conn, 200) =~ "Edit Post"
    end
  end

  describe "update post" do
    setup [:create_post]

    test "redirects when data is valid", %{conn: conn, post: post} do
      conn = put(conn, Routes.post_path(conn, :update, post), post: @update_attrs)
      assert redirected_to(conn) == Routes.post_path(conn, :show, post)

      conn = get(conn, Routes.post_path(conn, :show, post))
      assert html_response(conn, 200) =~ "some updated message_id"
    end

    test "renders errors when data is invalid", %{conn: conn, post: post} do
      conn = put(conn, Routes.post_path(conn, :update, post), post: @invalid_attrs)
      assert html_response(conn, 200) =~ "Edit Post"
    end
  end

  describe "delete post" do
    setup [:create_post]

    test "deletes chosen post", %{conn: conn, post: post} do
      conn = delete(conn, Routes.post_path(conn, :delete, post))
      assert redirected_to(conn) == Routes.post_path(conn, :index)
      assert_error_sent 404, fn ->
        get(conn, Routes.post_path(conn, :show, post))
      end
    end
  end

  defp create_post(_) do
    post = fixture(:post)
    %{post: post}
  end
end