From 12a69335ac1f0c2d3e4c5201d3ca8c74cdbf18f5 Mon Sep 17 00:00:00 2001 From: Clyde Date: Thu, 12 Sep 2024 14:22:46 -0700 Subject: [PATCH] attendee controller, serializer and tests are now up and running --- .../api/v1/attendees_controller.rb | 10 +-- app/controllers/api/v1/events_controller.rb | 41 ++++----- .../api/v1/user_events_controller.rb | 4 + app/models/{user_event.rb => attendee.rb} | 3 +- app/models/event.rb | 4 +- app/models/user.rb | 6 +- app/serializers/attendee_serializer.rb | 6 ++ .../{user_event_spec.rb => attendee_spec.rb} | 3 +- spec/models/event_spec.rb | 4 +- spec/models/user_spec.rb | 6 +- spec/requests/api/v1/attendee_request_spec.rb | 81 ++++++++++++++++++ .../api/v1/user_events_request_spec.rb | 84 ------------------- 12 files changed, 127 insertions(+), 125 deletions(-) rename app/models/{user_event.rb => attendee.rb} (61%) create mode 100644 app/serializers/attendee_serializer.rb rename spec/models/{user_event_spec.rb => attendee_spec.rb} (85%) create mode 100644 spec/requests/api/v1/attendee_request_spec.rb delete mode 100644 spec/requests/api/v1/user_events_request_spec.rb diff --git a/app/controllers/api/v1/attendees_controller.rb b/app/controllers/api/v1/attendees_controller.rb index 7bd53e3..7e5558e 100644 --- a/app/controllers/api/v1/attendees_controller.rb +++ b/app/controllers/api/v1/attendees_controller.rb @@ -3,18 +3,18 @@ def create attendee = Attendee.new(attendee_params) if attendee.save - render json: attendee, status: :created + render json: AttendeeSerializer.new(attendee), status: :created else render json: { errors: attendee.errors.full_messages }, status: :unprocessable_entity end end def destroy - attendee = Attendee.find_by(user_id: params[:user_id], event_id: params[:event_id]) - + attendee = Attendee.find(params[:id]) + # require 'pry'; binding.pry if attendee attendee.destroy - render json: { message: 'Attendee removed successfully' }, status: :ok + render json: { message: 'Attendee removed successfully' }, status: :no_content else render json: { errors: 'Attendee not found' }, status: :not_found end @@ -22,7 +22,7 @@ def destroy def index attendees = Attendee.where(event_id: params[:event_id]) - render json: attendees, status: :ok + render json: AttendeeSerializer.new(attendees), status: :ok end ##### NEED TO SQUARE AWAY JSON RENDERING WITH SERIALIZERS FOR ALL ACTIONS ##### private diff --git a/app/controllers/api/v1/events_controller.rb b/app/controllers/api/v1/events_controller.rb index 7a57ec8..851d56d 100644 --- a/app/controllers/api/v1/events_controller.rb +++ b/app/controllers/api/v1/events_controller.rb @@ -3,35 +3,28 @@ class Api::V1::EventsController < ApplicationController # THAT WILL HAPPEN IN USER_EVENTS CONTROLLER # WE JUST NEED INDEX ACTION TO SHOW ALL EVENTS EVENT.ALL # AND SHOW ACTION TO SHOW A SINGLE EVENT EVENT.FIND(BY ID) - def create - event = Event.new(event_params) + # def create + # event = Event.new(event_params) - if event.save - render json: EventSerializer.new(event), status: :created - else - render json: { errors: event.errors }, status: :unprocessable_entity - end - end + # if event.save + # render json: EventSerializer.new(event), status: :created + # else + # render json: { errors: event.errors }, status: :unprocessable_entity + # end + # end - def destroy - event = Event.find(params[:id]) - event.destroy - end + # def destroy + # event = Event.find(params[:id]) + # event.destroy + # end def index - begin - if params[:user_id] - user = User.find(params[:user_id]) - events = user.events - render json: EventSerializer.new(events) - else - events = Event.all - render json: EventSerializer.new(events) - end - rescue ActiveRecord::RecordNotFound => e - render json: ErrorSerializer.new(e).serialize_json, status: :not_found - end + # if events = Event.all + # render json: EventSerializer.new(events) + # else + # render json: ErrorSerializer.new(e).serialize_json, status: :not_found + # end end private diff --git a/app/controllers/api/v1/user_events_controller.rb b/app/controllers/api/v1/user_events_controller.rb index 5776277..ab624a8 100644 --- a/app/controllers/api/v1/user_events_controller.rb +++ b/app/controllers/api/v1/user_events_controller.rb @@ -14,6 +14,10 @@ def create end end + def index + + end + private params.require(:event).permit(:venue_name, :event_name, :date_time, :artist, :location, :user_id) end \ No newline at end of file diff --git a/app/models/user_event.rb b/app/models/attendee.rb similarity index 61% rename from app/models/user_event.rb rename to app/models/attendee.rb index d7dfaaa..ed2c4ae 100644 --- a/app/models/user_event.rb +++ b/app/models/attendee.rb @@ -1,10 +1,9 @@ require "rails_helper" -class UserEvent < ApplicationRecord +class Attendee < ApplicationRecord belongs_to :user belongs_to :event - validates :host, inclusion: { in: [true, false] } validates :user_id, presence: true validates :event_id, presence: true end \ No newline at end of file diff --git a/app/models/event.rb b/app/models/event.rb index 28614cb..9c84302 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -1,6 +1,6 @@ class Event < ApplicationRecord - has_many :user_events - has_many :users, through: :user_events + has_many :attendees + has_many :users, through: :attendees validates :venue_name, presence: true validates :date_time, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index f3127d0..5b37535 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,10 +1,10 @@ class User < ApplicationRecord - has_many :user_events + has_many :attendees has_many :events - has_many :user_artists, through: :user_artists + has_many :user_artists has_many :artists, through: :user_artists validates :name, presence: true validates :email, presence: true - + end \ No newline at end of file diff --git a/app/serializers/attendee_serializer.rb b/app/serializers/attendee_serializer.rb new file mode 100644 index 0000000..6fadcc1 --- /dev/null +++ b/app/serializers/attendee_serializer.rb @@ -0,0 +1,6 @@ +class AttendeeSerializer + + include JSONAPI::Serializer + attributes :user_id, :event_id + +end \ No newline at end of file diff --git a/spec/models/user_event_spec.rb b/spec/models/attendee_spec.rb similarity index 85% rename from spec/models/user_event_spec.rb rename to spec/models/attendee_spec.rb index 1ba082a..a087de7 100644 --- a/spec/models/user_event_spec.rb +++ b/spec/models/attendee_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe UserEvent, type: :model do +RSpec.describe Attendee, type: :model do describe "validations" do it { should validate_presence_of(:user_id) } it { should validate_presence_of(:event_id) } @@ -10,4 +10,5 @@ it { should belong_to(:user) } it { should belong_to(:event) } end + end \ No newline at end of file diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index 6523fc4..2279137 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -9,7 +9,7 @@ end describe 'associations' do - it { should have_many(:user_events) } - it { should have_many(:users).through(:user_events) } + it { should have_many(:attendees) } + it { should have_many(:users).through(:attendees) } end end \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e87d010..2e189f8 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -7,7 +7,9 @@ end describe 'associations' do - it { should have_many(:user_events) } - it { should have_many(:events).through(:user_events) } + it { should have_many(:attendees) } + it { should have_many(:events) } + it { should have_many(:user_artists) } + it { should have_many(:artists).through(:user_artists) } end end \ No newline at end of file diff --git a/spec/requests/api/v1/attendee_request_spec.rb b/spec/requests/api/v1/attendee_request_spec.rb new file mode 100644 index 0000000..2bba7ea --- /dev/null +++ b/spec/requests/api/v1/attendee_request_spec.rb @@ -0,0 +1,81 @@ +require 'rails_helper' + +RSpec.describe 'Attendees 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') + + @attendee = Attendee.create(user_id: @user1.id, event_id: @event1.id) + end + + # it "returns all attendee by its id" do + # atttendee_id = @attendee.id + + # get "/api/v1/users/#{@user1.id}/attendees/#{atttendee_id}" + + # attendee = JSON.parse(response.body, symbolize_names: true) + + # expect(response).to be_successful + # expect(response.status).to eq(200) + + # expect(attendee).to be_a(Hash) + # expect(attendee).to have_key(:data) + + # expect(attendee[:data][:attributes][:user_id]).to eq(@user1.id) + # expect(attendee[:data][:attributes][:event_id]).to eq(@event1.id) + # end + + it "creates a new attendee" do + user_id = @user2.id + event_id = @event1.id + host = false + + post "/api/v1/attendees", params: { attendee: { user_id: user_id, event_id: event_id } } +# require 'pry'; binding.pry + attendee = JSON.parse(response.body, symbolize_names: true) + + expect(response).to be_successful + expect(response.status).to eq(201) + + expect(attendee).to be_a(Hash) + # require 'pry'; binding.pry + expect(attendee).to have_key(:data) + + expect(attendee[:data][:attributes][:user_id]).to eq(user_id) + expect(attendee[:data][:attributes][:event_id]).to eq(event_id) + + end + + # it "updates an existing attendee" do + # atttendee_id = @attendee.id + # new_user_id = @user2.id + # new_event_id = @event1.id + + # put "/api/v1/attendees/#{atttendee_id}", params: { attendee: { user_id: new_user_id, event_id: new_event_id } } + + # attendee = JSON.parse(response.body, symbolize_names: true) + + # expect(response).to be_successful + # expect(response.status).to eq(200) + + # expect(attendee).to be_a(Hash) + # expect(attendee).to have_key(:data) + + # expect(attendee[:data][:attributes][:user_id]).to eq(new_user_id) + # expect(attendee[:data][:attributes][:event_id]).to eq(new_event_id) + # end + + it "destroys an existing attendee" do + atttendee_id = @attendee.id + + + delete "/api/v1/attendees/#{atttendee_id}" + expect(response).to be_successful + expect(response.status).to eq(204) + + expect(Attendee.find_by(id: atttendee_id)).to be_nil + 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 deleted file mode 100644 index 99c3fb0..0000000 --- a/spec/requests/api/v1/user_events_request_spec.rb +++ /dev/null @@ -1,84 +0,0 @@ -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 - - - 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