class ApiUsersController < ApplicationController before_filter :signed_in_user, only: [:index, :edit, :update, :delete, :friend_request_index, :friend_request_show, :friend_request_create, :friend_request_update, :friend_index, :friend_destroy] respond_to :json def index @users = User.paginate(page: params[:page]) end def show @user = User.find(params[:id]) end def create User.save(params) respond_with @user, responder: ApiResponder, :location => api_user_detail_url(@user) end def update User.save(params) respond_with @user, responder: ApiResponder end def delete @user = User.find(params[:id]) @user.delete respond_with @user, responder: ApiResponder end 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") 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 ActiveRecord::Base.transaction do @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 end respond_with @friend_request, responder: ApiResponder end def friend_index # NOTE: friend_index.rabl template references the friends property @user = User.find(params[:id]) 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