Skip to content

Commit 2a58021

Browse files
committed
new user model
1 parent 0273a6c commit 2a58021

File tree

8 files changed

+95
-24
lines changed

8 files changed

+95
-24
lines changed

test-api/Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ gem 'puma', '~> 3.11'
1414
# Use Redis adapter to run Action Cable in production
1515
# gem 'redis', '~> 4.0'
1616
# Use ActiveModel has_secure_password
17-
# gem 'bcrypt', '~> 3.1.7'
17+
gem 'bcrypt', '~> 3.1.7'
1818

1919
# Use ActiveStorage variant
2020
# gem 'mini_magick', '~> 4.8'
2121

2222
# Use Capistrano for deployment
2323
# gem 'capistrano-rails', group: :development
2424

25+
gem 'active_model_serializers'
26+
2527
# Reduces boot times through caching; required in config/boot.rb
2628
gem 'bootsnap', '>= 1.1.0', require: false
2729

test-api/Gemfile.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ GEM
2424
erubi (~> 1.4)
2525
rails-dom-testing (~> 2.0)
2626
rails-html-sanitizer (~> 1.0, >= 1.0.3)
27+
active_model_serializers (0.10.8)
28+
actionpack (>= 4.1, < 6)
29+
activemodel (>= 4.1, < 6)
30+
case_transform (>= 0.2)
31+
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
2732
activejob (5.2.1)
2833
activesupport (= 5.2.1)
2934
globalid (>= 0.3.6)
@@ -43,10 +48,13 @@ GEM
4348
minitest (~> 5.1)
4449
tzinfo (~> 1.1)
4550
arel (9.0.0)
51+
bcrypt (3.1.12)
4652
bootsnap (1.3.2)
4753
msgpack (~> 1.0)
4854
builder (3.2.3)
4955
byebug (10.0.2)
56+
case_transform (0.2)
57+
activesupport
5058
concurrent-ruby (1.1.3)
5159
crass (1.0.4)
5260
erubi (1.7.1)
@@ -55,6 +63,7 @@ GEM
5563
activesupport (>= 4.2.0)
5664
i18n (1.1.1)
5765
concurrent-ruby (~> 1.0)
66+
jsonapi-renderer (0.2.0)
5867
listen (3.1.5)
5968
rb-fsevent (~> 0.9, >= 0.9.4)
6069
rb-inotify (~> 0.9, >= 0.9.7)
@@ -134,6 +143,8 @@ PLATFORMS
134143
ruby
135144

136145
DEPENDENCIES
146+
active_model_serializers
147+
bcrypt (~> 3.1.7)
137148
bootsnap (>= 1.1.0)
138149
byebug
139150
listen (>= 3.0.5, < 3.2)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class UsersController < ApplicationController
2+
3+
# POST /register
4+
def register
5+
@user = User.create(user_params)
6+
if @user.save
7+
response = { message: 'User created successfully'}
8+
render json: response, status: :created
9+
else
10+
render json: @user.errors, status: :bad
11+
end
12+
end
13+
14+
private
15+
16+
def user_params
17+
params.permit(
18+
:name,
19+
:email,
20+
:password
21+
)
22+
end
23+
end

test-api/app/models/user.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class User < ApplicationRecord
2+
#Validations
3+
validates_presence_of :name, :email, :password_digest
4+
validates :email, uniqueness: true
5+
6+
#encrypt password
7+
has_secure_password
8+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class MovieSerializer < ActiveModel::Serializer
2+
attributes :id
3+
end

test-api/client/src/components/Button.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
1-
import React, { Component } from 'react';
1+
import React, { Component } from "react";
22

33
class Button extends Component {
4-
54
state = {
5+
results: []
6+
};
67

7-
results: []
8-
}
9-
10-
11-
callApi = async() => {
12-
13-
const api_call = await fetch('http://localhost:3000/api/v1/movies');
8+
callApi = async () => {
9+
const api_call = await fetch("http://localhost:3000/api/v1/movies");
1410

15-
const data = await api_call.json();
16-
17-
this.setState({
18-
19-
results: data
20-
});
21-
}
11+
const data = await api_call.json();
2212

13+
this.setState({
14+
results: data
15+
});
16+
};
2317

2418
render() {
25-
2619
return (
2720
<div>
28-
<div className="card container mt-3">
21+
<div className="card container mt-3">
2922
<div className="card-body">
3023
<div className="row">
31-
<center>
32-
<button className="btn btn-primary" onClick={this.callApi}>Test Call!</button><br />
33-
</center><br /><br /><br /><br /><br />
34-
<p className="m-5">{this.state.results.map((obj) => <li>{obj.name}</li>)}</p>
24+
<center>
25+
<button className="btn btn-primary" onClick={this.callApi}>
26+
Test Call!
27+
</button>
28+
<br />
29+
</center>
30+
<br />
31+
<br />
32+
<br />
33+
<br />
34+
<br />
35+
<p className="m-5">
36+
{this.state.results.map(obj => (
37+
<li>{obj.name}</li>
38+
))}
39+
</p>
3540
</div>
3641
</div>
3742
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateUsers < ActiveRecord::Migration[5.2]
2+
def change
3+
create_table :users do |t|
4+
t.string :name
5+
t.string :email
6+
t.string :password_digest
7+
8+
t.timestamps
9+
end
10+
end
11+
end

test-api/db/schema.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2018_11_12_213959) do
13+
ActiveRecord::Schema.define(version: 2018_11_28_092508) do
1414

1515
create_table "movies", force: :cascade do |t|
1616
t.string "name"
@@ -19,4 +19,12 @@
1919
t.datetime "updated_at", null: false
2020
end
2121

22+
create_table "users", force: :cascade do |t|
23+
t.string "name"
24+
t.string "email"
25+
t.string "password_digest"
26+
t.datetime "created_at", null: false
27+
t.datetime "updated_at", null: false
28+
end
29+
2230
end

0 commit comments

Comments
 (0)