more friend API development
This commit is contained in:
parent
1fa514b2bd
commit
aaade12b59
|
|
@ -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
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
object @user.friends
|
||||
|
||||
attributes :id, :name, :online
|
||||
attributes :id, :name, :email, :online
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
object @friend_requests
|
||||
|
||||
extends "api_users/friend_request_show"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
object @friend_request
|
||||
|
||||
attributes :id, :user_id, :friend_id, :accepted, :created_at
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue