diff --git a/app/controllers/api_users_controller.rb b/app/controllers/api_users_controller.rb index 2aa4670b9..86a7d3638 100644 --- a/app/controllers/api_users_controller.rb +++ b/app/controllers/api_users_controller.rb @@ -1,7 +1,8 @@ class ApiUsersController < ApplicationController before_filter :signed_in_user, only: [:index, :edit, :update, :delete, - :friend_request_index, :friend_request_create, + :friend_request_index, :friend_request_show, + :friend_request_create, :friend_request_update, :friend_index, :friend_destroy] respond_to :json @@ -10,35 +11,61 @@ class ApiUsersController < ApplicationController @users = User.paginate(page: params[:page]) end + def show + @user = User.find(params[:id]) + end + def create @user = User.new() + @user.email = params[:email] @user.creator = current_user @user.description = params[:description] @user.save respond_with @user, responder: ApiResponder, :location => api_user_detail_url(@user) end - def show - @user = User.find(params[:id]) - end - def delete @user = User.find(params[:id]) @user.delete - respond_with @user, responder: ApiResponder end def friend_request_index - end - - def friend_request_create + # 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 + @friend_request = FriendRequest.find(params[:id]) + end + + def friend_request_create + @friend_request = FriendRequest.new() + @friend_request.user_id = params[:user_id] + @friend_request.friend_id = params[:friend_id] + @friend_request.save + respond_with @friend_request, responder: ApiResponder, :location => api_friend_request_detail_url(@friend_request) end def friend_request_update + @friend_request = FriendRequest.find(params[:id]) + @friend_request.accepted = params[:accepted] + @friend_request.save + + # create both records for this friendship + if @friend_request.accepted? + @friendship = Friendship.new() + @friendship.user_id = @friend_request.user_id + @friendship.friend_id = @friend_request.friend_id + @friendship.save + + @friendship = Friendship.new() + @friendship.user_id = @friend_request.friend_id + @friendship.friend_id = @friend_request.user_id + @friendship.save + end + + respond_with @friend_request, responder: ApiResponder end def friend_index @@ -47,7 +74,9 @@ class ApiUsersController < ApplicationController end def friend_destroy + # 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 end 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 339c8928b..b9a4cc999 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, :online \ No newline at end of file +attributes :id, :name, :email, :online \ No newline at end of file diff --git a/app/views/api_users/friend_request_index.rabl b/app/views/api_users/friend_request_index.rabl new file mode 100644 index 000000000..5d5261574 --- /dev/null +++ b/app/views/api_users/friend_request_index.rabl @@ -0,0 +1,3 @@ +object @friend_requests + +extends "api_users/friend_request_show" \ No newline at end of file diff --git a/app/views/api_users/friend_request_show.rabl b/app/views/api_users/friend_request_show.rabl new file mode 100644 index 000000000..74daf6176 --- /dev/null +++ b/app/views/api_users/friend_request_show.rabl @@ -0,0 +1,3 @@ +object @friend_request + +attributes :id, :user_id, :friend_id, :accepted, :created_at \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d7dbc0787..2b4a66d46 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ SampleApp::Application.routes.draw do resources :users resources :music_sessions + resources :friend_requests resources :sessions, only: [:new, :create, :destroy] @@ -34,16 +35,16 @@ SampleApp::Application.routes.draw do # users match '/users' => 'api_users#index', :via => :get - match '/users' => 'api_users#create', :via => :post match '/users/:id' => 'api_users#show', :via => :get, :as => 'api_user_detail' - match '/users/:id' => 'api_users#edit', :via => :put + match '/users' => 'api_users#create', :via => :post + match '/users/:id' => 'api_users#update', :via => :put match '/users/:id' => 'api_users#destroy', :via => :delete # friend requests match '/users/:id/friend_requests' => 'api_users#friend_request_index', :via => :get - match '/users/:id/friend_requests' => 'api_users#friend_request_create', :via => :post - match '/users/:id/friends/:friend_request_id' => 'api_users#friend_request_show', :via => :get, :as => 'api_user_friend_request_detail' - match '/users/:id/friends/:friend_request_id' => 'api_users#friend_request_update', :via => :put + match '/friend_requests/:id' => 'api_users#friend_request_show', :via => :get, :as => 'api_friend_request_detail' + match '/friend_requests' => 'api_users#friend_request_create', :via => :post + match '/friend_requests/:id' => 'api_users#friend_request_update', :via => :put # friends match '/users/:id/friends' => 'api_users#friend_index', :via => :get