Skip to content

Commit

Permalink
Add chapter 4 work
Browse files Browse the repository at this point in the history
  • Loading branch information
marcisme committed May 23, 2016
1 parent dcee68b commit 8eb0c95
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 27 deletions.
2 changes: 1 addition & 1 deletion marcisme/rumbl/lib/rumbl.ex
Expand Up @@ -10,7 +10,7 @@ defmodule Rumbl do
# Start the endpoint when the application starts
supervisor(Rumbl.Endpoint, []),
# Start the Ecto repository
# supervisor(Rumbl.Repo, []),
supervisor(Rumbl.Repo, []),
# Here you could define other workers and supervisors as children
# worker(Rumbl.Worker, [arg1, arg2, arg3]),
]
Expand Down
24 changes: 1 addition & 23 deletions marcisme/rumbl/lib/rumbl/repo.ex
@@ -1,25 +1,3 @@
defmodule Rumbl.Repo do

@moduledoc """
In memory repository.
"""

def all(Rumbl.User) do
[%Rumbl.User{id: "1", name: "José", username: "josevalim", password: "elixir"},
%Rumbl.User{id: "2", name: "Bruce", username: "redrapids", password: "7langs"},
%Rumbl.User{id: "3", name: "Chris", username: "chrismccord", password: "phx"}]
end

def all(_module), do: []

def get(module, id) do
Enum.find all(module), fn map -> map.id == id end
end

def get_by(module, params) do
Enum.find all(module), fn map ->
Enum.all?(params, fn {key, val} -> Map.get(map, key) == val end)
end
end

use Ecto.Repo, otp_app: :rumbl
end
15 changes: 15 additions & 0 deletions marcisme/rumbl/priv/repo/migrations/20160522234640_create_user.exs
@@ -0,0 +1,15 @@
defmodule Rumbl.Repo.Migrations.CreateUser do
use Ecto.Migration

def change do
create table(:users) do
add :name, :string
add :username, :string, null: false
add :password_hash, :string

timestamps
end

create unique_index(:users, [:username])
end
end
18 changes: 18 additions & 0 deletions marcisme/rumbl/web/controllers/user_controller.ex
@@ -1,5 +1,23 @@
defmodule Rumbl.UserController do
use Rumbl.Web, :controller
alias Rumbl.User

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.name} created!")
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end

def index(conn, _params) do
users = Repo.all(Rumbl.User)
Expand Down
17 changes: 16 additions & 1 deletion marcisme/rumbl/web/models/user.ex
@@ -1,3 +1,18 @@
defmodule Rumbl.User do
defstruct [:id, :name, :username, :password]
use Rumbl.Web, :model

schema "users" do
field :name, :string
field :username, :string
field :password, :string, virtual: true
field :password_hash, :string

timestamps
end

def changeset(model, params \\ :empty) do
model
|> cast(params, ~w(name username), [])
|> validate_length(:username, min: 1, max: 20)
end
end
3 changes: 1 addition & 2 deletions marcisme/rumbl/web/router.ex
Expand Up @@ -16,9 +16,8 @@ defmodule Rumbl.Router do
scope "/", Rumbl do
pipe_through :browser # Use the default browser stack

get "/users", UserController, :index
get "/users/:id", UserController, :show
get "/", PageController, :index
resources "/users", UserController, only: [:index, :show, :new, :create]
end

# Other scopes may use custom stacks.
Expand Down
23 changes: 23 additions & 0 deletions marcisme/rumbl/web/templates/user/new.html.eex
@@ -0,0 +1,23 @@
<h1>New User</h1>

<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>

<%= form_for @changeset, user_path(@conn, :create), fn f -> %>
<div class="form-group">
<%= text_input f, :name, placeholder: "Name", class: "form-control" %>
<%= error_tag f, :name %>
</div>
<div class="form-group">
<%= text_input f, :username, placeholder: "Username", class: "form-control" %>
<%= error_tag f, :username %>
</div>
<div class="form-group">
<%= password_input f, :password, placeholder: "Password", class: "form-control" %>
<%= error_tag f, :password %>
</div>
<%= submit "Create User", class: "btn btn-primary" %>
<% end %>

0 comments on commit 8eb0c95

Please sign in to comment.