Skip to content

Commit

Permalink
feat: created method to create a new user
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdurbin committed Aug 4, 2024
1 parent f03eb72 commit 6daeb7a
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gem "puma", ">= 5.0"
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ windows jruby ]
Expand Down Expand Up @@ -57,5 +57,4 @@ gem "faraday", "~> 2.10"

gem "webmock", "~> 3.23"


gem "jsonapi-serializer", "~> 2.2"
8 changes: 2 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ GEM
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
base64 (0.2.0)
bcrypt (3.1.20)
bigdecimal (3.1.8)
bootsnap (1.18.3)
msgpack (~> 1.2)
Expand Down Expand Up @@ -107,11 +108,6 @@ GEM
docile (1.4.1)
drb (2.2.1)
erubi (1.13.0)
factory_bot (6.4.6)
activesupport (>= 5.0.0)
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
railties (>= 5.0.0)
faker (3.4.2)
i18n (>= 1.8.11, < 2)
faraday (2.10.1)
Expand Down Expand Up @@ -282,10 +278,10 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
capybara (~> 3.40)
debug
factory_bot_rails
faker (~> 3.4)
faraday (~> 2.10)
jsonapi-serializer (~> 2.2)
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/api/v1/users_controller.rb
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
6 changes: 6 additions & 0 deletions app/models/user.rb
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
17 changes: 17 additions & 0 deletions app/serializers/error_serializer.rb
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
5 changes: 5 additions & 0 deletions app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UserSerializer
include JSONAPI::Serializer

attributes :email
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace :api do
namespace :v1 do
resources :users, only: %i[create]

get '/forecast', to: 'weather#forecast'
end
end
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240804194322_create_users.rb
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
24 changes: 24 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions spec/models/user_spec.rb
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
19 changes: 19 additions & 0 deletions spec/requests/api/v1/user_request_spec.rb
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

0 comments on commit 6daeb7a

Please sign in to comment.