VRFS-81 - recording/favorite development

This commit is contained in:
Brian Smith 2012-11-21 14:49:00 -05:00
parent 98c6ad0bb0
commit d3ddf3b114
18 changed files with 339 additions and 112 deletions

View File

@ -1,7 +1,7 @@
class ApiBandsController < ApplicationController
before_filter :api_signed_in_user, only: [:index, :show, :create, :update,
:following_create, :following_destroy]
before_filter :api_signed_in_user, :only => [:index, :show, :create, :update,
:following_create, :following_destroy]
respond_to :json
@ -14,7 +14,14 @@ class ApiBandsController < ApplicationController
end
def create
@band = Band.save(params)
@band = Band.save(params[:id],
params[:name],
params[:website],
params[:biography],
params[:city],
params[:state],
params[:country],
params[:genres])
respond_with @band, responder: ApiResponder, :location => api_band_detail_url(@band)
=begin
@ -31,7 +38,14 @@ class ApiBandsController < ApplicationController
end
def update
@band = Band.save(params)
@band = Band.save(params[:id],
params[:name],
params[:website],
params[:biography],
params[:city],
params[:state],
params[:country],
params[:genres])
# check for errors
if @band.errors.nil? || @band.errors.size == 0
@ -44,25 +58,25 @@ class ApiBandsController < ApplicationController
end
end
# FOLLOWERS
###################### FOLLOWERS ########################
def follower_index
# NOTE: follower_index.rabl template references the followers property
@band = Band.find(params[:id])
end
# FOLLOWINGS
def following_create
@follower = BandFollower.new()
@follower.user_id = params[:follower_id]
@follower.follower_id = params[:id]
@follower.save
@user = User.find(params[:follower_id])
respond_with @user, responder: ApiResponder, :location => api_following_index_url(@user)
###################### RECORDINGS #######################
def recording_index
@recordings = Recording.paginate(page: params[:page])
end
def following_destroy
JamRuby::BandFollower.delete_all "(user_id = '#{params[:user_id]}' AND band_id = '#{params[:id]}')"
respond_with responder: ApiResponder
def recording_create
Recording.save(params[:recording_id],
params[:public],
params[:description],
params[:id],
true)
end
def recording_destroy
end
end

View File

@ -1,9 +1,6 @@
class ApiUsersController < ApiController
before_filter :api_signed_in_user, only: [:index, :show, :update, :delete,
:friend_request_index, :friend_request_show,
:friend_request_create, :friend_request_update,
:friend_index, :friend_destroy]
before_filter :api_signed_in_user, :except => [:create, :signup_confirm, :auth_session_create]
respond_to :json
@ -56,7 +53,21 @@ class ApiUsersController < ApiController
end
def update
@user = User.save(params)
auth_user(params[:id])
@user = User.save(params[:id],
params[:first_name],
params[:last_name],
params[:email],
params[:password],
params[:password_confirmation],
params[:musician],
params[:gender],
params[:birth_date],
params[:internet_service_provider],
params[:city],
params[:state],
params[:country],
params[:instruments])
# check for errors
if @user.errors.nil? || @user.errors.size == 0
@ -67,7 +78,6 @@ class ApiUsersController < ApiController
response.status = :unprocessable_entity
respond_with @user
end
# FRIENDS
end
def delete
@ -85,33 +95,80 @@ class ApiUsersController < ApiController
###################### FOLLOWINGS #######################
def following_index
@user = User.find(params[:id])
end
# TODO: get band followings and merge (@user.band_followings)
def band_following_index
@user = User.find(params[:id])
end
def following_create
id = params[:id]
auth_user(id)
@user = User.find(id)
if !params[:user_id].nil?
@follower = UserFollower.new()
@follower.user_id = params[:user_id]
@follower.follower_id = params[:id]
User.create_user_following(params[:user_id], id)
respond_with @user, responder: ApiResponder, :location => api_user_following_index_url(@user)
elsif !params[:band_id].nil?
@follower = BandFollower.new()
@follower.band_id = params[:band_id]
@follower.follower_id = params[:id]
User.create_band_following(params[:band_id], id)
respond_with @user, responder: ApiResponder, :location => api_band_following_index_url(@user)
end
@follower.save
@user = User.find(params[:id])
respond_with @user, responder: ApiResponder, :location => api_following_index_url(@user)
end
def following_destroy
JamRuby::UserFollower.delete_all "(user_id = '#{params[:user_id]}' AND follower_id = '#{params[:id]}')"
#JamRuby::BandFollower.delete_all "(band_id = '#{params[:band_id]}' AND follower_id = '#{params[:id]}')"
auth_user(params[:id])
User.delete_user_following(params[:user_id], params[:id])
respond_with responder: ApiResponder
end
###################### RECORDINGS #######################
def recording_index
@recordings = Recording.where("user_id=#{params[:id]}").paginate(page: params[:page])
end
def recording_create
auth_user(params[:id])
@recording = Recording.save(params[:recording_id],
params[:public],
params[:description],
params[:id],
false)
# check for errors
if @recording.errors.nil? || @recording.errors.size == 0
respond_with @recording, responder: ApiResponder, :status => 201, :location => api_recording_detail_url(@recording)
else
raise ActiveRecord::Rollback
response.status = :unprocessable_entity
respond_with @recording
end
end
def recording_update
auth_user(params[:id])
@recording = Recording.save(params[:recording_id],
params[:public],
params[:description],
params[:id],
false)
# check for errors
if @recording.errors.nil? || @recording.errors.size == 0
respond_with @recording, responder: ApiResponder, :status => 200
else
raise ActiveRecord::Rollback
response.status = :unprocessable_entity
respond_with @recording
end
end
def recording_destroy
auth_user(params[:id])
Recording.delete(params[:recording_id], params[:id], false)
end
###################### FAVORITES ########################
def favorite_index
@user = User.find(params[:id])
@ -120,31 +177,34 @@ class ApiUsersController < ApiController
end
def favorite_create
@follower = UserFavorite.new()
@follower.user_id = params[:user_id]
@follower.follower_id = params[:id]
auth_user(params[:id])
@favorite = UserFavorite.new()
User.create_favorite(params[:id], params[:recording_id])
@follower.save
@user = User.find(params[:id])
respond_with @user, responder: ApiResponder, :location => api_favorite_index_url(@user)
end
def favorite_destroy
JamRuby::UserFavorite.delete_all "(user_id = '#{params[:id]}' AND recording_id = '#{params[:recording_id]}')"
auth_user(params[:id])
User.delete_favorite(params[:id], params[:recording_id])
respond_with responder: ApiResponder
end
###################### FRIENDS ##########################
def friend_request_index
auth_user(params[:id])
# get all outgoing and incoming friend requests
@friend_requests = FriendRequest.where("(friend_id='#{params[:id]}' OR user_id='#{params[:id]}') AND accepted is null")
end
def friend_request_show
auth_user(params[:id])
@friend_request = FriendRequest.find(params[:id])
end
def friend_request_create
auth_user(params[:user_id])
@friend_request = FriendRequest.new()
@friend_request.user_id = params[:user_id]
@friend_request.friend_id = params[:friend_id]
@ -181,6 +241,7 @@ class ApiUsersController < ApiController
end
def friend_destroy
auth_user(params[:id])
# clean up both records representing this "friendship"
JamRuby::Friendship.delete_all "(user_id = '#{params[:id]}' AND friend_id = '#{params[:friend_id]}') OR (user_id = '#{params[:friend_id]}' AND friend_id = '#{params[:id]}')"
respond_with responder: ApiResponder
@ -202,4 +263,11 @@ class ApiUsersController < ApiController
sign_out
render :json => { :success => true }, :status => 200
end
protected
def auth_user(id)
if current_user.id != id
raise PermissionError, "You do not have permissions to perform this action."
end
end
end

View File

View File

View File

@ -0,0 +1,27 @@
object @user.band_followings
attributes :band_id
node :name do |following|
following.band.name
end
node :city do |following|
following.band.city
end
node :state do |following|
following.band.state
end
node :country do |following|
following.band.country
end
node :photo_url do |following|
following.band.photo_url
end
node :logo_url do |following|
following.band.logo_url
end

View File

@ -0,0 +1,11 @@
object @user.favorites
attributes :recording_id
node :description do |favorite|
favorite.recording.description
end
node :public do |favorite|
favorite.recording.public
end

View File

@ -0,0 +1,3 @@
object @user
extends "api_users/show"

View File

View File

@ -1,6 +1,6 @@
object @user
attributes :id, :first_name, :last_name, :city, :state, :country, :online, :photo_url, :musician, :gender, :birth_date, :internet_service_provider, :friend_count, :follower_count, :following_count
attributes :id, :first_name, :last_name, :city, :state, :country, :online, :photo_url, :musician, :gender, :birth_date, :internet_service_provider, :friend_count, :follower_count, :following_count, :favorite_count
unless @user.friends.nil? || @user.friends.size == 0
child :friends => :friends do

View File

@ -47,9 +47,7 @@ SampleApp::Application.routes.draw do
match '/genres' => 'api_genres#index', :via => :get
# recordings
match '/recordings' => 'api_recordings#create', :via => :post
match '/recordings/:id' => 'api_recordings#show', :via => :get, :as => 'api_recording_detail'
match '/recordings/:id' => 'api_recordings#update', :via => :post
# users
match '/users' => 'api_users#index', :via => :get
@ -77,13 +75,17 @@ SampleApp::Application.routes.draw do
match '/users/:id/followers' => 'api_users#follower_index', :via => :get
# user followings
match '/users/:id/followings' => 'api_users#following_index', :via => :get, :as => 'api_following_index'
match '/users/:id/followings' => 'api_users#following_index', :via => :get, :as => 'api_user_following_index'
match '/users/:id/band_followings' => 'api_users#band_following_index', :via => :get, :as => 'api_band_following_index'
match '/users/:id/followings' => 'api_users#following_create', :via => :post
match '/users/:id/followings/:user_id' => 'api_users#following_destroy', :via => :delete
# user recordings
match '/users/:id/recordings' => 'api_users#recording_index', :via => :get
match '/users/:id/recordings/:recording_id' => 'api_users#recording_delete', :via => :delete
match '/users/:id/recordings/:recording_id' => 'api_users#recording_show', :via => :get, :as => 'api_recording_detail'
match '/users/:id/recordings' => 'api_users#recording_create', :via => :post
match '/users/:id/recordings/:recording_id' => 'api_users#recording_update', :via => :post
match '/users/:id/recordings/:recording_id' => 'api_users#recording_destroy', :via => :delete
# favorites
match '/users/:id/favorites' => 'api_users#favorite_index', :via => :get, :as => 'api_favorite_index'
@ -101,7 +103,10 @@ SampleApp::Application.routes.draw do
# band recordings
match '/bands/:id/recordings' => 'api_bands#recording_index', :via => :get
match '/bands/:id/recordings/:recording_id' => 'api_bands#recording_delete', :via => :delete
match '/bands/:id/recordings/:recording_id' => 'api_bands#recording_show', :via => :get
match '/bands/:id/recordings' => 'api_bands#recording_create', :via => :post
match '/bands/:id/recordings/:recording_id' => 'api_bands#recording_update', :via => :post
match '/bands/:id/recordings/:recording_id' => 'api_bands#recording_destroy', :via => :delete
# invitations
match '/invitations/:id' => 'api_invitations#show', :via => :get, :as => 'api_invitation_detail'
@ -109,7 +114,7 @@ SampleApp::Application.routes.draw do
match '/invitations' => 'api_invitations#index', :via => :get
match '/invitations' => 'api_invitations#create', :via => :post
# invitations
# instruments
match '/instruments/:id' => 'api_instruments#show', :via => :get, :as => 'api_instrument_detail'
match '/instruments' => 'api_instruments#index', :via => :get

View File

@ -1,19 +1,22 @@
FactoryGirl.define do
factory :user, :class => "JamRuby::User" do
factory :user, :class => JamRuby::User do
sequence(:email) { |n| "person_#{n}@example.com"}
sequence(:first_name) { |n| "Person" }
sequence(:last_name) { |n| "#{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
email_confirmed true
musician true
city "Apex"
state "NC"
country "USA"
factory :admin do
admin true
end
end
factory :fan, :class => "JamRuby::User" do
factory :fan, :class => JamRuby::User do
sequence(:first_name) { |n| "Person" }
sequence(:last_name) { |n| "#{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
@ -21,9 +24,12 @@ FactoryGirl.define do
password_confirmation "foobar"
email_confirmed true
musician false
city "Apex"
state "NC"
country "USA"
end
factory :music_session, :class => "JamRuby::MusicSession" do
factory :music_session, :class => JamRuby::MusicSession do
sequence(:description) { |n| "Music Session #{n}" }
fan_chat true
fan_access true
@ -44,6 +50,10 @@ FactoryGirl.define do
end
factory :band, :class => JamRuby::Band do
sequence(:name) { |n| "Band" }
biography "Established 1978"
city "Apex"
state "NC"
country "USA"
end
end

View File

View File

@ -1,6 +1,6 @@
require 'spec_helper'
describe "Search API ", :type => :api do
describe "Search API", :type => :api do
include Rack::Test::Methods
@ -30,8 +30,8 @@ describe "Search API ", :type => :api do
User.create_search_index
@musician = FactoryGirl.create(:user, first_name: "Peach", last_name: "Nothing", email: "user@example.com", musician: true)
@fan = FactoryGirl.create(:user, first_name: "Peach Peach", last_name: "Grovery", email: "fan@example.com", musician: false)
@band = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
@band2 = Band.save(name: "Peach", website: "www.bands2.com", biography: "zomg we rock")
@band = Band.save(nil, "Peach pit", "www.bands.com", "zomg we rock", "Apex", "NC", "USA", nil)
@band2 = Band.save(nil, "Peach", "www.bands2.com", "zomg we rock", "Apex", "NC", "USA", nil)
User.search_index.refresh
Band.search_index.refresh

View File

@ -6,7 +6,7 @@ describe "User API", :type => :api do
subject { page }
describe "profile page" do
describe "profile" do
let(:user) { FactoryGirl.create(:user) }
let(:fan) { FactoryGirl.create(:fan) }
@ -14,74 +14,163 @@ describe "User API", :type => :api do
UserMailer.deliveries.clear
end
def login(login_user)
def login(email, password, http_code, success)
# login as fan
post '/api/auth_session.json', { :email => login_user.email, :password => login_user.password }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
JSON.parse(last_response.body).should == { "success" => true }
post '/api/auth_session.json', { :email => email, :password => password }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == http_code
JSON.parse(last_response.body).should == { "success" => success }
end
context "unauthenticated user" do
it "successful login" do
# can't access most apis; not logged in yet!'
get '/api/users.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 403
it "should allow successful login" do
# can't access most apis; not logged in yet!'
get '/api/users.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 403
# login
login(user)
# login
login(user.email, user.password, 200, true)
# can now login
get '/api/users.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
# can now login
get '/api/users.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
# log back out
delete '/api/auth_session.json', "CONTENT_TYPE" => 'application/json'
# log back out
delete '/api/auth_session.json', "CONTENT_TYPE" => 'application/json'
# can't access most apis; not logged in yet!'
get '/api/users.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 403
# can't access most apis; not logged in yet!'
get '/api/users.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 403
end
it "should deny bad login" do
# login
login("nothing", "mur", 404, false)
# can't access most apis; not logged in yet!'
get '/api/users.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 403
end
end
it "bad login" do
# login
post '/api/auth_session.json', { :email => "nothing", :password => "mur" }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 404
JSON.parse(last_response.body).should == { "success" => false }
context "authenticated user" do
# log in a valid user
=begin
before do
puts "logging in"
post '/sessions', "session[email]" => fan.email, "session[password]" => fan.password
rack_mock_session.cookie_jar["remember_token"].should == fan.remember_token
end
=end
# can't access most apis; not logged in yet!'
get '/api/users.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 403
it "should allow user updates" do
# login as fan
login(fan.email, fan.password, 200, true)
# update the user's first name and musician flag'
post "/api/users/#{fan.id}.json", { :first_name => "Brian", :musician => true }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
# login as fan (credentials are not saved between API calls)
login(fan.email, fan.password, 200, true)
# get the user's details
get "/api/users/#{fan.id}.json", "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
updated_user = JSON.parse(last_response.body)
updated_user["musician"].should == true
updated_user["first_name"].should == "Brian"
end
it "should allow user to follow user" do
# create user following
login(user.email, user.password, 200, true)
post "/api/users/#{user.id}/followings.json", { :user_id => fan.id }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 201
# get following
login(user.email, user.password, 200, true)
get "/api/users/#{user.id}/followings.json"
last_response.status.should == 200
followings = JSON.parse(last_response.body)
followings.size.should == 1
followings[0]["user_id"].should == fan.id
# get followers for for fan
login(fan.email, fan.password, 200, true)
get "/api/users/#{fan.id}/followers.json"
last_response.status.should == 200
followers = JSON.parse(last_response.body)
followers.size.should == 1
followers[0]["user_id"].should == user.id
end
it "should allow user to follow band" do
# create band following
login(user.email, user.password, 200, true)
band = FactoryGirl.create(:band)
post "/api/users/#{user.id}/followings.json", { :band_id => band.id }.to_json, "CONTENT_TYPE" => 'application/json'
# get following
login(user.email, user.password, 200, true)
get "/api/users/#{user.id}/band_followings.json"
last_response.status.should == 200
followings = JSON.parse(last_response.body)
followings.size.should == 1
followings[0]["band_id"].should == band.id
# get followers for band
login(user.email, user.password, 200, true)
get "/api/bands/#{band.id}/followers.json"
last_response.status.should == 200
followers = JSON.parse(last_response.body)
followers.size.should == 1
followers[0]["user_id"].should == user.id
# delete followings
end
it "should not allow user to create following for another user" do
login(user.email, user.password, 200, true)
post "/api/users/2/followings.json", { :user_id => fan.id }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 500
end
it "should not allow user to delete following of another user" do
end
it "should allow musician to create recordings" do
login(user.email, user.password, 200, true)
end
it "should not allow fan to create recordings" do
end
it "should allow user to get recordings" do
end
it "should allow user to create favorites" do
end
it "should allow user to delete favorites" do
end
it "should allow musician to create band" do
end
it "should not allow fan to create band" do
end
it "should allow user to send friend request" do
end
it "should allow user to accept friend request" do
end
it "should allow user to deny friend request" do
end
end
it "allows user to update attributes" do
# login as fan
login(fan)
# update the user's first name and musician flag'
post '/api/users/' + fan.id + '.json', { :first_name => "Brian", :musician => true }.to_json, "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
# login as fan
login(fan)
# get the user's details
get '/api/users/' + fan.id + '.json', "CONTENT_TYPE" => 'application/json'
last_response.status.should == 200
updated_user = JSON.parse(last_response.body)
updated_user["musician"].should == true
updated_user["first_name"].should == "Brian"
end
it "allows user to add followings" do
end
it "can get followers" do
end
it "allows user to delete followings" do
end
end
end