diff --git a/marcisme/rumbl/lib/rumbl.ex b/marcisme/rumbl/lib/rumbl.ex index 3ea532b..855b62b 100644 --- a/marcisme/rumbl/lib/rumbl.ex +++ b/marcisme/rumbl/lib/rumbl.ex @@ -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]), ] diff --git a/marcisme/rumbl/lib/rumbl/repo.ex b/marcisme/rumbl/lib/rumbl/repo.ex index bffc072..9e30d4c 100644 --- a/marcisme/rumbl/lib/rumbl/repo.ex +++ b/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: "JoseĢ", 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 diff --git a/marcisme/rumbl/priv/repo/migrations/20160522234640_create_user.exs b/marcisme/rumbl/priv/repo/migrations/20160522234640_create_user.exs new file mode 100644 index 0000000..85f96f4 --- /dev/null +++ b/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 diff --git a/marcisme/rumbl/web/controllers/user_controller.ex b/marcisme/rumbl/web/controllers/user_controller.ex index 736ad9c..c755e2d 100644 --- a/marcisme/rumbl/web/controllers/user_controller.ex +++ b/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) diff --git a/marcisme/rumbl/web/models/user.ex b/marcisme/rumbl/web/models/user.ex index b4f69a5..8875d9f 100644 --- a/marcisme/rumbl/web/models/user.ex +++ b/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 diff --git a/marcisme/rumbl/web/router.ex b/marcisme/rumbl/web/router.ex index 2de4253..bda82bd 100644 --- a/marcisme/rumbl/web/router.ex +++ b/marcisme/rumbl/web/router.ex @@ -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. diff --git a/marcisme/rumbl/web/templates/user/new.html.eex b/marcisme/rumbl/web/templates/user/new.html.eex new file mode 100644 index 0000000..e9b3929 --- /dev/null +++ b/marcisme/rumbl/web/templates/user/new.html.eex @@ -0,0 +1,23 @@ +

New User

+ +<%= if @changeset.action do %> +
+

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

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