Skip to content

Commit

Permalink
mix phoenix.gen.html Post posts title:string body:text author:referen…
Browse files Browse the repository at this point in the history
…ces:users
  • Loading branch information
rmg committed Apr 20, 2017
1 parent 7dc08e4 commit a2b22c8
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 0 deletions.
15 changes: 15 additions & 0 deletions phoenix_blog/priv/repo/migrations/20170420192251_create_post.exs
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions phoenix_blog/test/controllers/post_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions phoenix_blog/test/models/post_test.exs
Original file line number Diff line number Diff line change
@@ -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
65 changes: 65 additions & 0 deletions phoenix_blog/web/controllers/post_controller.ex
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions phoenix_blog/web/models/post.ex
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions phoenix_blog/web/templates/post/edit.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h2>Edit post</h2>

<%= render "form.html", changeset: @changeset,
action: post_path(@conn, :update, @post) %>

<%= link "Back", to: post_path(@conn, :index) %>
23 changes: 23 additions & 0 deletions phoenix_blog/web/templates/post/form.html.eex
Original file line number Diff line number Diff line change
@@ -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 %>

<div class="form-group">
<%= label f, :title, class: "control-label" %>
<%= text_input f, :title, class: "form-control" %>
<%= error_tag f, :title %>
</div>

<div class="form-group">
<%= label f, :body, class: "control-label" %>
<%= textarea f, :body, class: "form-control" %>
<%= error_tag f, :body %>
</div>

<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
30 changes: 30 additions & 0 deletions phoenix_blog/web/templates/post/index.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h2>Listing posts</h2>

<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Body</th>
<th>Author</th>

<th></th>
</tr>
</thead>
<tbody>
<%= for post <- @posts do %>
<tr>
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= post.author %></td>

<td class="text-right">
<%= 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" %>
</td>
</tr>
<% end %>
</tbody>
</table>

<%= link "New post", to: post_path(@conn, :new) %>
6 changes: 6 additions & 0 deletions phoenix_blog/web/templates/post/new.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h2>New post</h2>

<%= render "form.html", changeset: @changeset,
action: post_path(@conn, :create) %>

<%= link "Back", to: post_path(@conn, :index) %>
23 changes: 23 additions & 0 deletions phoenix_blog/web/templates/post/show.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h2>Show post</h2>

<ul>

<li>
<strong>Title:</strong>
<%= @post.title %>
</li>

<li>
<strong>Body:</strong>
<%= @post.body %>
</li>

<li>
<strong>Author:</strong>
<%= @post.author %>
</li>

</ul>

<%= link "Edit", to: post_path(@conn, :edit, @post) %>
<%= link "Back", to: post_path(@conn, :index) %>
3 changes: 3 additions & 0 deletions phoenix_blog/web/views/post_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule PhoenixBlog.PostView do
use PhoenixBlog.Web, :view
end

0 comments on commit a2b22c8

Please sign in to comment.