-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mix phoenix.gen.html Post posts title:string body:text author:referen…
…ces:users
- Loading branch information
Showing
11 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
phoenix_blog/priv/repo/migrations/20170420192251_create_post.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule PhoenixBlog.PostView do | ||
use PhoenixBlog.Web, :view | ||
end |