From f3bf0f061cb7d2d2ba838e5ae9d59a6ae73a90a4 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Mon, 3 Dec 2012 22:40:40 -0500 Subject: [PATCH] VRFS-102 - added routes for likes --- app/controllers/api_users_controller.rb | 10 +++++++++- app/views/api_bands/index.rabl | 2 +- app/views/api_bands/recording_index.rabl | 1 + app/views/api_bands/recording_show.rabl | 1 + app/views/api_bands/show.rabl | 3 ++- app/views/api_users/recording_index.rabl | 3 ++- app/views/api_users/recording_show.rabl | 7 +++++-- app/views/api_users/show.rabl | 3 ++- config/routes.rb | 8 +++++++- spec/requests/users_api_spec.rb | 9 ++++++++- 10 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/controllers/api_users_controller.rb b/app/controllers/api_users_controller.rb index a69e9cbde..43ea13c36 100644 --- a/app/controllers/api_users_controller.rb +++ b/app/controllers/api_users_controller.rb @@ -100,6 +100,7 @@ class ApiUsersController < ApiController id = params[:id] auth_user(id) @user = User.find(id) + if !params[:user_id].nil? User.create_user_following(params[:user_id], id) respond_with @user, responder: ApiResponder, :location => api_user_following_index_url(@user) @@ -112,7 +113,14 @@ class ApiUsersController < ApiController def following_destroy auth_user(params[:id]) - User.delete_user_following(params[:user_id], params[:id]) + + if !params[:user_id].nil? + User.delete_following(params[:user_id], nil, params[:id]) + + elsif !params[:band_id].nil? + User.delete_following(nil, params[:band_id], params[:id]) + end + respond_with responder: ApiResponder, :status => 204 end diff --git a/app/views/api_bands/index.rabl b/app/views/api_bands/index.rabl index c1e57847b..74531e3ae 100644 --- a/app/views/api_bands/index.rabl +++ b/app/views/api_bands/index.rabl @@ -1,4 +1,4 @@ collection @bands # do not retrieve all child collections when showing a list of bands -attributes :id, :name, :city, :state, :country, :photo_url, :logo_url, \ No newline at end of file +attributes :id, :name, :city, :state, :country, :photo_url, :logo_url \ No newline at end of file diff --git a/app/views/api_bands/recording_index.rabl b/app/views/api_bands/recording_index.rabl index 434361bd1..153606970 100644 --- a/app/views/api_bands/recording_index.rabl +++ b/app/views/api_bands/recording_index.rabl @@ -6,6 +6,7 @@ node :genres do |recording| unless recording.genres.nil? || recording.genres.size == 0 child :genres => :genres do attributes :id, :description + #partial('api_genres/index', :object => recording.genres) end end end \ No newline at end of file diff --git a/app/views/api_bands/recording_show.rabl b/app/views/api_bands/recording_show.rabl index 95ca98734..4936e763d 100644 --- a/app/views/api_bands/recording_show.rabl +++ b/app/views/api_bands/recording_show.rabl @@ -5,5 +5,6 @@ attributes :id, :description, :public unless @recording.genres.nil? || @recording.genres.size == 0 child :genres => :genres do attributes :id, :description + #partial('api_genres/index', :object => @recording.genres) end end \ No newline at end of file diff --git a/app/views/api_bands/show.rabl b/app/views/api_bands/show.rabl index 23f58272e..fc4b171d9 100644 --- a/app/views/api_bands/show.rabl +++ b/app/views/api_bands/show.rabl @@ -1,6 +1,6 @@ object @band -attributes :id, :name, :city, :state, :country, :website, :biography, :photo_url, :logo_url, :follower_count +attributes :id, :name, :city, :state, :country, :website, :biography, :photo_url, :logo_url, :liker_count, :follower_count, :recording_count, :session_count unless @band.users.nil? || @band.users.size == 0 child :users => :musicians do @@ -20,5 +20,6 @@ end unless @band.genres.nil? || @band.genres.size == 0 child :genres => :genres do attributes :id, :description + #partial('api_genres/index', :object => @band.genres) end end diff --git a/app/views/api_users/recording_index.rabl b/app/views/api_users/recording_index.rabl index 434361bd1..d0bf502d6 100644 --- a/app/views/api_users/recording_index.rabl +++ b/app/views/api_users/recording_index.rabl @@ -1,11 +1,12 @@ collection @recordings -attributes :id, :description, :public +attributes :id, :description, :public, :favorite_count node :genres do |recording| unless recording.genres.nil? || recording.genres.size == 0 child :genres => :genres do attributes :id, :description + #partial('api_genres/index', :object => recording.genres) end end end \ No newline at end of file diff --git a/app/views/api_users/recording_show.rabl b/app/views/api_users/recording_show.rabl index 95ca98734..f0248d79c 100644 --- a/app/views/api_users/recording_show.rabl +++ b/app/views/api_users/recording_show.rabl @@ -1,9 +1,12 @@ object @recording -attributes :id, :description, :public +attributes :id, :description, :public, :favorite_count unless @recording.genres.nil? || @recording.genres.size == 0 child :genres => :genres do attributes :id, :description + #partial('api_genres/index', :object => @band.genres) end -end \ No newline at end of file +end + +#TODO: show the Users that have added this Recording as a Favorite ONLY IF the owning user has "Better Analytics service level" \ No newline at end of file diff --git a/app/views/api_users/show.rabl b/app/views/api_users/show.rabl index 740ddda93..60d306346 100644 --- a/app/views/api_users/show.rabl +++ b/app/views/api_users/show.rabl @@ -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, :favorite_count +attributes :id, :first_name, :last_name, :city, :state, :country, :online, :photo_url, :musician, :gender, :birth_date, :internet_service_provider, :friend_count, :liker_count, :follower_count, :following_count, :favorite_count, :recording_count, :session_count unless @user.friends.nil? || @user.friends.size == 0 child :friends => :friends do @@ -14,6 +14,7 @@ unless @user.bands.nil? || @user.bands.size == 0 child :genres => :genres do attributes :id, :description + #partial('api_genres/index', :object => @user.bands.genres) end end end diff --git a/config/routes.rb b/config/routes.rb index fd9bad358..b4a3ba0f9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -75,7 +75,13 @@ SampleApp::Application.routes.draw do 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 + match '/users/:id/followings' => 'api_users#following_destroy', :via => :delete + + # user likes + match '/users/:id/likes' => 'api_users#like_index', :via => :get, :as => 'api_user_like_index' + match '/users/:id/band_likes' => 'api_users#band_like_index', :via => :get, :as => 'api_band_like_index' + match '/users/:id/likes' => 'api_users#like_create', :via => :post + match '/users/:id/likes' => 'api_users#like_destroy', :via => :delete # user recordings match '/users/:id/recordings' => 'api_users#recording_index', :via => :get diff --git a/spec/requests/users_api_spec.rb b/spec/requests/users_api_spec.rb index 6124167a4..5086fbe68 100644 --- a/spec/requests/users_api_spec.rb +++ b/spec/requests/users_api_spec.rb @@ -43,7 +43,7 @@ describe "User API", :type => :api do def delete_user_following(authenticated_user, source_user, target_user) login(authenticated_user.email, authenticated_user.password, 200, true) - delete "/api/users/#{source_user.id}/followings/#{target_user.id}.json" + delete "/api/users/#{source_user.id}/followings.json", { :user_id => target_user.id }.to_json, "CONTENT_TYPE" => 'application/json' return last_response end @@ -304,6 +304,7 @@ describe "User API", :type => :api do private_description = "My Private Recording" last_response = create_user_recording(user, user, private_description, false, ["rock"]) last_response.status.should == 201 + private_recording = JSON.parse(last_response.body) private_recording["description"].should == private_description private_recording["public"].should == false @@ -416,6 +417,12 @@ describe "User API", :type => :api do favorites[0]["recording_id"].should == recording["id"] favorites[0]["description"].should == "My Recording" favorites[0]["public"].should == true + + # get recording + last_response = get_user_recording(user, user, recording["id"]) + last_response.status.should == 200 + recording_with_favorite = JSON.parse(last_response.body) + recording_with_favorite["favorite_count"].should == 1 end it "should not allow user to create favorite for another user" do