From a2b22c83c864e53f0a2d8c72700abbefb5d1b6a5 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Thu, 20 Apr 2017 12:23:09 -0700 Subject: [PATCH] mix phoenix.gen.html Post posts title:string body:text author:references:users --- .../migrations/20170420192251_create_post.exs | 15 +++++ .../test/controllers/post_controller_test.exs | 66 +++++++++++++++++++ phoenix_blog/test/models/post_test.exs | 18 +++++ .../web/controllers/post_controller.ex | 65 ++++++++++++++++++ phoenix_blog/web/models/post.ex | 20 ++++++ phoenix_blog/web/templates/post/edit.html.eex | 6 ++ phoenix_blog/web/templates/post/form.html.eex | 23 +++++++ .../web/templates/post/index.html.eex | 30 +++++++++ phoenix_blog/web/templates/post/new.html.eex | 6 ++ phoenix_blog/web/templates/post/show.html.eex | 23 +++++++ phoenix_blog/web/views/post_view.ex | 3 + 11 files changed, 275 insertions(+) create mode 100644 phoenix_blog/priv/repo/migrations/20170420192251_create_post.exs create mode 100644 phoenix_blog/test/controllers/post_controller_test.exs create mode 100644 phoenix_blog/test/models/post_test.exs create mode 100644 phoenix_blog/web/controllers/post_controller.ex create mode 100644 phoenix_blog/web/models/post.ex create mode 100644 phoenix_blog/web/templates/post/edit.html.eex create mode 100644 phoenix_blog/web/templates/post/form.html.eex create mode 100644 phoenix_blog/web/templates/post/index.html.eex create mode 100644 phoenix_blog/web/templates/post/new.html.eex create mode 100644 phoenix_blog/web/templates/post/show.html.eex create mode 100644 phoenix_blog/web/views/post_view.ex diff --git a/phoenix_blog/priv/repo/migrations/20170420192251_create_post.exs b/phoenix_blog/priv/repo/migrations/20170420192251_create_post.exs new file mode 100644 index 0000000..43332bb --- /dev/null +++ b/phoenix_blog/priv/repo/migrations/20170420192251_create_post.exs @@ -0,0 +1,15 @@ +defmodule PhoenixBlog.Repo.Migrations.CreatePost do + use Ecto.Migration + + def change do + create table(:posts) do + add :title, :string + add :body, :text + add :author, references(:users, on_delete: :nothing) + + timestamps() + end + create index(:posts, [:author]) + + end +end diff --git a/phoenix_blog/test/controllers/post_controller_test.exs b/phoenix_blog/test/controllers/post_controller_test.exs new file mode 100644 index 0000000..be24cd6 --- /dev/null +++ b/phoenix_blog/test/controllers/post_controller_test.exs @@ -0,0 +1,66 @@ +defmodule PhoenixBlog.PostControllerTest do + use PhoenixBlog.ConnCase + + alias PhoenixBlog.Post + @valid_attrs %{body: "some content", title: "some content"} + @invalid_attrs %{} + + test "lists all entries on index", %{conn: conn} do + conn = get conn, post_path(conn, :index) + assert html_response(conn, 200) =~ "Listing posts" + end + + test "renders form for new resources", %{conn: conn} do + conn = get conn, post_path(conn, :new) + assert html_response(conn, 200) =~ "New post" + end + + test "creates resource and redirects when data is valid", %{conn: conn} do + conn = post conn, post_path(conn, :create), post: @valid_attrs + assert redirected_to(conn) == post_path(conn, :index) + assert Repo.get_by(Post, @valid_attrs) + end + + test "does not create resource and renders errors when data is invalid", %{conn: conn} do + conn = post conn, post_path(conn, :create), post: @invalid_attrs + assert html_response(conn, 200) =~ "New post" + end + + test "shows chosen resource", %{conn: conn} do + post = Repo.insert! %Post{} + conn = get conn, post_path(conn, :show, post) + assert html_response(conn, 200) =~ "Show post" + end + + test "renders page not found when id is nonexistent", %{conn: conn} do + assert_error_sent 404, fn -> + get conn, post_path(conn, :show, -1) + end + end + + test "renders form for editing chosen resource", %{conn: conn} do + post = Repo.insert! %Post{} + conn = get conn, post_path(conn, :edit, post) + assert html_response(conn, 200) =~ "Edit post" + end + + test "updates chosen resource and redirects when data is valid", %{conn: conn} do + post = Repo.insert! %Post{} + conn = put conn, post_path(conn, :update, post), post: @valid_attrs + assert redirected_to(conn) == post_path(conn, :show, post) + assert Repo.get_by(Post, @valid_attrs) + end + + test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do + post = Repo.insert! %Post{} + conn = put conn, post_path(conn, :update, post), post: @invalid_attrs + assert html_response(conn, 200) =~ "Edit post" + end + + test "deletes chosen resource", %{conn: conn} do + post = Repo.insert! %Post{} + conn = delete conn, post_path(conn, :delete, post) + assert redirected_to(conn) == post_path(conn, :index) + refute Repo.get(Post, post.id) + end +end diff --git a/phoenix_blog/test/models/post_test.exs b/phoenix_blog/test/models/post_test.exs new file mode 100644 index 0000000..8a5a0d5 --- /dev/null +++ b/phoenix_blog/test/models/post_test.exs @@ -0,0 +1,18 @@ +defmodule PhoenixBlog.PostTest do + use PhoenixBlog.ModelCase + + alias PhoenixBlog.Post + + @valid_attrs %{body: "some content", title: "some content"} + @invalid_attrs %{} + + test "changeset with valid attributes" do + changeset = Post.changeset(%Post{}, @valid_attrs) + assert changeset.valid? + end + + test "changeset with invalid attributes" do + changeset = Post.changeset(%Post{}, @invalid_attrs) + refute changeset.valid? + end +end diff --git a/phoenix_blog/web/controllers/post_controller.ex b/phoenix_blog/web/controllers/post_controller.ex new file mode 100644 index 0000000..fa91498 --- /dev/null +++ b/phoenix_blog/web/controllers/post_controller.ex @@ -0,0 +1,65 @@ +defmodule PhoenixBlog.PostController do + use PhoenixBlog.Web, :controller + + alias PhoenixBlog.Post + + def index(conn, _params) do + posts = Repo.all(Post) + render(conn, "index.html", posts: posts) + end + + def new(conn, _params) do + changeset = Post.changeset(%Post{}) + render(conn, "new.html", changeset: changeset) + end + + def create(conn, %{"post" => post_params}) do + changeset = Post.changeset(%Post{}, post_params) + + case Repo.insert(changeset) do + {:ok, _post} -> + conn + |> put_flash(:info, "Post created successfully.") + |> redirect(to: post_path(conn, :index)) + {:error, changeset} -> + render(conn, "new.html", changeset: changeset) + end + end + + def show(conn, %{"id" => id}) do + post = Repo.get!(Post, id) + render(conn, "show.html", post: post) + end + + def edit(conn, %{"id" => id}) do + post = Repo.get!(Post, id) + changeset = Post.changeset(post) + render(conn, "edit.html", post: post, changeset: changeset) + end + + def update(conn, %{"id" => id, "post" => post_params}) do + post = Repo.get!(Post, id) + changeset = Post.changeset(post, post_params) + + case Repo.update(changeset) do + {:ok, post} -> + conn + |> put_flash(:info, "Post updated successfully.") + |> redirect(to: post_path(conn, :show, post)) + {:error, changeset} -> + render(conn, "edit.html", post: post, changeset: changeset) + end + end + + def delete(conn, %{"id" => id}) do + post = Repo.get!(Post, id) + + # Here we use delete! (with a bang) because we expect + # it to always work (and if it does not, it will raise). + Repo.delete!(post) + + conn + |> put_flash(:info, "Post deleted successfully.") + |> redirect(to: post_path(conn, :index)) + end +end diff --git a/phoenix_blog/web/models/post.ex b/phoenix_blog/web/models/post.ex new file mode 100644 index 0000000..7aecb83 --- /dev/null +++ b/phoenix_blog/web/models/post.ex @@ -0,0 +1,20 @@ +defmodule PhoenixBlog.Post do + use PhoenixBlog.Web, :model + + schema "posts" do + field :title, :string + field :body, :string + belongs_to :author, PhoenixBlog.Author + + timestamps() + end + + @doc """ + Builds a changeset based on the `struct` and `params`. + """ + def changeset(struct, params \\ %{}) do + struct + |> cast(params, [:title, :body]) + |> validate_required([:title, :body]) + end +end diff --git a/phoenix_blog/web/templates/post/edit.html.eex b/phoenix_blog/web/templates/post/edit.html.eex new file mode 100644 index 0000000..ace36da --- /dev/null +++ b/phoenix_blog/web/templates/post/edit.html.eex @@ -0,0 +1,6 @@ +

Edit post

+ +<%= render "form.html", changeset: @changeset, + action: post_path(@conn, :update, @post) %> + +<%= link "Back", to: post_path(@conn, :index) %> diff --git a/phoenix_blog/web/templates/post/form.html.eex b/phoenix_blog/web/templates/post/form.html.eex new file mode 100644 index 0000000..0658a87 --- /dev/null +++ b/phoenix_blog/web/templates/post/form.html.eex @@ -0,0 +1,23 @@ +<%= form_for @changeset, @action, fn f -> %> + <%= if @changeset.action do %> +
+

Oops, something went wrong! Please check the errors below.

+
+ <% end %> + +
+ <%= label f, :title, class: "control-label" %> + <%= text_input f, :title, class: "form-control" %> + <%= error_tag f, :title %> +
+ +
+ <%= label f, :body, class: "control-label" %> + <%= textarea f, :body, class: "form-control" %> + <%= error_tag f, :body %> +
+ +
+ <%= submit "Submit", class: "btn btn-primary" %> +
+<% end %> diff --git a/phoenix_blog/web/templates/post/index.html.eex b/phoenix_blog/web/templates/post/index.html.eex new file mode 100644 index 0000000..eeba040 --- /dev/null +++ b/phoenix_blog/web/templates/post/index.html.eex @@ -0,0 +1,30 @@ +

Listing posts

+ + + + + + + + + + + + +<%= for post <- @posts do %> + + + + + + + +<% end %> + +
TitleBodyAuthor
<%= post.title %><%= post.body %><%= post.author %> + <%= link "Show", to: post_path(@conn, :show, post), class: "btn btn-default btn-xs" %> + <%= link "Edit", to: post_path(@conn, :edit, post), class: "btn btn-default btn-xs" %> + <%= link "Delete", to: post_path(@conn, :delete, post), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %> +
+ +<%= link "New post", to: post_path(@conn, :new) %> diff --git a/phoenix_blog/web/templates/post/new.html.eex b/phoenix_blog/web/templates/post/new.html.eex new file mode 100644 index 0000000..cc709c3 --- /dev/null +++ b/phoenix_blog/web/templates/post/new.html.eex @@ -0,0 +1,6 @@ +

New post

+ +<%= render "form.html", changeset: @changeset, + action: post_path(@conn, :create) %> + +<%= link "Back", to: post_path(@conn, :index) %> diff --git a/phoenix_blog/web/templates/post/show.html.eex b/phoenix_blog/web/templates/post/show.html.eex new file mode 100644 index 0000000..204e6e6 --- /dev/null +++ b/phoenix_blog/web/templates/post/show.html.eex @@ -0,0 +1,23 @@ +

Show post

+ + + +<%= link "Edit", to: post_path(@conn, :edit, @post) %> +<%= link "Back", to: post_path(@conn, :index) %> diff --git a/phoenix_blog/web/views/post_view.ex b/phoenix_blog/web/views/post_view.ex new file mode 100644 index 0000000..b5958c7 --- /dev/null +++ b/phoenix_blog/web/views/post_view.ex @@ -0,0 +1,3 @@ +defmodule PhoenixBlog.PostView do + use PhoenixBlog.Web, :view +end