Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
75 additions
and
27 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -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
15
marcisme/rumbl/priv/repo/migrations/20160522234640_create_user.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 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 |
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
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 |
---|---|---|
@@ -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 |
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
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 @@ | ||
<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 %> |