diff --git a/app/controllers/api/v1/attendees_controller.rb b/app/controllers/api/v1/attendees_controller.rb index 3687f66..4de35d6 100644 --- a/app/controllers/api/v1/attendees_controller.rb +++ b/app/controllers/api/v1/attendees_controller.rb @@ -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 diff --git a/app/controllers/api/v1/events_controller.rb b/app/controllers/api/v1/events_controller.rb index 7a46f53..65a25b1 100644 --- a/app/controllers/api/v1/events_controller.rb +++ b/app/controllers/api/v1/events_controller.rb @@ -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) diff --git a/app/controllers/api/v1/user_events_controller.rb b/app/controllers/api/v1/user_events_controller.rb index 90ad9bf..3669841 100644 --- a/app/controllers/api/v1/user_events_controller.rb +++ b/app/controllers/api/v1/user_events_controller.rb @@ -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) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index a914b9b..3652ac3 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -1,5 +1,4 @@ class Api::V1::UsersController < ApplicationController - def index users = User.all render json: UserSerializer.new(users) @@ -40,7 +39,7 @@ def update end end -private + private def user_params params.require(:user).permit(:name, :email) diff --git a/app/models/event.rb b/app/models/event.rb index 9c84302..9ff79ef 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -6,5 +6,4 @@ class Event < ApplicationRecord validates :date_time, presence: true validates :artist, presence: true validates :location, presence: true - end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index fda4e8c..9a1dfc1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,5 +6,4 @@ class User < ApplicationRecord validates :name, presence: true validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP } - end \ No newline at end of file diff --git a/app/poros/concert.rb b/app/poros/concert.rb index 8046241..9504e9f 100644 --- a/app/poros/concert.rb +++ b/app/poros/concert.rb @@ -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] diff --git a/app/serializers/artist_serializer.rb b/app/serializers/artist_serializer.rb index ac5f2cc..e7ab171 100644 --- a/app/serializers/artist_serializer.rb +++ b/app/serializers/artist_serializer.rb @@ -1,4 +1,5 @@ class ArtistSerializer include JSONAPI::Serializer + attributes :name, :musicbrainz_id end \ No newline at end of file diff --git a/app/serializers/attendee_serializer.rb b/app/serializers/attendee_serializer.rb index 6fadcc1..abef24a 100644 --- a/app/serializers/attendee_serializer.rb +++ b/app/serializers/attendee_serializer.rb @@ -1,6 +1,5 @@ class AttendeeSerializer - include JSONAPI::Serializer + attributes :user_id, :event_id - end \ No newline at end of file diff --git a/app/serializers/event_serializer.rb b/app/serializers/event_serializer.rb index a20dd23..d2fc329 100644 --- a/app/serializers/event_serializer.rb +++ b/app/serializers/event_serializer.rb @@ -1,4 +1,5 @@ class EventSerializer include JSONAPI::Serializer + attributes :venue_name, :event_name, :date_time, :artist, :location end \ No newline at end of file diff --git a/app/serializers/user_event_serializer.rb b/app/serializers/user_event_serializer.rb index 1d45968..2c44a75 100644 --- a/app/serializers/user_event_serializer.rb +++ b/app/serializers/user_event_serializer.rb @@ -2,5 +2,4 @@ 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/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index b5e9339..75f2e3c 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -2,5 +2,4 @@ class UserSerializer include JSONAPI::Serializer attributes :name, :email, :created_at, :updated_at - end \ No newline at end of file diff --git a/app/services/ticket_master_service.rb b/app/services/ticket_master_service.rb index fce361c..8304ac8 100644 --- a/app/services/ticket_master_service.rb +++ b/app/services/ticket_master_service.rb @@ -1,8 +1,6 @@ class TicketMasterService - def self.conn Faraday.new(url: "https://app.ticketmaster.com/discovery/v2/") do |faraday| - end end diff --git a/config/routes.rb b/config/routes.rb index 1e9056f..1f435ec 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/requests/api/v1/artist_request_spec.rb b/spec/requests/api/v1/artist_request_spec.rb index d787f79..5b569b5 100644 --- a/spec/requests/api/v1/artist_request_spec.rb +++ b/spec/requests/api/v1/artist_request_spec.rb @@ -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) @@ -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 @@ -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 } } @@ -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) @@ -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") @@ -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) @@ -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 @@ -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) diff --git a/spec/requests/api/v1/attendee_request_spec.rb b/spec/requests/api/v1/attendee_request_spec.rb index c92c0da..8335c68 100644 --- a/spec/requests/api/v1/attendee_request_spec.rb +++ b/spec/requests/api/v1/attendee_request_spec.rb @@ -1,7 +1,6 @@ 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') @@ -10,59 +9,38 @@ @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) @@ -70,31 +48,11 @@ 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) @@ -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) @@ -119,7 +76,6 @@ describe 'index' do - it 'returns attendees' do get "/api/v1/attendees?event_id=#{@event1.id}" @@ -127,7 +83,7 @@ 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) @@ -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]) diff --git a/spec/requests/api/v1/event_spec_request.rb b/spec/requests/api/v1/event_spec_request.rb index f7e68cc..915fafc 100644 --- a/spec/requests/api/v1/event_spec_request.rb +++ b/spec/requests/api/v1/event_spec_request.rb @@ -1,84 +1,6 @@ require 'rails_helper' RSpec.describe "Event" do - - # describe '#creating an event' do - # it 'Creates an event' do - # event_params = { - # event_name: "Bluegrass Week", - # venue_name: "San Antonio Fair", - # date_time: "2024-12-31T20:00:00Z", - # artist: "Marty Robbins", - # location: "San Antonio, TX" - # } - - # post '/api/v1/events', params: event_params - # # require 'pry'; binding.pry - # expect(response).to be_successful - # expect(response.status).to be(201) - - # event = JSON.parse(response.body, symbolize_names: true)[:data][:attributes] - - # expect(event).to have_key(:venue_name) - # expect(event[:venue_name]).to eq("San Antonio Fair") - # expect(event[:venue_name]).to be_a(String) - - # expect(event).to have_key(:event_name) - # expect(event[:event_name]).to eq("Bluegrass Week") - # expect(event[:event_name]).to be_a(String) - - # expect(event).to have_key(:date_time) - # expect(event[:date_time]).to eq("2024-12-31T20:00:00.000Z") - # expect(event[:date_time]).to be_a(String) - - # expect(event).to have_key(:artist) - # expect(event[:artist]).to eq("Marty Robbins") - # expect(event[:artist]).to be_a(String) - - # expect(event).to have_key(:location) - # expect(event[:location]).to eq("San Antonio, TX") - # expect(event[:location]).to be_a(String) - - # end - - # it 'sad path' do - # event_params = { - # event_name: "Bluegrass Week", - # venue_name: "San Antonio Fair", - # date_time: "2024-12-31T20:00:00Z", - # location: "San Antonio, TX" - # } - - # post '/api/v1/events', params: event_params - # # require 'pry'; binding.pry - # expect(response).to_not be_successful - # expect(response.status).to be(422) - - # event = JSON.parse(response.body, symbolize_names: true) - # # require 'pry'; binding.pry - # expect(event).to have_key(:errors) - # expect(event[:errors][:artist].first).to eq("can't be blank") - - # end - # end - - # describe '#delete' do - # it 'deletes an event' do - # event_params = Event.create( - # event_name: "Bluegrass Week", - # venue_name: "San Antonio Fair", - # date_time: "2024-12-31T20:00:00Z", - # artist: "Marty Robbins", - # location: "San Antonio, TX") - - # delete "/api/v1/events/#{event_params.id}" - # # require 'pry'; binding.pry - # expect(response).to be_successful - # expect(response.status).to eq(204) - - # end - # end - describe '#index' do it 'gets all events' do event1 = Event.create( @@ -103,14 +25,14 @@ expect(response).to be_successful json = JSON.parse(response.body, symbolize_names: true) - # require 'pry'; binding.pry expect(json[:data].size).to eq(2) + event = json[:data] expect(event.first[:attributes]).to have_key(:venue_name) expect(event.last[:attributes]).to have_key(:venue_name) -# require 'pry'; binding.pry + expect(event.first[:attributes][:venue_name]).to eq("San Antonio Fair") expect(event.last[:attributes][:venue_name]).to eq("San Antonio") @@ -172,7 +94,6 @@ expect(event).to have_key(:location) expect(event[:location]).to eq("San Antonio, TX") - end end end \ No newline at end of file diff --git a/spec/requests/api/v1/ticketmaster_request_spec.rb b/spec/requests/api/v1/ticketmaster_request_spec.rb index 2483bd9..a52713f 100644 --- a/spec/requests/api/v1/ticketmaster_request_spec.rb +++ b/spec/requests/api/v1/ticketmaster_request_spec.rb @@ -18,7 +18,6 @@ ticket_m_details = JSON.parse(response.body, symbolize_names: true) - # require 'pry'; binding.pry expect(response).to be_successful expect(ticket_m_details).to be_a(Hash) expect(ticket_m_details[:data][:attributes][:concert_name]).to eq("DRAKE") @@ -34,10 +33,8 @@ error = JSON.parse(response.body, symbolize_names: true) - # require 'pry'; binding.pry expect(error).to have_key(:error) expect(error[:error]).to be_a(String) expect(error[:error]).to eq('No concerts found') end - end \ No newline at end of file diff --git a/spec/requests/api/v1/user_request_spec.rb b/spec/requests/api/v1/user_request_spec.rb index b4a13f6..227fa59 100644 --- a/spec/requests/api/v1/user_request_spec.rb +++ b/spec/requests/api/v1/user_request_spec.rb @@ -1,7 +1,6 @@ - require 'rails_helper' +require 'rails_helper' RSpec.describe 'User 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') @@ -71,7 +70,6 @@ put "/api/v1/users/#{user_id}", params: { user: { name: updated_name } } - user = JSON.parse(response.body, symbolize_names: true) expect(response).to be_successful expect(response.status).to eq(200) @@ -88,18 +86,14 @@ user_id = user.id put "/api/v1/users/#{user_id}", params: { user: { email: "invalid-email" } } - expect(response).to_not be_successful expect(response.status).to eq(422) - user = JSON.parse(response.body, symbolize_names: true) - expect(user).to be_a(Hash) expect(user).to have_key(:errors) - expect(user[:errors].first).to be_a(String) expect(user[:errors].first).to eq("Email is invalid") end @@ -121,10 +115,10 @@ expect(response).to_not be_successful expect(response.status).to eq(404) - # require 'pry'; binding.pry + user = JSON.parse(response.body, symbolize_names: true) + expect(user).to be_a(Hash) - # require 'pry'; binding.pry expect(user[:errors]).to be_a(String) expect(user[:errors]).to eq("User not found") end @@ -148,6 +142,4 @@ expect(users[:data].last[:attributes][:name]).to eq(@user2.name) expect(users[:data].last[:attributes][:email]).to eq(@user2.email) end - - end diff --git a/spec/services/ticket_master_service_spec.rb b/spec/services/ticket_master_service_spec.rb index 626946c..9b5d500 100644 --- a/spec/services/ticket_master_service_spec.rb +++ b/spec/services/ticket_master_service_spec.rb @@ -1,22 +1,25 @@ require "rails_helper" RSpec.describe TicketMasterService do - describe "get_events" do it "returns a list of events" do base_url = "https://app.ticketmaster.com/discovery/v2/events.json" json_response_drake = File.read("spec/fixtures/drake_events.json") - # api_key = Rails.application.credentials.ticket_master[:api_key] + api_key = ENV['TICKETMASTER_API_KEY'] artist = "Drake" stub_request(:get, "#{base_url}?keyword=#{artist}&apikey=#{api_key}") .to_return(status: 200, body: json_response_drake, headers: { 'Content-Type' => 'application/json' }) - + events = TicketMasterService.get_events(artist) expect(events).to be_a(Hash) + expect(events).to have_key(:_embedded) + expect(events[:_embedded]).to be_a(Hash) + expect(events[:_embedded]).to have_key(:events) + expect(events[:_embedded][:events]).to be_a(Array) end end end \ No newline at end of file