* VRFS-27; working on invitations controller
This commit is contained in:
parent
072be0062f
commit
5f9c7c24a2
|
|
@ -0,0 +1,20 @@
|
|||
class ApiController < ApplicationController
|
||||
|
||||
# define common error handlers
|
||||
rescue_from 'JamRuby::StateError' do |exception|
|
||||
@exception = exception
|
||||
render "errors/state_error.rabl", :status => 500
|
||||
end
|
||||
rescue_from 'JamRuby::JamArgrumentError' do |exception|
|
||||
@exception = exception
|
||||
render "errors/jam_argument_error.rabl", :status => 500
|
||||
end
|
||||
rescue_from 'JamRuby::PermissionError' do |exception|
|
||||
@exception = exception
|
||||
render "errors/permission_error.rabl", :status => 500
|
||||
end
|
||||
rescue_from 'ActiveRecord::RecordNotFound' do |exception|
|
||||
render :json => {:message => exception.message}, :status => 404
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
class ApiMusicSessionsController < ApiController
|
||||
|
||||
# have to be signed in currently to see this screen
|
||||
before_filter :signed_in_user
|
||||
|
||||
respond_to :json
|
||||
|
||||
def initialize
|
||||
@mq_router = MQRouter.new
|
||||
@message_factory = MessageFactory.new
|
||||
|
||||
end
|
||||
|
||||
def index
|
||||
@invitations = Invitation.paginate(page: params[:page])
|
||||
end
|
||||
|
||||
def create
|
||||
ConnectionManager.active_record_transaction do |connection_manager|
|
||||
|
||||
client_id = params[:client_id]
|
||||
|
||||
if client_id.nil?
|
||||
raise JamArgumentError, "client_id must be specified"
|
||||
end
|
||||
|
||||
@music_session = MusicSession.new()
|
||||
@music_session.creator = current_user
|
||||
@music_session.description = params[:description]
|
||||
genres = params[:genres]
|
||||
|
||||
|
||||
unless genres.nil?
|
||||
genres.each do |genre|
|
||||
loaded_genre = Genre.find_by_description!(genre)
|
||||
@music_session.genres << loaded_genre
|
||||
end
|
||||
end
|
||||
|
||||
saved = @music_session.save
|
||||
|
||||
if saved
|
||||
# auto-join this user into the newly created session
|
||||
connection_manager.join_music_session(current_user.id, client_id, @music_session.id)
|
||||
end
|
||||
end
|
||||
|
||||
if @music_session.errors.any?
|
||||
# we have to do this because api_session_detail_url will fail with a bad @music_session (or something like it)
|
||||
response.status = :unprocessable_entity
|
||||
respond_with @music_session
|
||||
else
|
||||
respond_with @music_session, responder: ApiResponder, :location => api_session_detail_url(@music_session)
|
||||
end
|
||||
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 participant_show
|
||||
@connection = Connection.find_by_client_id(params[:id])
|
||||
end
|
||||
|
||||
def participant_create
|
||||
@music_session = nil
|
||||
@connection = nil
|
||||
|
||||
ConnectionManager.active_record_transaction do |connection_manager|
|
||||
@music_session = MusicSession.find(params[:id])
|
||||
|
||||
if @music_session.nil?
|
||||
raise JamArgumentError, "no session found"
|
||||
end
|
||||
|
||||
client_id = params[:client_id]
|
||||
|
||||
connection_manager.join_music_session(current_user.id, client_id, @music_session.id)
|
||||
|
||||
@connection = Connection.find_by_client_id(client_id)
|
||||
end
|
||||
|
||||
# send out notification to queue to the rest of the session
|
||||
user_joined = @message_factory.user_joined_music_session(current_user.id, current_user.name)
|
||||
@mq_router.user_publish_to_session(@music_session, current_user, user_joined, sender = {:client_id => @connection.client_id})
|
||||
|
||||
respond_with @connection, responder: ApiResponder, :location => api_session_participant_detail_url(@connection.client_id)
|
||||
end
|
||||
|
||||
def participant_delete
|
||||
|
||||
ConnectionManager.active_record_transaction do |connection_manager|
|
||||
@connection = Connection.find_by_client_id(params[:id])
|
||||
|
||||
if @connection.nil?
|
||||
raise JamArgumentError, "no client found with specified client_id #{id}"
|
||||
end
|
||||
|
||||
if @connection.user.id != current_user.id
|
||||
raise PermissionError, "you do not own this connection"
|
||||
end
|
||||
|
||||
connection_manager.leave_music_session(current_user.id, @connection.client_id, @connection.music_session_id)
|
||||
end
|
||||
|
||||
respond_with @connection, responder: ApiResponder
|
||||
end
|
||||
end
|
||||
|
|
@ -1,23 +1,5 @@
|
|||
class ApiMusicSessionsController < ApplicationController
|
||||
|
||||
# TODO: roll these into a base ApiController class?s
|
||||
rescue_from 'JamRuby::StateError' do |exception|
|
||||
@exception = exception
|
||||
render "errors/state_error.rabl", :status => 500
|
||||
end
|
||||
rescue_from 'JamRuby::JamArgrumentError' do |exception|
|
||||
@exception = exception
|
||||
render "errors/jam_argument_error.rabl", :status => 500
|
||||
end
|
||||
rescue_from 'JamRuby::PermissionError' do |exception|
|
||||
@exception = exception
|
||||
render "errors/permission_error.rabl", :status => 500
|
||||
end
|
||||
|
||||
rescue_from 'ActiveRecord::RecordNotFound' do |exception|
|
||||
render :json => {:message => exception.message}, :status => 404
|
||||
end
|
||||
|
||||
class ApiMusicSessionsController < ApiController
|
||||
|
||||
# have to be signed in currently to see this screen
|
||||
before_filter :signed_in_user
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue