diff --git a/app/controllers/api_music_sessions_controller.rb b/app/controllers/api_music_sessions_controller.rb
new file mode 100644
index 000000000..72e302947
--- /dev/null
+++ b/app/controllers/api_music_sessions_controller.rb
@@ -0,0 +1,54 @@
+class ApiMusicSessionsController < ApplicationController
+
+ # have to be signed in currently to see this screen
+ before_filter :signed_in_user
+
+ respond_to :json
+
+ def index
+ @music_sessions = MusicSession.paginate(page: params[:page])
+ end
+
+ def create
+ @music_session = MusicSession.new()
+ @music_session.creator = current_user
+ @music_session.description = params[:description]
+ @music_session.save
+ respond_with @music_session, responder: ApiResponder, :location => api_session_detail_url(@music_session)
+ end
+
+ def show
+ @music_session = MusicSession.find(params[:id])
+ end
+
+ def delete
+ @music_session = MusicSession.find(params[:id])
+ @music_session.delete
+
+ respond_with @music_session, responder: ApiResponder
+ end
+
+ def client_show
+ @music_session_client = MusicSessionClient.find(params[:id])
+ end
+
+ def client_create
+ @music_session = MusicSession.find(params[:id])
+
+ @music_session_client = MusicSessionClient.new()
+ @music_session_client.ip_address = params[:ip_address]
+ @music_session_client.music_session = @music_session
+ @music_session_client.user = current_user
+ @music_session_client.save
+
+ respond_with @music_session_client, responder: ApiResponder, :location => api_session_client_detail_url(@music_session_client)
+ end
+
+ def client_delete
+ @music_session = MusicSession.find(params[:id])
+ @music_session_client = MusicSessionClient.find_by_user_id_and_music_session_id(current_user, @music_session)
+ @music_session_client.delete
+
+ respond_with @music_session_client, responder: ApiResponder
+ end
+end
\ No newline at end of file
diff --git a/app/controllers/music_sessions_controller.rb b/app/controllers/music_sessions_controller.rb
index 74cff92a4..c72f6f22f 100644
--- a/app/controllers/music_sessions_controller.rb
+++ b/app/controllers/music_sessions_controller.rb
@@ -3,36 +3,14 @@ class MusicSessionsController < ApplicationController
# have to be signed in currently to see this screen
before_filter :signed_in_user
- respond_to :html, :xml, :json
-
-
- def api_index
- @music_sessions = MusicSession.paginate(page: params[:page])
- end
-
- def api_create
- @music_session = MusicSession.new()
- @music_session.creator = current_user
- @music_session.description = params[:description]
- @music_session.save
- respond_with @music_session, responder: ApiResponder, :location => api_session_detail_url(@music_session)
- end
-
- def api_show
- @music_session = MusicSession.find(params[:id])
- end
-
- def api_member_create
- @music_session = MusicSession.find(params[:id])
-
- MusicSessionClient.ip_address = params[:ip_address]
- end
+ respond_to :html
def index
@music_sessions = MusicSession.paginate(page: params[:page])
end
def show
+ @music_session = MusicSession.find(params[:id])
# use gon to pass variables into javascript
gon.websocket_gateway_uri = Rails.application.config.websocket_gateway_uri
diff --git a/app/views/api_music_sessions/index.rabl b/app/views/api_music_sessions/index.rabl
new file mode 100644
index 000000000..4b7420de3
--- /dev/null
+++ b/app/views/api_music_sessions/index.rabl
@@ -0,0 +1,3 @@
+object @music_sessions
+
+extends "api_music_sessions/show"
diff --git a/app/views/api_music_sessions/member_show.rabl b/app/views/api_music_sessions/member_show.rabl
new file mode 100644
index 000000000..1f5ffadb2
--- /dev/null
+++ b/app/views/api_music_sessions/member_show.rabl
@@ -0,0 +1,3 @@
+object @music_session_client
+
+attributes :id, :ip_address
\ No newline at end of file
diff --git a/app/views/music_sessions/api_show.rabl b/app/views/api_music_sessions/show.rabl
similarity index 100%
rename from app/views/music_sessions/api_show.rabl
rename to app/views/api_music_sessions/show.rabl
diff --git a/app/views/jam_ruby/jam_sessions/_music_session.html.erb b/app/views/jam_ruby/music_sessions/_music_session.html.erb
similarity index 77%
rename from app/views/jam_ruby/jam_sessions/_music_session.html.erb
rename to app/views/jam_ruby/music_sessions/_music_session.html.erb
index 992a7cdaa..5f2d5a861 100644
--- a/app/views/jam_ruby/jam_sessions/_music_session.html.erb
+++ b/app/views/jam_ruby/music_sessions/_music_session.html.erb
@@ -1,5 +1,5 @@
- <%= link_to music_session.name, music_session %>
+ <%= link_to music_session.description, music_session %>
<% if music_session.creator == current_user || current_user.admin %>
| <%= link_to "delete", music_session, method: :delete,
data: { confirm: "You sure?" } %>
diff --git a/app/views/music_sessions/api_index.rabl b/app/views/music_sessions/api_index.rabl
deleted file mode 100644
index e7d33121c..000000000
--- a/app/views/music_sessions/api_index.rabl
+++ /dev/null
@@ -1,3 +0,0 @@
-object @music_sessions
-
-extends "music_sessions/api_show"
diff --git a/app/views/music_sessions/index.html.erb b/app/views/music_sessions/index.html.erb
index 8fab3cec0..9099cc92a 100644
--- a/app/views/music_sessions/index.html.erb
+++ b/app/views/music_sessions/index.html.erb
@@ -1,5 +1,5 @@
-<% provide(:title, 'Jam Sessions') %>
-Jam Sessions
+<% provide(:title, 'Music Sessions') %>
+Music Sessions
<%= will_paginate %>
diff --git a/config/routes.rb b/config/routes.rb
index ff861752e..e107e1774 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -23,8 +23,13 @@ SampleApp::Application.routes.draw do
match '/client', to: 'clients#index'
scope '/api' do
- match '/sessions/:id' => 'music_sessions#api_show', :via => :get, :as => 'api_session_detail'
- match '/sessions' => 'music_sessions#api_index', :via => :get
- match '/sessions' => 'music_sessions#api_create', :via => :post
+ match '/sessions/:id/musician' => 'api_music_sessions#client_show', :via => :get, :as => 'api_session_client_detail'
+ match '/sessions/:id/musician' => 'api_music_sessions#client_create', :via => :post
+ match '/sessions/:id/musician' => 'api_music_sessions#client_delete', :via => :delete
+ match '/sessions/:id' => 'api_music_sessions#show', :via => :get, :as => 'api_session_detail'
+ match '/sessions/:id' => 'api_music_sessions#delete', :via => :delete
+ match '/sessions' => 'api_music_sessions#index', :via => :get
+ match '/sessions' => 'api_music_sessions#create', :via => :post
+
end
end
diff --git a/spec/requests/authentication_pages_spec.rb b/spec/requests/authentication_pages_spec.rb
index 8c126f62e..4612417eb 100644
--- a/spec/requests/authentication_pages_spec.rb
+++ b/spec/requests/authentication_pages_spec.rb
@@ -34,7 +34,8 @@ describe "Authentication" do
click_button "Sign in"
end
- it { should have_selector('title', text: user.name) }
+ # it now goes to /music_sessions
+ it { should have_selector('title', text: "Music Sessions") }
it { should have_link('Users', href: users_path) }
it { should have_link('Profile', href: user_path(user)) }
@@ -78,7 +79,8 @@ describe "Authentication" do
end
it "should render the default (profile) page" do
- page.should have_selector('title', text: user.name)
+ # it now goes to /music_sessions
+ page.should have_selector('title', text: "Music Sessions")
end
end
end
diff --git a/spec/requests/music_session_pages_spec.rb b/spec/requests/music_session_pages_spec.rb
index 568a2dc1e..c322102ff 100644
--- a/spec/requests/music_session_pages_spec.rb
+++ b/spec/requests/music_session_pages_spec.rb
@@ -6,11 +6,12 @@ describe "Music Session API ", :type => :api do
subject { page }
-
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
- before do 2
+ before do
#sign_in user
+ MusicSession.delete_all
+
post '/sessions', "session[email]" => user.email, "session[password]" => user.password
rack_mock_session.cookie_jar["remember_token"].should == user.remember_token
end
@@ -34,5 +35,72 @@ describe "Music Session API ", :type => :api do
list[0]["id"].should == music_session["id"]
end
+
+
+ it "should delete session" do
+ # check that there are no sessions
+ get '/api/sessions.json'
+ list = JSON.parse(last_response.body)
+ list.length.should == 0
+
+ # create the session
+ post '/api/sessions.json', '{"description" : "a session"}', "CONTENT_TYPE" => 'application/json'
+
+ # now fetch it's data
+ get last_response.headers["Location"]
+ music_session = JSON.parse(last_response.body)
+
+ get '/api/sessions.json'
+ list = JSON.parse(last_response.body)
+ list.length.should == 1
+
+ delete "/api/sessions/#{music_session["id"]}.json", '', "CONTENT_TYPE" => 'application/json'
+ last_response.status.should eql(204)
+
+ get '/api/sessions.json'
+ list = JSON.parse(last_response.body)
+ list.length.should == 0
+ end
+
+
+ it "should add/remove member from session" do
+ # create the session
+ post '/api/sessions.json', '{"description" : "a session"}', "CONTENT_TYPE" => 'application/json'
+
+ # now fetch it's data
+ get last_response.headers["Location"]
+ music_session = JSON.parse(last_response.body)
+ music_session["musicians"].length.should == 0
+
+ # create a member
+ post "/api/sessions/#{music_session["id"]}/musician.json", '{ "ip_address" : "1.2.3.4" }', "CONTENT_TYPE" => 'application/json'
+ last_response.status.should eql(201)
+
+ musician = JSON.parse(last_response.body)
+
+ # re-fetch the session now that there is a musician
+ get "/api/sessions/#{music_session["id"]}.json"
+ music_session = JSON.parse(last_response.body)
+
+ # there should be one musician in the response
+ music_session["musicians"].length.should == 1
+ musician = music_session["musicians"][0]
+
+ # and that musician should have the same IP address
+ musician["ip_address"].should == "1.2.3.4"
+
+ # now delete that musician
+ delete "/api/sessions/#{music_session["id"]}/musician.json", '', "CONTENT_TYPE" => 'application/json'
+ last_response.status.should eql(204)
+
+ # re-fetch the session now that there is not a musician
+ get "/api/sessions/#{music_session["id"]}.json"
+ music_session = JSON.parse(last_response.body)
+
+ # there should be no musicians in the response
+ music_session["musicians"].length.should == 0
+ end
+
+
end
end