-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created method to create a new user
- Loading branch information
1 parent
f03eb72
commit 6daeb7a
Showing
11 changed files
with
111 additions
and
8 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
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,16 @@ | ||
class Api::V1::UsersController < ApplicationController | ||
def create | ||
@user = User.new(user_params) | ||
if @user.save | ||
render json: UserSerializer.new(@user), status: :created | ||
else | ||
render ErrorSerializer.new(@user.errors).serialize_json, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def user_params | ||
params.permit(:email, :password, :password_confirmation) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class User < ApplicationRecord | ||
validates :email, uniqueness: true, presence: true | ||
validates_presence_of :password | ||
|
||
has_secure_password | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class ErrorSerializer | ||
def initialize(error_object) | ||
@error_object = error_object | ||
end | ||
|
||
def serialize_json | ||
binding.pry | ||
{ | ||
errors: [ | ||
{ | ||
status: @error_object.status_code, | ||
title: @error_object.message | ||
} | ||
] | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class UserSerializer | ||
include JSONAPI::Serializer | ||
|
||
attributes :email | ||
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,10 @@ | ||
class CreateUsers < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :users do |t| | ||
t.string :email | ||
t.string :password_digest | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,9 @@ | ||
require 'rails_helper' | ||
|
||
describe User, type: :model do | ||
describe "validations" do | ||
it {should validate_presence_of(:email)} | ||
it {should validate_uniqueness_of(:email)} | ||
it {should validate_presence_of(:password)} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "User api endpoints" do | ||
describe 'create a new user' do | ||
it 'allows a user to create a user', :vcr do | ||
user_params = { | ||
email: '[email protected]', | ||
password: 'password', | ||
password_confirmation: 'password' | ||
} | ||
|
||
post api_v1_users_path, params: user_params | ||
expect(response).to have_http_status(:created) | ||
|
||
user = User.last | ||
expect(user.email).to eq("[email protected]") | ||
end | ||
end | ||
end |