Permalink
Browse files

Added user crud

  • Loading branch information...
kidooom committed Oct 7, 2015
1 parent 801f903 commit 336236aee52c95b2daf18ee9d80c589d862df876
@@ -0,0 +1,15 @@
defmodule PhoenixTraining.Repo.Migrations.CreateUser do
use Ecto.Migration
def change do
create table(:users) do
add :name, :string
add :email, :string
add :bio, :string
add :number_of_pets, :integer
timestamps
end
end
end
@@ -0,0 +1,71 @@
defmodule PhoenixTraining.UserControllerTest do
use PhoenixTraining.ConnCase
alias PhoenixTraining.User
@valid_attrs %{bio: "some content", email: "some content", name: "some content", number_of_pets: 42}
@invalid_attrs %{}
setup do
conn = conn()
{:ok, conn: conn}
end
test "lists all entries on index", %{conn: conn} do
conn = get conn, user_path(conn, :index)
assert html_response(conn, 200) =~ "Listing users"
end
test "renders form for new resources", %{conn: conn} do
conn = get conn, user_path(conn, :new)
assert html_response(conn, 200) =~ "New user"
end
test "creates resource and redirects when data is valid", %{conn: conn} do
conn = post conn, user_path(conn, :create), user: @valid_attrs
assert redirected_to(conn) == user_path(conn, :index)
assert Repo.get_by(User, @valid_attrs)
end
test "does not create resource and renders errors when data is invalid", %{conn: conn} do
conn = post conn, user_path(conn, :create), user: @invalid_attrs
assert html_response(conn, 200) =~ "New user"
end
test "shows chosen resource", %{conn: conn} do
user = Repo.insert! %User{}
conn = get conn, user_path(conn, :show, user)
assert html_response(conn, 200) =~ "Show user"
end
test "renders page not found when id is nonexistent", %{conn: conn} do
assert_raise Ecto.NoResultsError, fn ->
get conn, user_path(conn, :show, -1)
end
end
test "renders form for editing chosen resource", %{conn: conn} do
user = Repo.insert! %User{}
conn = get conn, user_path(conn, :edit, user)
assert html_response(conn, 200) =~ "Edit user"
end
test "updates chosen resource and redirects when data is valid", %{conn: conn} do
user = Repo.insert! %User{}
conn = put conn, user_path(conn, :update, user), user: @valid_attrs
assert redirected_to(conn) == user_path(conn, :show, user)
assert Repo.get_by(User, @valid_attrs)
end
test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
user = Repo.insert! %User{}
conn = put conn, user_path(conn, :update, user), user: @invalid_attrs
assert html_response(conn, 200) =~ "Edit user"
end
test "deletes chosen resource", %{conn: conn} do
user = Repo.insert! %User{}
conn = delete conn, user_path(conn, :delete, user)
assert redirected_to(conn) == user_path(conn, :index)
refute Repo.get(User, user.id)
end
end
View
@@ -0,0 +1,18 @@
defmodule PhoenixTraining.UserTest do
use PhoenixTraining.ModelCase
alias PhoenixTraining.User
@valid_attrs %{bio: "some content", email: "some content", name: "some content", number_of_pets: 42}
@invalid_attrs %{}
test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
end
test "changeset with invalid attributes" do
changeset = User.changeset(%User{}, @invalid_attrs)
refute changeset.valid?
end
end
@@ -0,0 +1,67 @@
defmodule PhoenixTraining.UserController do
use PhoenixTraining.Web, :controller
alias PhoenixTraining.User
plug :scrub_params, "user" when action in [:create, :update]
def index(conn, _params) do
users = Repo.all(User)
render(conn, "index.html", users: users)
end
def new(conn, _params) do
changeset = User.changeset(%User{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, _user} ->
conn
|> put_flash(:info, "User created successfully.")
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
user = Repo.get!(User, id)
render(conn, "show.html", user: user)
end
def edit(conn, %{"id" => id}) do
user = Repo.get!(User, id)
changeset = User.changeset(user)
render(conn, "edit.html", user: user, changeset: changeset)
end
def update(conn, %{"id" => id, "user" => user_params}) do
user = Repo.get!(User, id)
changeset = User.changeset(user, user_params)
case Repo.update(changeset) do
{:ok, user} ->
conn
|> put_flash(:info, "User updated successfully.")
|> redirect(to: user_path(conn, :show, user))
{:error, changeset} ->
render(conn, "edit.html", user: user, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
user = Repo.get!(User, 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!(user)
conn
|> put_flash(:info, "User deleted successfully.")
|> redirect(to: user_path(conn, :index))
end
end
View
@@ -0,0 +1,26 @@
defmodule PhoenixTraining.User do
use PhoenixTraining.Web, :model
schema "users" do
field :name, :string
field :email, :string
field :bio, :string
field :number_of_pets, :integer
timestamps
end
@required_fields ~w(name email bio number_of_pets)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
View
@@ -18,6 +18,7 @@ defmodule PhoenixTraining.Router do
get "/", PageController, :index
get "/hello", HelloController, :index
resources "/users", UserController
end
# Other scopes may use custom stacks.
@@ -0,0 +1,6 @@
<h2>Edit user</h2>
<%= render "form.html", changeset: @changeset,
action: user_path(@conn, :update, @user) %>
<%= link "Back", to: user_path(@conn, :index) %>
@@ -0,0 +1,36 @@
<%= 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>
<ul>
<%= for {attr, message} <- f.errors do %>
<li><%= humanize(attr) %> <%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= label f, :name, "Name", class: "control-label" %>
<%= text_input f, :name, class: "form-control" %>
</div>
<div class="form-group">
<%= label f, :email, "Email", class: "control-label" %>
<%= text_input f, :email, class: "form-control" %>
</div>
<div class="form-group">
<%= label f, :bio, "Bio", class: "control-label" %>
<%= text_input f, :bio, class: "form-control" %>
</div>
<div class="form-group">
<%= label f, :number_of_pets, "Number of pets", class: "control-label" %>
<%= number_input f, :number_of_pets, class: "form-control" %>
</div>
<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
@@ -0,0 +1,32 @@
<h2>Listing users</h2>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Bio</th>
<th>Number of pets</th>
<th></th>
</tr>
</thead>
<tbody>
<%= for user <- @users do %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td><%= user.bio %></td>
<td><%= user.number_of_pets %></td>
<td class="text-right">
<%= link "Show", to: user_path(@conn, :show, user), class: "btn btn-default btn-xs" %>
<%= link "Edit", to: user_path(@conn, :edit, user), class: "btn btn-default btn-xs" %>
<%= link "Delete", to: user_path(@conn, :delete, user), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= link "New user", to: user_path(@conn, :new) %>
@@ -0,0 +1,6 @@
<h2>New user</h2>
<%= render "form.html", changeset: @changeset,
action: user_path(@conn, :create) %>
<%= link "Back", to: user_path(@conn, :index) %>
@@ -0,0 +1,28 @@
<h2>Show user</h2>
<ul>
<li>
<strong>Name:</strong>
<%= @user.name %>
</li>
<li>
<strong>Email:</strong>
<%= @user.email %>
</li>
<li>
<strong>Bio:</strong>
<%= @user.bio %>
</li>
<li>
<strong>Number of pets:</strong>
<%= @user.number_of_pets %>
</li>
</ul>
<%= link "Edit", to: user_path(@conn, :edit, @user) %>
<%= link "Back", to: user_path(@conn, :index) %>
View
@@ -0,0 +1,3 @@
defmodule PhoenixTraining.UserView do
use PhoenixTraining.Web, :view
end

0 comments on commit 336236a

Please sign in to comment.