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

Remove all comments and add a couple more tests #31

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions app/controllers/api/v1/attendees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def destroy
render json: { errors: 'Attendee not found' }, status: :not_found
end
end

def index
attendees = Attendee.where(event_id: params[:event_id])
render json: AttendeeSerializer.new(attendees), status: :ok
end
##### NEED TO SQUARE AWAY JSON RENDERING WITH SERIALIZERS FOR ALL ACTIONS #####

private

def attendee_params
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
class Api::V1::EventsController < ApplicationController
# THIS CONTROLLER NOW RENDERS JSON FOR ALL DB EVENTS, WE DO NOT NEED A CREATE AND DESTROY ACTION FOR EVENTS IN THIS CONTROLLER
# 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 index
events = Event.all
render json: EventSerializer.new(events)
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/api/v1/user_events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
class Api::V1::UserEventsController < ApplicationController
# this controller will handle creating an event associated with a user
# deleting an event associated with a user
# showing an event associated with a user
# showing all events associated with a user
def create
user = User.find(params[:user_id])
event = user.events.create(event_params)
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class Api::V1::UsersController < ApplicationController

def index
users = User.all
render json: UserSerializer.new(users)
Expand Down Expand Up @@ -40,7 +39,7 @@ def update
end
end

private
private

def user_params
params.require(:user).permit(:name, :email)
Expand Down
1 change: 0 additions & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ class Event < ApplicationRecord
validates :date_time, presence: true
validates :artist, presence: true
validates :location, presence: true

end
1 change: 0 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ class User < ApplicationRecord

validates :name, presence: true
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }

end
2 changes: 1 addition & 1 deletion app/poros/concert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Concert
attr_reader :concert_name, :concert_date, :concert_time, :concert_venue, :concert_country, :id

def initialize(data)
@id = SecureRandom.uuid # json serializer needed an id
@id = SecureRandom.uuid
@concert_name = data[:name]
@concert_date = data[:dates][:start][:localDate]
@concert_time = data[:dates][:start][:localTime]
Expand Down
1 change: 1 addition & 0 deletions app/serializers/artist_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ArtistSerializer
include JSONAPI::Serializer

attributes :name, :musicbrainz_id
end
3 changes: 1 addition & 2 deletions app/serializers/attendee_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class AttendeeSerializer

include JSONAPI::Serializer

attributes :user_id, :event_id

end
1 change: 1 addition & 0 deletions app/serializers/event_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class EventSerializer
include JSONAPI::Serializer

attributes :venue_name, :event_name, :date_time, :artist, :location
end
1 change: 0 additions & 1 deletion app/serializers/user_event_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ class UserEventSerializer
include JSONAPI::Serializer

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

end
1 change: 0 additions & 1 deletion app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ class UserSerializer
include JSONAPI::Serializer

attributes :name, :email, :created_at, :updated_at

end
2 changes: 0 additions & 2 deletions app/services/ticket_master_service.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class TicketMasterService

def self.conn
Faraday.new(url: "https://app.ticketmaster.com/discovery/v2/") do |faraday|

end
end

Expand Down
12 changes: 3 additions & 9 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
namespace :api do
namespace :v1 do
resources :events, only: [:show, :index] # this will be all events in db no matter the user
resources :events, only: [:show, :index]
resources :users, only: [:show, :create, :update, :destroy, :index] do
resources :user_events, only: [:create, :show, :index, :destroy] #user_events controller to pull events for a user (and create and delete)
resources :user_events, only: [:create, :show, :index, :destroy]
end
resources :artists, only: [:index, :create, :destroy]
resources :attendees, only: [:index, :create, :destroy]
get '/concerts', to: 'concert#show' # this will be the top concert for a given artist
get '/concerts', to: 'concert#show'
end
end
end
26 changes: 17 additions & 9 deletions spec/requests/api/v1/artist_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

get "/api/v1/artists", params: { name: "Green" }

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

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

expect(data).to be_a(Hash)
Expand All @@ -35,7 +37,6 @@

expect(data[:data].first[:attributes]).to have_key(:musicbrainz_id)
expect(data[:data].first[:attributes][:musicbrainz_id]).to eq(nil)

end
end

Expand All @@ -54,7 +55,6 @@
end

describe "POST /api/v1/artists" do

let(:user) { create(:user) }
let(:artist_params) { { artist: { name: "Green Day", musicbrainz_id: "id" }, user_id: user.id } }

Expand All @@ -77,8 +77,11 @@
it "returns an error if required parameters are missing" do
post "/api/v1/artists", params: { artist: { name: nil, musicbrainz_id: nil }, user_id: user.id }

expect(response).to have_http_status(:bad_request)
expect(response).to_not be_successful
expect(response.status).to eq(400)

data = JSON.parse(response.body, symbolize_names: true)[:errors]

expect(data).to be_an(Array)

expect(data.first[:detail]).to be_a(String)
Expand All @@ -88,8 +91,11 @@
it "returns an error if the user does not exist" do
post "/api/v1/artists", params: { artist: { name: "Green Day", musicbrainz_id: "id" }, user_id: -1 }

expect(response).to have_http_status(:not_found)
expect(response).to_not be_successful
expect(response.status).to eq(404)

data = JSON.parse(response.body, symbolize_names: true)[:errors]

expect(data).to be_an(Array)
expect(data.first[:detail]).to be_a(String)
expect(data.first[:detail]).to eq("Couldn't find User with 'id'=-1")
Expand All @@ -99,12 +105,14 @@
describe "DELETE /api/v1/artists/:id" do
let(:user) { create(:user) }
let(:artist) { create(:artist) }
let!(:user_artist) { create(:user_artist, user: user, artist: artist) } # Create the association
let!(:user_artist) { create(:user_artist, user: user, artist: artist) }

it "deletes an artist from a user's saved artists" do
delete "/api/v1/artists/#{artist.id}", params: { user_id: user.id }

expect(response).to have_http_status(:ok)
expect(response).to be_successful
expect(response.status).to eq(200)

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

expect(data[:message]).to be_a(String)
Expand All @@ -125,7 +133,7 @@

it 'parse error' do
allow(ArtistFacade).to receive(:search_artists).and_raise(JSON::ParserError.new('unexpected token'))
#^ this is basically mocking the search artist from the facade and then we raise the JSON::ParserError then just simulate where the json response fails at.

get "/api/v1/artists", params: { name: 'invalid_artist' }

expect(response).to_not be_successful
Expand All @@ -144,8 +152,8 @@
get "/api/v1/artists", params: { name: 'artist_with_error' }

expect(response).to_not be_successful
# require 'pry'; binding.pry
expect(response.status).to eq(500)

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

expect(data[:errors]).to be_an(Array)
Expand Down
61 changes: 8 additions & 53 deletions spec/requests/api/v1/attendee_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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]')
Expand All @@ -10,91 +9,50 @@

@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 'cant create attendee' do
user_id = User.create(name: 'John Doe', email: '')
event_id = @event1.id
# host = false


post "/api/v1/attendees", params: { attendee: { user_id: user_id, event_id: event_id } }
# require 'pry'; binding.pry


expect(response).to_not be_successful
expect(response.status).to eq(422)
attendee = JSON.parse(response.body, symbolize_names: true)

expect(attendee).to be_a(Hash)
# require 'pry'; binding.pry

expect(attendee).to have_key(:errors)

expect(attendee[:errors]).to be_a(Array)
expect(attendee[:errors].first).to be_a(String)
expect(attendee[:errors].first).to eq("User must exist")
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
attendee_id = @attendee.id

delete "/api/v1/attendees/#{attendee_id}"


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

Expand All @@ -106,10 +64,9 @@

delete "/api/v1/attendees/#{attendee_id}"


expect(response).to_not be_successful
expect(response.status).to eq(404)
# require 'pry'; binding.pry

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

expect(attendee).to have_key(:errors)
Expand All @@ -119,15 +76,14 @@


describe 'index' do

it 'returns attendees' do
get "/api/v1/attendees?event_id=#{@event1.id}"

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

attendees = JSON.parse(response.body, symbolize_names: true)
# require 'pry'; binding.pry

expect(attendees).to have_key(:data)
expect(attendees).to be_a(Hash)
expect(attendees.count).to eq(1)
Expand All @@ -145,7 +101,6 @@
expect(attendees[:data].first).to have_key(:attributes)
expect(attendees[:data].first[:attributes]).to be_a(Hash)

# require 'pry'; binding.pry
expect(attendees[:data].first[:attributes][:user_id]).to be_a(Integer)
expect(attendees[:data].first[:attributes][:user_id]).to eq(attendees[:data].first[:attributes][:user_id])

Expand Down
Loading