Class: SupplejackApi::UserSetsController

Inherits:
ApplicationController show all
Includes:
Concerns::UserSetsControllerMetrics
Defined in:
app/controllers/supplejack_api/user_sets_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin!, #authenticate_user!, #current_user, #find_user_set

Instance Method Details

#admin_indexObject

Endpoint for the administrator to get a list of sets for any user.


25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/supplejack_api/user_sets_controller.rb', line 25

def admin_index
  @user = User.find_by_api_key(params[:user_id])

  if @user
    @user_sets = @user.user_sets
    render json: serializable_array(@user_sets).to_json
  else
    render json: { errors: "The user with api key: '#{params[:user_id]}' was not found" }, status: :not_found
  end
end

#createObject


57
58
59
60
61
62
63
64
# File 'app/controllers/supplejack_api/user_sets_controller.rb', line 57

def create
  @user_set = current_user.user_sets.build
  if @user_set.update_attributes_and_embedded(params[:set])
    render json: UserSetSerializer.new(@user_set, user: true)
  else
    render json: { errors: @user_set.errors.to_hash }, status: :unprocessable_entity
  end
end

#destroyObject


74
75
76
77
# File 'app/controllers/supplejack_api/user_sets_controller.rb', line 74

def destroy
  @user_set.destroy
  respond_with(@user_set)
end

43
44
45
46
# File 'app/controllers/supplejack_api/user_sets_controller.rb', line 43

def featured_sets_index
  @user_sets = UserSet.featured_sets(4)
  render json: serializable_array(@user_sets, user: true, featured: true).to_json
end

#indexObject


18
19
20
21
# File 'app/controllers/supplejack_api/user_sets_controller.rb', line 18

def index
  @user_sets = current_user.user_sets
  render json: serializable_array(@user_sets).to_json
end

#public_indexObject

Enpoint for the administrator to fetch a list of all public sets


38
39
40
41
# File 'app/controllers/supplejack_api/user_sets_controller.rb', line 38

def public_index
  @user_sets = UserSet.public_sets(page: params[:page])
  render json: serializable_array(@user_sets, user: true, total: true).to_json
end

#showObject


48
49
50
51
52
53
54
55
# File 'app/controllers/supplejack_api/user_sets_controller.rb', line 48

def show
  @user_set = UserSet.custom_find(params[:id])
  if @user_set
    render json: UserSetSerializer.new(@user_set, user: current_user)
  else
    render json: { errors: "Set with id: #{params[:id]} was not found." }, status: :not_found
  end
end

#updateObject


66
67
68
69
70
71
72
# File 'app/controllers/supplejack_api/user_sets_controller.rb', line 66

def update
  if @user_set.update_attributes_and_embedded(params[:set], current_user)
    render json: UserSetSerializer.new(@user_set, user: true)
  else
    render json: { errors: @user_set.errors.to_hash }, status: :unprocessable_entity
  end
end