Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ca user events crud #5

Merged
merged 4 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ gem "bootsnap", require: false
gem "jsonapi-serializer"
gem "faraday"
gem "factory_bot_rails"
gem 'bundler-audit', require: false
gem 'bundler-audit'
gem 'brakeman'

group :development, :test do
Expand Down
41 changes: 41 additions & 0 deletions app/controllers/api/v1/user_events_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Api::V1::UserEventsController < ApplicationController
def show
user_event = UserEvent.find(params[:id])
render json: UserEventSerializer.new(user_event)
end

def create
user_event = UserEvent.new(user_event_params)
if user_event.save
render json: UserEventSerializer.new(user_event), status: :created
else
render json: { errors: user_event.errors.full_messages }, status: :unprocessable_entity
end
end

def destroy
user_event = UserEvent.find(params[:id])

if user_event.destroy
render json: { message: "UserEvent has been deleted" }, status: :no_content
else
render json: { errors: user_event.errors.full_messages }, status: :unprocessable_entity
end
end

def update
user_event = UserEvent.find(params[:id])

if user_event.update(user_event_params)
render json: UserEventSerializer.new(user_event), status: :ok
else
render json: { errors: user_event.errors.full_messages }, status: :unprocessable_entity
end
end

private

def user_event_params
params.require(:user_event).permit(:user_id, :event_id, :host)
end
end
6 changes: 6 additions & 0 deletions app/serializers/user_event_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UserEventSerializer
include JSONAPI::Serializer

attributes :user_id, :event_id, :host, :created_at, :updated_at

end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace :v1 do
resources :events, only: [:show, :create, :update, :destroy]
resources :users, only: [:show, :create, :update, :destroy]
resources :user_events, only: [:show, :create, :update, :destroy]
end
end
end
84 changes: 84 additions & 0 deletions spec/requests/api/v1/user_events_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'rails_helper'

RSpec.describe 'UserEvents API', type: :request do

before :each do
@user1 = User.create(name: 'John Doe', email: '[email protected]')
@user2 = User.create(name: 'Jane Doe', email: '[email protected]')

@event1 = Event.create(venue_name: 'The Fillmore', date_time: '2021-09-01T20:00:00.000Z', artist: 'The Beatles', location: 'San Francisco')

@user_event1 = UserEvent.create(user_id: @user1.id, event_id: @event1.id, host: true)
end

it "sends a user_event by its id" do
user_event_id = @user_event1.id

get "/api/v1/user_events/#{user_event_id}"

user_event = JSON.parse(response.body, symbolize_names: true)

expect(response).to be_successful
expect(response.status).to eq(200)

expect(user_event).to be_a(Hash)
expect(user_event).to have_key(:data)

expect(user_event[:data][:attributes][:user_id]).to eq(@user1.id)
expect(user_event[:data][:attributes][:event_id]).to eq(@event1.id)
expect(user_event[:data][:attributes][:host]).to eq(true)
end


it "creates a new user_event" do
user_id = @user2.id
event_id = @event1.id
host = false

post "/api/v1/user_events", params: { user_event: { user_id: user_id, event_id: event_id, host: host } }
# require 'pry'; binding.pry
user_event = JSON.parse(response.body, symbolize_names: true)

expect(response).to be_successful
expect(response.status).to eq(201)

expect(user_event).to be_a(Hash)
expect(user_event).to have_key(:data)

expect(user_event[:data][:attributes][:user_id]).to eq(user_id)
expect(user_event[:data][:attributes][:event_id]).to eq(event_id)
expect(user_event[:data][:attributes][:host]).to eq(host)
end

it "updates an existing user_event" do
user_event_id = @user_event1.id
new_user_id = @user2.id
new_event_id = @event1.id
new_host = false

put "/api/v1/user_events/#{user_event_id}", params: { user_event: { user_id: new_user_id, event_id: new_event_id, host: new_host } }

user_event = JSON.parse(response.body, symbolize_names: true)

expect(response).to be_successful
expect(response.status).to eq(200)

expect(user_event).to be_a(Hash)
expect(user_event).to have_key(:data)

expect(user_event[:data][:attributes][:user_id]).to eq(new_user_id)
expect(user_event[:data][:attributes][:event_id]).to eq(new_event_id)
expect(user_event[:data][:attributes][:host]).to eq(new_host)
end

it "destroys an existing user_event" do
user_event_id = @user_event1.id

delete "/api/v1/user_events/#{user_event_id}"

expect(response).to be_successful
expect(response.status).to eq(204)

expect(UserEvent.find_by(id: user_event_id)).to be_nil
end
end
Loading