Skip to content

Commit

Permalink
Merge pull request #13 from concertmate/ca_attendees
Browse files Browse the repository at this point in the history
attendee controller, serializer and tests are now up and running
  • Loading branch information
GBowman1 authored Sep 12, 2024
2 parents 7494c30 + 12a6933 commit 381ca9d
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 125 deletions.
10 changes: 5 additions & 5 deletions app/controllers/api/v1/attendees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ 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
end

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
Expand Down
41 changes: 17 additions & 24 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/api/v1/user_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions app/models/user_event.rb → app/models/attendee.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions app/serializers/attendee_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AttendeeSerializer

include JSONAPI::Serializer
attributes :user_id, :event_id

end
Original file line number Diff line number Diff line change
@@ -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) }
Expand All @@ -10,4 +10,5 @@
it { should belong_to(:user) }
it { should belong_to(:event) }
end

end
4 changes: 2 additions & 2 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
81 changes: 81 additions & 0 deletions spec/requests/api/v1/attendee_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'rails_helper'

RSpec.describe 'Attendees 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')

@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
84 changes: 0 additions & 84 deletions spec/requests/api/v1/user_events_request_spec.rb

This file was deleted.

0 comments on commit 381ca9d

Please sign in to comment.