Class: Api::V1::AspectsController

Inherits:
BaseController show all
Defined in:
app/controllers/api/v1/aspects_controller.rb

Instance Attribute Summary

Attributes included from OpenidConnect::ProtectedResourceEndpoint

#current_token

Instance Method Summary collapse

Methods included from OpenidConnect::ProtectedResourceEndpoint

#access_token?, #require_access_token

Instance Method Details

#createObject


30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/api/v1/aspects_controller.rb', line 30

def create
  params.require(%i[name])
  aspect = current_user.aspects.build(name: params[:name])
  if aspect&.save
    render json: aspect_as_json(aspect, true)
  else
    render_error 422, "Failed to create the aspect"
  end
rescue ActionController::ParameterMissing
  render_error 422, "Failed to create the aspect"
end

#destroyObject


56
57
58
59
60
61
62
63
# File 'app/controllers/api/v1/aspects_controller.rb', line 56

def destroy
  aspect = current_user.aspects.where(id: params[:id]).first
  if aspect&.destroy
    head :no_content
  else
    render_error 422, "Failed to delete the aspect"
  end
end

#indexObject


14
15
16
17
18
19
# File 'app/controllers/api/v1/aspects_controller.rb', line 14

def index
  aspects_query = current_user.aspects
  aspects_page = index_pager(aspects_query).response
  aspects_page[:data] = aspects_page[:data].map {|a| aspect_as_json(a, false) }
  render_paged_api_response aspects_page
end

#showObject


21
22
23
24
25
26
27
28
# File 'app/controllers/api/v1/aspects_controller.rb', line 21

def show
  aspect = current_user.aspects.where(id: params[:id]).first
  if aspect
    render json: aspect_as_json(aspect, true)
  else
    render_error 404, "Aspect with provided ID could not be found"
  end
end

#updateObject


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/api/v1/aspects_controller.rb', line 42

def update
  aspect = current_user.aspects.where(id: params[:id]).first

  if !aspect
    render_error 404, "Failed to update the aspect"
  elsif aspect.update!(aspect_params(true))
    render json: aspect_as_json(aspect, true)
  else
    render_error 422, "Failed to update the aspect"
  end
rescue ActionController::ParameterMissing, ActiveRecord::RecordInvalid
  render_error 422, "Failed to update the aspect"
end