Skip to content

Instantly share code, notes, and snippets.

@quangpham
Created April 28, 2015 13:58
Show Gist options
  • Save quangpham/790d583fc7c3a0f04172 to your computer and use it in GitHub Desktop.
Save quangpham/790d583fc7c3a0f04172 to your computer and use it in GitHub Desktop.
class Api::Mobile::V1::GroupsController < Api::Mobile::V1::ApplicationApiController
acts_as_token_authentication_handler_for User
before_action :check_user_house_access
before_action :check_group_id, only: [:accept, :leave, :join]
before_action :check_user_group_access, only: [:accept, :leave]
#Todo: invite friends to group
def index
public_groups = Group.where(house_id: params[:house_id], public: true).select(:id, :name, :icon, :feed_type)
public_groups_ids = public_groups.map { |g| g.id }
invited_groups_ids = UserGroupR.where(user_id: current_user.id, accepted: false).map { |r| r.group_id }
invited_groups_ids = invited_groups_ids & public_groups_ids
invited_groups = Group.where(id: invited_groups_ids).select(:id, :name, :icon, :feed_type)
accepted_groups_ids = UserGroupR.where(user_id: current_user.id, accepted: true).map { |r| r.group_id }
accepted_groups_ids = accepted_groups_ids & public_groups_ids
accepted_groups = Group.where(id: accepted_groups_ids).select(:id, :name, :icon, :feed_type)
return render json: {accepted: accepted_groups, invited: invited_groups, public: public_groups - accepted_groups - invited_groups}
end
def create
return render status: 400, json: {error: "name_missing"} unless params[:name]
return render status: 400, json: {error: "public_missing"} if params[:public].nil?
group = Group.create(params.permit(:house_id, :name, :public, :description))
return render json: group if UserGroupR.create(user_id: current_user.id, group_id: group.id, accepted: true)
end
def accept
return render json: {message: "ok"} if @user_group_r.update(accepted: true)
end
def leave
return render json: {message: "ok"} if @user_group_r.destroy
end
def join
return render status: 400, json: {error: "user_already_join_this_group"} if UserGroupR.find_by(user_id: current_user.id, group_id: params[:id], accepted: true)
return render json: {message: "ok"} if UserGroupR.create(user_id: current_user.id, group_id: params[:id], accepted: true)
end
private
def check_user_house_access
return render status: 400, json: {error: "house_id_missing"} unless params[:house_id]
if user_house_r = UserHouseR.find_by(user_id: current_user.id, house_id: params[:house_id])
return render status: 403, json: {error: "membership_not_yet_confirmed"} unless user_house_r.is_member
else
return render status: 403, json: {error: "use_is_not_member_of_this_house"}
end
end
def check_group_id
return render status: 400, json: {error: "id_missing"} unless params[:id]
end
def check_user_group_access
@user_group_r = UserGroupR.find_by(user_id: current_user.id, group_id: params[:id])
return render status: 400, json: {error: "user_group_r_not_found"} unless @user_group_r
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment