jam-cloud/app/controllers/api_music_sessions_controll...

165 lines
4.9 KiB
Ruby

class ApiMusicSessionsController < ApiController
# have to be signed in currently to see this screen
before_filter :api_signed_in_user
before_filter :lookup_session, only: [:show, :update, :delete]
respond_to :json
def index
# params[:participants] is either nil, meaning "everything", or it's an array of musician ids
# params[:genres] is either nil, meaning "everything", or it's an array of genre ids
# params[:friends_only] does the obvious.
# params[:my_bands_only] also does the obvious.
# Importantly, friends and my_bands are ORed not ANDed. So, if you specify both as true, you'll get more results, not fewer.
@music_sessions = MusicSession.index(current_user, params[:participants], params[:genres], params[:friends_only], params[:my_bands_only], params[:keyword])
end
def create
client_id = params[:client_id]
if client_id.nil?
raise JamArgumentError, "client_id must be specified"
end
band = Band.find(params[:band]) unless params[:band].nil?
@music_session = MusicSessionManager.new.create(
current_user,
client_id,
params[:description],
params[:musician_access],
params[:approval_required],
params[:fan_chat],
params[:fan_access],
band,
params[:genres],
params[:tracks],
params[:legal_terms])
if @music_session.errors.any?
# we have to do this because api_session_detail_url will fail with a bad @music_session
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
unless @music_session.can_see? current_user
raise ActiveRecord::RecordNotFound
end
end
def update
@music_session = MusicSessionManager.new.update(
@music_session,
params[:music_session][:description],
params[:music_session][:genres],
params[:music_session][:musician_access],
params[:music_session][:approval_required],
params[:music_session][:fan_chat],
params[:music_session][:fan_access])
#FIXME: What to do if validations fail?
respond_with @music_session, responder: ApiResponder, :location => api_session_detail_url(@music_session)
end
def delete
unless @music_session.can_delete? current_user
raise ActiveRecord::RecordNotound
end
@music_session.destroy # required to make 'tire' integration work
respond_with @music_session, responder: ApiResponder
end
def participant_show
@connection = Connection.find_by_client_id(params[:id])
end
def participant_create
@connection = MusicSessionManager.new.participant_create(
current_user,
params[:id],
params[:client_id],
params[:as_musician],
params[:tracks])
if @connection.errors.any?
response.status = :unprocessable_entity
respond_with @connection
else
respond_with @connection, responder: ApiResponder, :location => api_session_participant_detail_url(@connection.client_id)
end
end
def participant_delete
client_id = params[:id]
@connection = Connection.find_by_client_id!(client_id)
music_session = MusicSession.find(@connection.music_session_id)
MusicSessionManager.new.participant_delete(current_user, @connection, music_session)
respond_with @connection, responder: ApiResponder
end
def lookup_session
@music_session = MusicSession.find(params[:id])
end
def track_index
@tracks = Track.index(current_user, params[:id])
end
def track_show
begin
@track = Track.joins(:connection)
.where(:connections => {:user_id => "#{current_user.id}"})
.find(params[:track_id])
rescue ActiveRecord::RecordNotFound
render :json => { :message => ValidationMessages::TRACK_NOT_FOUND }, :status => 404
end
end
def track_create
@track = Track.save(nil,
params[:connection_id],
params[:instrument_id],
params[:sound])
respond_with @track, responder: ApiResponder, :status => 201, :location => api_session_track_detail_url(@track.connection.music_session, @track)
end
def track_update
begin
@track = Track.save(params[:track_id],
nil,
params[:instrument_id],
params[:sound])
respond_with @track, responder: ApiResponder, :status => 200
rescue ActiveRecord::RecordNotFound
render :json => { :message => ValidationMessages::TRACK_NOT_FOUND }, :status => 404
end
end
def track_destroy
begin
@track = Track.find(params[:track_id])
unless @track.nil?
@track.delete
respond_with responder: ApiResponder, :status => 204
end
rescue ActiveRecord::RecordNotFound
render :json => { :message => ValidationMessages::TRACK_NOT_FOUND }, :status => 404
end
end
end