From 22b84bc533e99ccc8409cdd92ca1141de5fd0bba Mon Sep 17 00:00:00 2001 From: Clyde Date: Wed, 11 Sep 2024 21:09:34 -0700 Subject: [PATCH 1/3] test for get user events created --- Gemfile | 1 + Gemfile.lock | 3 ++ .../api/v1/user_events_request_spec.rb | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 spec/requests/api/v1/user_events_request_spec.rb diff --git a/Gemfile b/Gemfile index 5760a8f..521f962 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,7 @@ gem "jsonapi-serializer" gem "faraday" gem "factory_bot_rails" gem 'bundler-audit' +gem 'brakeman' group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem diff --git a/Gemfile.lock b/Gemfile.lock index d4e6bc2..6307feb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -81,6 +81,8 @@ GEM bigdecimal (3.1.8) bootsnap (1.18.4) msgpack (~> 1.2) + brakeman (6.2.1) + racc builder (3.3.0) bundler-audit (0.9.2) bundler (>= 1.2.0, < 3) @@ -269,6 +271,7 @@ PLATFORMS DEPENDENCIES bootsnap + brakeman bundler-audit debug factory_bot_rails diff --git a/spec/requests/api/v1/user_events_request_spec.rb b/spec/requests/api/v1/user_events_request_spec.rb new file mode 100644 index 0000000..1df8f1a --- /dev/null +++ b/spec/requests/api/v1/user_events_request_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +RSpec.describe 'UserEvents API', type: :request do + + before :each do + @user1 = User.create(name: 'John Doe', email: 'john@email.com') + @user2 = User.create(name: 'Jane Doe', email: 'jane@email.com') + + @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 +end \ No newline at end of file From 73cddf03ad25bb267df5c62e599dfeb01e742d1f Mon Sep 17 00:00:00 2001 From: Clyde Date: Wed, 11 Sep 2024 21:13:41 -0700 Subject: [PATCH 2/3] user_events get is working --- app/controllers/api/v1/user_events_controller.rb | 6 ++++++ app/serializers/user_event_serializer.rb | 6 ++++++ config/routes.rb | 1 + 3 files changed, 13 insertions(+) create mode 100644 app/controllers/api/v1/user_events_controller.rb create mode 100644 app/serializers/user_event_serializer.rb diff --git a/app/controllers/api/v1/user_events_controller.rb b/app/controllers/api/v1/user_events_controller.rb new file mode 100644 index 0000000..b035eb0 --- /dev/null +++ b/app/controllers/api/v1/user_events_controller.rb @@ -0,0 +1,6 @@ +class Api::V1::UserEventsController < ApplicationController + def show + user_event = UserEvent.find(params[:id]) + render json: UserEventSerializer.new(user_event) + end +end \ No newline at end of file diff --git a/app/serializers/user_event_serializer.rb b/app/serializers/user_event_serializer.rb new file mode 100644 index 0000000..1d45968 --- /dev/null +++ b/app/serializers/user_event_serializer.rb @@ -0,0 +1,6 @@ +class UserEventSerializer + include JSONAPI::Serializer + + attributes :user_id, :event_id, :host, :created_at, :updated_at + +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 3c82e88..58a137f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,7 @@ namespace :api do namespace :v1 do resources :users, only: [:show, :create, :update, :destroy] + resources :user_events, only: [:show, :create, :update, :destroy] end end end From 44c345fa1d4a1b79691b1d79062cbd895d3f9625 Mon Sep 17 00:00:00 2001 From: Clyde Date: Wed, 11 Sep 2024 21:38:13 -0700 Subject: [PATCH 3/3] user_events full crud complete --- .../api/v1/user_events_controller.rb | 35 ++++++++++++ .../api/v1/user_events_request_spec.rb | 53 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/app/controllers/api/v1/user_events_controller.rb b/app/controllers/api/v1/user_events_controller.rb index b035eb0..532f094 100644 --- a/app/controllers/api/v1/user_events_controller.rb +++ b/app/controllers/api/v1/user_events_controller.rb @@ -3,4 +3,39 @@ 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 \ No newline at end of file diff --git a/spec/requests/api/v1/user_events_request_spec.rb b/spec/requests/api/v1/user_events_request_spec.rb index 1df8f1a..99c3fb0 100644 --- a/spec/requests/api/v1/user_events_request_spec.rb +++ b/spec/requests/api/v1/user_events_request_spec.rb @@ -28,4 +28,57 @@ 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 \ No newline at end of file