diff --git a/app/controllers/api_bands_controller.rb b/app/controllers/api_bands_controller.rb index 8441ca533..28f55ece6 100644 --- a/app/controllers/api_bands_controller.rb +++ b/app/controllers/api_bands_controller.rb @@ -1,6 +1,7 @@ class ApiBandsController < ApplicationController - before_filter :signed_in_user, only: [:index, :show, :create, :update] + before_filter :signed_in_user, only: [:index, :show, :create, :update, + :following_create, :following_destroy] respond_to :json @@ -43,4 +44,25 @@ class ApiBandsController < ApplicationController end end + # 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) + end + + def following_destroy + JamRuby::BandFollower.delete_all "(user_id = '#{params[:user_id]}' AND band_id = '#{params[:id]}')" + respond_with responder: ApiResponder + end + end \ No newline at end of file diff --git a/app/controllers/api_users_controller.rb b/app/controllers/api_users_controller.rb index 09db4e923..2a3ef1ebe 100644 --- a/app/controllers/api_users_controller.rb +++ b/app/controllers/api_users_controller.rb @@ -21,7 +21,7 @@ class ApiUsersController < ApplicationController # check for errors if @user.errors.nil? || @user.errors.size == 0 respond_with @user, responder: ApiResponder, :location => api_user_detail_url(@user) - + else raise ActiveRecord::Rollback response.status = :unprocessable_entity @@ -49,6 +49,44 @@ class ApiUsersController < ApplicationController respond_with @user, responder: ApiResponder end + # FOLLOWERS + def follower_index + # NOTE: follower_index.rabl template references the followers property + @user = User.find(params[:id]) + end + + # FOLLOWINGS + def following_index + @user = User.find(params[:id]) + + # TODO: get band followings and merge (@user.band_followings) + end + + def following_create + + if !params[:user_id].nil? + @follower = UserFollower.new() + @follower.user_id = params[:user_id] + @follower.follower_id = params[:id] + + elsif !params[:band_id].nil? + @follower = BandFollower.new() + @follower.band_id = params[:band_id] + @follower.follower_id = params[:id] + 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 "(ban_id = '#{params[:band_id]}' AND follower_id = '#{params[:id]}')" + respond_with responder: ApiResponder + end + + # FRIENDS def friend_request_index # get all outgoing and incoming friend requests @friend_requests = FriendRequest.where("(friend_id='#{params[:id]}' OR user_id='#{params[:id]}') AND accepted is null") diff --git a/app/views/api_bands/follower_index.rabl b/app/views/api_bands/follower_index.rabl new file mode 100644 index 000000000..3408346cd --- /dev/null +++ b/app/views/api_bands/follower_index.rabl @@ -0,0 +1,27 @@ +object @band.followers + +attributes :follower_id => :user_id + +node :name do |follower| + follower.user.name +end + +node :city do |follower| + follower.user.city +end + +node :state do |follower| + follower.user.state +end + +node :country do |follower| + follower.user.country +end + +node :musician do |follower| + follower.user.musician +end + +node :photo_url do |follower| + follower.user.photo_url +end \ No newline at end of file diff --git a/app/views/api_bands/following_create.rabl b/app/views/api_bands/following_create.rabl new file mode 100644 index 000000000..94be1bebd --- /dev/null +++ b/app/views/api_bands/following_create.rabl @@ -0,0 +1,3 @@ +object @band.followings + +extends "api_bands/following_index" \ No newline at end of file diff --git a/app/views/api_bands/index.rabl b/app/views/api_bands/index.rabl index def94e9f6..c1e57847b 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, :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/show.rabl b/app/views/api_bands/show.rabl index 2c869fb54..0a942d3c3 100644 --- a/app/views/api_bands/show.rabl +++ b/app/views/api_bands/show.rabl @@ -1,6 +1,6 @@ object @band -attributes :id, :name, :website, :biography, :photo_url, :logo_url +attributes :id, :name, :city, :state, :country, :website, :biography, :photo_url, :logo_url, :follower_count unless @band.users.nil? || @band.users.size == 0 child :users => :musicians do diff --git a/app/views/api_users/follower_index.rabl b/app/views/api_users/follower_index.rabl new file mode 100644 index 000000000..35122bee5 --- /dev/null +++ b/app/views/api_users/follower_index.rabl @@ -0,0 +1,27 @@ +object @user.followers + +attributes :follower_id => :user_id + +node :name do |follower| + follower.user.name +end + +node :city do |follower| + follower.user.city +end + +node :state do |follower| + follower.user.state +end + +node :country do |follower| + follower.user.country +end + +node :musician do |follower| + follower.user.musician +end + +node :photo_url do |follower| + follower.user.photo_url +end \ No newline at end of file diff --git a/app/views/api_users/following_create.rabl b/app/views/api_users/following_create.rabl new file mode 100644 index 000000000..c95a073e0 --- /dev/null +++ b/app/views/api_users/following_create.rabl @@ -0,0 +1,3 @@ +object @user.followings + +extends "api_users/following_index" \ No newline at end of file diff --git a/app/views/api_users/following_index.rabl b/app/views/api_users/following_index.rabl new file mode 100644 index 000000000..d1a31401b --- /dev/null +++ b/app/views/api_users/following_index.rabl @@ -0,0 +1,27 @@ +object @user.followings + +attributes :user_id + +node :name do |following| + following.user.name +end + +node :city do |following| + following.user.city +end + +node :state do |following| + following.user.state +end + +node :country do |following| + following.user.country +end + +node :musician do |following| + following.user.musician +end + +node :photo_url do |following| + following.user.photo_url +end \ No newline at end of file diff --git a/app/views/api_users/friend_index.rabl b/app/views/api_users/friend_index.rabl index b9a4cc999..3e5c75cd2 100644 --- a/app/views/api_users/friend_index.rabl +++ b/app/views/api_users/friend_index.rabl @@ -1,3 +1,3 @@ object @user.friends -attributes :id, :name, :email, :online \ No newline at end of file +attributes :id, :name, :city, :state, :country, :email, :online \ No newline at end of file diff --git a/app/views/api_users/index.rabl b/app/views/api_users/index.rabl index 6e0ebdb53..34558346d 100644 --- a/app/views/api_users/index.rabl +++ b/app/views/api_users/index.rabl @@ -1,4 +1,4 @@ collection @users # do not retrieve all child collections when showing a list of users -attributes :id, :name, :email, :online, :musician, :photo_url \ No newline at end of file +attributes :id, :name, :city, :state, :country, :email, :online, :musician, :photo_url \ No newline at end of file diff --git a/app/views/api_users/show.rabl b/app/views/api_users/show.rabl index 473770206..a4589cac9 100644 --- a/app/views/api_users/show.rabl +++ b/app/views/api_users/show.rabl @@ -1,6 +1,6 @@ object @user -attributes :id, :name, :email, :online, :photo_url +attributes :id, :name, :city, :state, :country, :email, :online, :photo_url, :friend_count, :follower_count, :following_count unless @user.friends.nil? || @user.friends.size == 0 child :friends => :friends do diff --git a/config/routes.rb b/config/routes.rb index 157c58185..c861a0482 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,28 +43,6 @@ SampleApp::Application.routes.draw do match '/users/:id' => 'api_users#update', :via => :post match '/users/:id' => 'api_users#destroy', :via => :delete - # bands - match '/bands' => 'api_bands#index', :via => :get - match '/bands/:id' => 'api_bands#show', :via => :get, :as => 'api_band_detail' - match '/bands' => 'api_bands#create', :via => :post - match '/bands/:id' => 'api_bands#update', :via => :post -=begin - match '/bands/:id/followers' => 'api_bands#followers_index', :via => :get - - # followers - match '/users/:id/followers' => 'api_users#followers_index', :via => :get - - # followings - match '/users/:id/followings' => 'api_users#followings_index', :via => :get - match '/users/:id/followings' => 'api_users#followings_create', :via => :post - match '/users/:id/followings/:user_id' => 'api_users#followings_destroy', :via => :delete - - # favorites - match '/users/:id/favorites' => 'api_users#favorites_index', :via => :get - match '/users/:id/favorites' => 'api_users#favorites_create', :via => :post - match '/users/:id/favorites/:recording_id' => 'api_users#favorites_destroy', :via => :delete -=end - # friend requests match '/users/:id/friend_requests' => 'api_users#friend_request_index', :via => :get match '/friend_requests/:id' => 'api_users#friend_request_show', :via => :get, :as => 'api_friend_request_detail' @@ -75,13 +53,38 @@ SampleApp::Application.routes.draw do match '/users/:id/friends' => 'api_users#friend_index', :via => :get match '/users/:id/friends/:friend_id' => 'api_users#friend_destroy', :via => :delete + # user followers + 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_create', :via => :post + match '/users/:id/followings/:user_id' => 'api_users#following_destroy', :via => :delete + + # favorites + match '/users/:id/favorites' => 'api_users#favorite_index', :via => :get + match '/users/:id/favorites' => 'api_users#favorite_create', :via => :post + match '/users/:id/favorites/:recording_id' => 'api_users#favorite_destroy', :via => :delete + + # bands + match '/bands' => 'api_bands#index', :via => :get + match '/bands/:id' => 'api_bands#show', :via => :get, :as => 'api_band_detail' + match '/bands' => 'api_bands#create', :via => :post + match '/bands/:id' => 'api_bands#update', :via => :post + + # band followers + match '/bands/:id/followers' => 'api_bands#follower_index', :via => :get +=begin + # band followings + match '/bands/:id/followings' => 'api_bands#following_create', :via => :post + match '/bands/:id/followings/:user_id' => 'api_bands#following_destroy', :via => :delete +=end # invitations match '/invitations/:id' => 'api_invitations#show', :via => :get, :as => 'api_invitation_detail' match '/invitations/:id' => 'api_invitations#delete', :via => :delete match '/invitations' => 'api_invitations#index', :via => :get match '/invitations' => 'api_invitations#create', :via => :post - # invitations match '/instruments/:id' => 'api_instruments#show', :via => :get, :as => 'api_instrument_detail' match '/instruments' => 'api_instruments#index', :via => :get