From 1fa514b2bd97c73e9c572d03f1d4b6a6074763ef Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Sun, 14 Oct 2012 00:29:49 -0400 Subject: [PATCH] added /friends subresource for user API --- app/controllers/api_users_controller.rb | 19 ++++++++++++------- app/views/api_users/friend_index.rabl | 3 +++ app/views/api_users/show.rabl | 2 +- config/routes.rb | 4 +--- 4 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 app/views/api_users/friend_index.rabl diff --git a/app/controllers/api_users_controller.rb b/app/controllers/api_users_controller.rb index a30b69203..2aa4670b9 100644 --- a/app/controllers/api_users_controller.rb +++ b/app/controllers/api_users_controller.rb @@ -1,6 +1,8 @@ class ApiUsersController < ApplicationController - before_filter :signed_in_user, only: [:index, :edit, :update, :destroy] + before_filter :signed_in_user, only: [:index, :edit, :update, :delete, + :friend_request_index, :friend_request_create, + :friend_index, :friend_destroy] respond_to :json @@ -33,16 +35,19 @@ class ApiUsersController < ApplicationController def friend_request_create end + def friend_request_show + end + + def friend_request_update + end + def friend_index - end - - def friend_create - end - - def friend_show + # NOTE: friend_index.rabl template references the friends property + @user = User.find(params[:id]) end def friend_destroy + 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]}')" 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 new file mode 100644 index 000000000..339c8928b --- /dev/null +++ b/app/views/api_users/friend_index.rabl @@ -0,0 +1,3 @@ +object @user.friends + +attributes :id, :name, :online \ No newline at end of file diff --git a/app/views/api_users/show.rabl b/app/views/api_users/show.rabl index bdc5be9b2..ef2d8a16e 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, :admin +attributes :id, :name, :email, :admin, :online child :friends => :friends do attributes :id, :name, :online diff --git a/config/routes.rb b/config/routes.rb index 501a8228d..d7dbc0787 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,12 +43,10 @@ SampleApp::Application.routes.draw do 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_edit', :via => :put + match '/users/:id/friends/:friend_request_id' => 'api_users#friend_request_update', :via => :put # friends match '/users/:id/friends' => 'api_users#friend_index', :via => :get - match '/users/:id/friends' => 'api_users#friend_create', :via => :post - match '/users/:id/friends/:friend_id' => 'api_users#friend_show', :via => :get, :as => 'api_user_friend_detail' match '/users/:id/friends/:friend_id' => 'api_users#friend_destroy', :via => :delete end end