Skip to content

Commit d5c626d

Browse files
committed
adds /register endpoint with unique email address (fail if email already registered) for #26
1 parent 43222e4 commit d5c626d

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

README.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,41 @@ Or, anyone who want's their own easy to deploy/scale "backend".
2727
If you are `new` to Elixir or Phoenix please
2828
see the Learning section below.
2929

30-
### Try It!
30+
### Try It! (Heroku)
3131

3232
Visit: https://dwylapi.herokuapp.com/rows
3333

3434
Create a new Person (User)
3535
```
36-
curl -H "Content-Type: application/json" -X POST -d '{"person": {"name":"Jimmy","email":"[email protected]"}}' https://dwylapi.herokuapp.com/people
36+
curl -H "Content-Type: application/json" -X POST -d '{"person": {"name":"Simon","email":"[email protected]"}}' https://dwylapi.herokuapp.com/people
3737
```
3838

3939
Now create a Row (record):
4040
```
41-
curl -H "Content-Type: application/json" -X POST -d '{"rows": {"id":1, "title":"Hello World","body":"This is my amazing post!", "people_id":"1"}}' https://dwylapi.herokuapp.com/rows
41+
curl -H "Content-Type: application/json" -X POST -d '{"rows": {"id":1, "title":"Simon Says Hi!","body":"This is my amazing post!", "people_id":"2"}}' https://dwylapi.herokuapp.com/rows
4242
```
4343
You should see your record when you visit:
4444
https://dwylapi.herokuapp.com/rows
4545

4646

47+
### localhost
48+
49+
Create a new Person (User)
50+
```
51+
curl -H "Content-Type: application/json" -X POST -d '{"person": {"name":"Simon","email":"[email protected]"}}' http://localhost:4000/people
52+
```
53+
54+
Now create a Row (record):
55+
```
56+
curl -H "Content-Type: application/json" -X POST -d '{"rows": {"id":1, "title":"Simon Says Hi!","body":"This is my amazing post!", "people_id":"22"}}' http://localhost:4000/rows
57+
```
58+
59+
/register a new person
60+
```
61+
```
62+
curl -H "Content-Type: application/json" -X POST -d '{"person": {"name":"Simon","email":"[email protected]"}}' http://localhost:4000/register
63+
```
64+
4765
4866
## *Required* Environment Variables
4967
@@ -65,7 +83,7 @@ To start your Phoenix app:
6583
+ (_Optionally_) Seed the database with dummy data: `mix run priv/repo/seeds.exs`
6684
+ Start Phoenix endpoint with `mix phx.server`
6785

68-
Now you can visit [`localhost:4000`](http://localhost:4000) from your browser.
86+
Now you can visit [`localhost:4000/rows`](http://localhost:4000/rows) from your browser.
6987

7088
<!--
7189

lib/api/people/person.ex

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Api.People.Person do
99
field :name, :string
1010
has_many :content, Api.Content.Rows, foreign_key: :people_people_id
1111

12+
1213
timestamps()
1314
end
1415

@@ -17,5 +18,6 @@ defmodule Api.People.Person do
1718
person
1819
|> cast(attrs, [:name, :email])
1920
|> validate_required([:name, :email])
21+
|> unique_constraint(:email)
2022
end
2123
end

lib/api/web/router.ex

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Api.Web.Router do
99
pipe_through :api
1010

1111
resources "/people", PersonController, except: [:new, :edit]
12+
post "/register", PersonController, :create
1213
resources "/rows", RowsController, except: [:new, :edit]
1314
end
1415

lib/api/web/views/rows_view.ex

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ defmodule Api.Web.RowsView do
1111
end
1212

1313
def render("rows.json", %{rows: rows}) do
14-
%{id: rows.id,
14+
%{
15+
id: rows.id,
1516
title: rows.title,
16-
body: rows.body}
17+
body: rows.body,
18+
people_id: rows.people_id,
19+
inserted_at: rows.inserted_at
20+
}
1721
end
1822
end

priv/repo/migrations/20170606090822_create_people_person.exs

+2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ defmodule Api.Repo.Migrations.CreateApi.People.Person do
99
timestamps()
1010
end
1111

12+
create unique_index(:people, [:email])
13+
1214
end
1315
end

0 commit comments

Comments
 (0)