first take on api_music_sessions_controller#index replacement, nindex.

This commit is contained in:
Scott Comer 2014-03-05 20:30:40 -06:00
parent 302341360e
commit 65b0c7115a
5 changed files with 212 additions and 16 deletions

View File

@ -40,6 +40,8 @@ module JamRuby
validate :creator_is_musician
validate :no_new_playback_while_playing
#default_scope :select => "*, 0 as score"
before_create :create_uuid
def create_uuid
#self.id = SecureRandom.uuid
@ -90,15 +92,15 @@ module JamRuby
as_musician = options[:as_musician].nil? ? true : options[:as_musician]
query = MusicSession
.joins(
.joins(
%Q{
INNER JOIN
connections
ON
music_sessions.id = connections.music_session_id
}
)
.joins(
)
.joins(
%Q{
LEFT OUTER JOIN
friendships
@ -107,8 +109,8 @@ module JamRuby
AND
friendships.friend_id = '#{current_user.id}'
}
)
.joins(
)
.joins(
%Q{
LEFT OUTER JOIN
invitations
@ -117,19 +119,19 @@ module JamRuby
AND
invitations.receiver_id = '#{current_user.id}'
}
)
.group(
)
.group(
%Q{
music_sessions.id
}
)
.order(
)
.order(
%Q{
SUM(CASE WHEN invitations.id IS NULL THEN 0 ELSE 1 END) DESC,
SUM(CASE WHEN friendships.user_id IS NULL THEN 0 ELSE 1 END) DESC,
music_sessions.created_at DESC
}
)
)
if as_musician
query = query.where(
@ -153,7 +155,7 @@ module JamRuby
if my_bands_only
query = query.joins(
%Q{
%Q{
LEFT OUTER JOIN
bands_musicians
ON
@ -164,11 +166,134 @@ module JamRuby
if my_bands_only || friends_only
query = query.where(
%Q{
#{friends_only ? "friendships.user_id IS NOT NULL" : "false"}
%Q{
#{friends_only ? "friendships.user_id IS NOT NULL" : "false"}
OR
#{my_bands_only ? "bands_musicians.band_id = music_sessions.band_id" : "false"}
}
)
end
return query
end
# This is a little confusing. You can specify *BOTH* friends_only and my_bands_only to be true
# If so, then it's an OR condition. If both are false, you can get sessions with anyone.
# note, this is mostly the same as above but includes paging through the result and and scores.
# thus it needs the client_id...
def self.nindex(current_user, options = {})
client_id = options[:client_id]
participants = options[:participants]
genres = options[:genres]
keyword = options[:keyword]
friends_only = options[:friends_only].nil? ? false : options[:friends_only]
my_bands_only = options[:my_bands_only].nil? ? false : options[:my_bands_only]
as_musician = options[:as_musician].nil? ? true : options[:as_musician]
offset = options[:offset]
limit = options[:limit]
connection = Connection.where(client_id: client_id).first!
locidispid = connection.locidispid
query = MusicSession
.select("music_sessions.*, max(coalesce(scores.score, 99)) as max_score")
.joins(
%Q{
INNER JOIN
connections
ON
music_sessions.id = connections.music_session_id
}
)
.joins(
%Q{
LEFT OUTER JOIN
scores
ON
scores.alocidispid = connections.locidispid
AND
scores.blocidispid = #{locidispid}
}
)
.joins(
%Q{
LEFT OUTER JOIN
friendships
ON
connections.user_id = friendships.user_id
AND
friendships.friend_id = '#{current_user.id}'
}
)
.joins(
%Q{
LEFT OUTER JOIN
invitations
ON
invitations.music_session_id = music_sessions.id
AND
invitations.receiver_id = '#{current_user.id}'
}
)
.group(
%Q{
music_sessions.id
}
)
.order(
%Q{
SUM(CASE WHEN invitations.id IS NULL THEN 0 ELSE 1 END) DESC,
SUM(CASE WHEN friendships.user_id IS NULL THEN 0 ELSE 1 END) DESC,
music_sessions.created_at DESC
}
)
if (offset)
query = query.offset(offset)
end
if (limit)
query = query.limit(limit)
end
if as_musician
query = query.where(
%Q{
musician_access = true
OR
invitations.id IS NOT NULL
}
)
else
# if you are trying to join the session as a fan/listener,
# we have to have a mount, fan_access has to be true, and we have to allow for the reload of icecast to have taken effect
query = query.joins('INNER JOIN icecast_mounts ON icecast_mounts.music_session_id = music_sessions.id INNER JOIN icecast_servers ON icecast_mounts.icecast_server_id = icecast_servers.id')
query = query.where(:fan_access => true)
query = query.where("(music_sessions.created_at < icecast_servers.config_updated_at)")
end
query = query.where("music_sessions.description like '%#{keyword}%'") unless keyword.nil?
query = query.where("connections.user_id" => participants.split(',')) unless participants.nil?
query = query.joins(:genres).where("genres.id" => genres.split(',')) unless genres.nil?
if my_bands_only
query = query.joins(
%Q{
LEFT OUTER JOIN
bands_musicians
ON
bands_musicians.user_id = '#{current_user.id}'
}
)
end
if my_bands_only || friends_only
query = query.where(
%Q{
#{friends_only ? "friendships.user_id IS NOT NULL" : "false"}
OR
#{my_bands_only ? "bands_musicians.band_id = music_sessions.band_id" : "false"}
}
)
end

View File

@ -302,6 +302,37 @@ describe MusicSession do
end
end
describe "nindex" do
it "nindex orders two sessions by created_at starting with most recent" do
creator = FactoryGirl.create(:user)
creator2 = FactoryGirl.create(:user)
earlier_session = FactoryGirl.create(:music_session, :creator => creator, :description => "Earlier Session")
c1 = FactoryGirl.create(:connection, user: creator, music_session: earlier_session, addr: 0x01020304, locidispid: 1)
later_session = FactoryGirl.create(:music_session, :creator => creator2, :description => "Later Session")
c2 = FactoryGirl.create(:connection, user: creator2, music_session: later_session, addr: 0x21020304, locidispid: 2)
user = FactoryGirl.create(:user)
c3 = FactoryGirl.create(:connection, user: user, locidispid: 3)
Score.createx(c1.locidispid, c1.client_id, c1.addr, c3.locidispid, c3.client_id, c3.addr, 20, nil);
Score.createx(c2.locidispid, c2.client_id, c2.addr, c3.locidispid, c3.client_id, c3.addr, 30, nil);
# scores!
ActiveRecord::Base.logger = Logger.new(STDOUT)
music_sessions = MusicSession.nindex(user, client_id: c3.client_id).take(100)
#music_sessions = MusicSession.index(user).take(100)
ActiveRecord::Base.logger = nil
puts(music_sessions.inspect)
music_sessions.length.should == 2
music_sessions[0].id.should == later_session.id
music_sessions[1].id.should == earlier_session.id
end
end
it "updates the fields of a music session properly" do
genre1 = FactoryGirl.create(:genre)

View File

@ -10,11 +10,18 @@ class ApiMusicSessionsController < ApiController
respond_to :json
def index
# returns a list of sessions which are hopefully interesting to you as three buckets (concatenated). The 1st bucket
# is those session to which you've been invited. The 2nd bucket is those sessions which are about a band that you
# are in or in which a friend is participating. The 3rd bucket is everything else (sessions - invites - friends - bands).
# 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.
# params[:friends_only] does the obvious. you get invited and friends sessions only.
# params[:my_bands_only] also does the obvious. you get invited and bands sessions only.
# params[:keyword] matches only sessions with that text in the description
# params[:as_musician] only returns sessions you can join as a musician (if true) or listen to a fan (if false)
# Importantly, friends_only and my_bands_only are ORed not ANDed. So, if you specify both as true, you'll get more
# results than if only one or the other is true, not fewer. if either is true you won't see the "everything else"
# sessions.
@music_sessions = MusicSession.index(current_user,
participants: params[:participants],
genres: params[:genres],
@ -24,6 +31,35 @@ class ApiMusicSessionsController < ApiController
as_musician: params[:as_musician])
end
def nindex
# returns a list of sessions which are hopefully interesting to you as three buckets (concatenated). The 1st bucket
# is those session to which you've been invited. The 2nd bucket is those sessions which are about a band that you
# are in or in which a friend is participating. The 3rd bucket is everything else (sessions - invites - friends - bands).
# pretty much the same as #index above, except scores are also returned.
# params[:client_id] is the client_id of the client making the call. needed to resovle scoring.
# 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. you get invited and friends sessions only.
# params[:my_bands_only] also does the obvious. you get invited and bands sessions only.
# params[:keyword] matches only sessions with that text in the description
# params[:as_musician] only returns sessions you can join as a musician (if true) or listen to a fan (if false)
# params[:offset] also does the obvious, starts at offset in the results (e.g. 0)
# params[:limit] also does the obvious, limits number of rows returned (e.g. 20)
# Importantly, friends_only and my_bands_only are ORed not ANDed. So, if you specify both as true, you'll get more
# results than if only one or the other is true, not fewer. if either is true you won't see the "everything else"
# sessions.
@music_sessions = MusicSession.nindex(current_user,
client_id: params[:client_id],
participants: params[:participants],
genres: params[:genres],
friends_only: params[:friends_only],
my_bands_only: params[:my_bands_only],
keyword: params[:keyword],
as_musician: params[:as_musician],
offset: params[:offset],
limit: params[:limit])
end
def create
client_id = params[:client_id]

View File

@ -0,0 +1,3 @@
object @music_sessions
extends "api_music_sessions/show"

View File

@ -109,6 +109,7 @@ SampleApp::Application.routes.draw do
match '/sessions/:id' => 'api_music_sessions#show', :via => :get, :as => 'api_session_detail'
match '/sessions/:id' => 'api_music_sessions#update', :via => :put
match '/sessions' => 'api_music_sessions#index', :via => :get
match '/sessions/nindex/:client_id' => 'api_music_sessions#nindex', :via => :get
match '/sessions' => 'api_music_sessions#create', :via => :post
match '/sessions/:id/perf' => 'api_music_sessions#perf_upload', :via => :put
match '/sessions/:id/comments' => 'api_music_sessions#add_comment', :via => :post