jam-cloud/app/controllers/api_users_controller.rb

82 lines
2.5 KiB
Ruby

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 = 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 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
@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
# 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