VRFS-1959 : jam_tracks#list_downloads API and model methods, as well as associated tests to verify.

This commit is contained in:
Steven Miers 2014-12-15 19:21:27 -06:00
parent 8ac9d19e73
commit 361d88a0fa
6 changed files with 62 additions and 14 deletions

View File

@ -30,6 +30,8 @@ ALTER TABLE jam_track_rights
ADD COLUMN md5 VARCHAR,
ADD COLUMN length INTEGER NOT NULL DEFAULT 0,
ADD COLUMN download_count INTEGER NOT NULL DEFAULT 0,
ADD COLUMN created_at timestamp without time zone NOT NULL,
ADD COLUMN updated_at timestamp without time zone NOT NULL,
ALTER COLUMN jam_track_id TYPE BIGINT USING 0,
ALTER COLUMN jam_track_id SET NOT NULL,
ADD CONSTRAINT jam_track_rights_user_id_fkey FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,

View File

@ -100,19 +100,35 @@ module JamRuby
since = 0 unless since || since == '' # guard against nil
downloads = []
user.jam_track_rights.limit(limit).collect do |jt_right|
downloads << jt_right
user.jam_track_rights
.limit(limit)
.where('jam_track_rights.id > ?', since)
.each do |jam_track_right|
downloads << {
:type => "jam_track",
:id => jam_track_right.id.to_s,
:jam_track_id => jam_track_right.jam_track_id,
:length => jam_track_right.length,
:md5 => jam_track_right.md5,
:url => jam_track_right.url,
:created_at => jam_track_right.created_at,
:next => jam_track_right.id
}
end
#JamTrack.joins(:recording).joins(:recording => :claimed_recordings)
next_date = downloads[-1][:next] if downloads.length > 0
next_date = since if next_date.nil? # echo back to the client the same value they passed in, if there are no results
{
'downloads' => downloads,
'next' => next_date.to_s
}
end
def right_for_user(user)
jam_track_rights.where("user_id=?", user).first
end
private
def sanitize_active_admin

View File

@ -17,6 +17,14 @@ describe JamTrackRight do
end
it "lists" do
jam_track_right = FactoryGirl.create(:jam_track_right)
jam_tracks = JamTrack.list_downloads(jam_track_right.user)
jam_tracks.should have_key('downloads')
jam_tracks.should have_key('next')
jam_tracks['downloads'].should have(1).items
end
describe "validations" do
it "one purchase per user/jam_track combo" do
user = FactoryGirl.create(:user)

View File

@ -1,15 +1,15 @@
class ApiJamtracksController < ApiController
class ApiJamTracksController < ApiController
# have to be signed in currently to see this screen
before_filter :api_signed_in_user
respond_to :json
def index
data = JamTrack.index current_user, params
@jamtracks = data[0]
@next = data[1]
def list_downloads
begin
render :json => JamTrack.list_downloads(current_user, params[:limit], params[:since]), :status => 200
rescue
render :json => { :message => "could not produce list of files" }, :status => 403
end
end
end

View File

@ -190,8 +190,8 @@ SampleApp::Application.routes.draw do
match '/music_notations/:id' => 'api_music_notations#download', :via => :get, :as => :download_music_notation
# Jamtracks
match '/jamtracks' => 'api_jamtracks#index', :via => :get
match '/jamtracks/downloads' => 'api_jam_tracks#list_downloads', :via => :get, :as => 'api_jam_tracks_list_downloads'
# Shopping carts
match '/shopping_carts/add_jamtrack' => 'api_shopping_carts#add_jamtrack', :via => :post
match '/shopping_carts' => 'api_shopping_carts#index', :via => :get

View File

@ -0,0 +1,22 @@
require 'spec_helper'
describe ApiJamTracksController do
before(:each) do
@user = FactoryGirl.create(:user)
@jam_track = FactoryGirl.create(:jam_track)
controller.current_user = @user
end
describe "download" do
let(:mix) { FactoryGirl.create(:mix) }
it "list download" do
right = JamTrackRight.create(:user=>@user, :jam_track=>@jam_track)
get :list_downloads
response.should be_success
json = JSON.parse(response.body)
json['downloads'].should have(1).items
end
end
end