Skip to content

Commit

Permalink
Add machine_groups endpoint and machine_group_id param to machines en…
Browse files Browse the repository at this point in the history
…dpoint
  • Loading branch information
RyanTG committed Oct 20, 2024
1 parent c91fa84 commit 1ef0bd4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
17 changes: 17 additions & 0 deletions app/controllers/api/v1/machine_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Api
module V1
class MachineGroupsController < InheritedResources::Base
skip_before_action :verify_authenticity_token

before_action :allow_cors
respond_to :json

api :GET, '/api/v1/machine_groups.json', 'Fetch all machine groups'
description 'Machine group IDs'
formats ['json']
def index
return_response(MachineGroup.all, 'machine_groups', nil, nil, 200)
end
end
end
end
3 changes: 3 additions & 0 deletions app/controllers/api/v1/machines_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MachinesController < InheritedResources::Base
description 'These are the canonical machine descriptions, not the location-centric ones'
param :no_details, Integer, desc: 'Omit unnecessary metadata for initial app loading', required: false
param :region_id, Integer, desc: 'show only machines from this region', required: false
param :machine_group_id, Integer, desc: 'show only machines from machine group id', required: false
param :manufacturer, String, desc: 'show only machines from this manufacturer', required: false
formats ['json']
def index
Expand All @@ -18,6 +19,8 @@ def index

machines = machines.select { |m| m.manufacturer == params[:manufacturer] } if params[:manufacturer]

machines = Machine.where(machine_group_id: params[:machine_group_id]) if params[:machine_group_id]

return_response(machines, 'machines', nil, nil, 200, except)
end
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
resources :machine_conditions, only: [:destroy, :update]
resources :machine_score_xrefs, only: [:create, :show]
resources :machines, only: [:index, :show]
resources :machine_groups, only: [:index, :show]
resources :operators, only: [:index, :show]
resources :statuses, only: [:index, :show]

Expand Down
17 changes: 17 additions & 0 deletions spec/requests/api/v1/machine_groups_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe Api::V1::MachineGroupsController, type: :request do
describe '#index' do
before(:each) do
FactoryBot.create(:machine_group, id: 66, name: 'Cleo Group')
FactoryBot.create(:machine_group, id: 67, name: 'Bawb Group')
end

it 'returns all machine groups in the database' do
get '/api/v1/machine_groups.json'

expect(response.body).to include('Cleo Group')
expect(response.body).to include('Bawb Group')
end
end
end
11 changes: 9 additions & 2 deletions spec/requests/api/v1/machines_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe Api::V1::MachinesController, type: :request do
describe '#index' do
before(:each) do
FactoryBot.create(:machine, id: 66, name: 'Cleo', manufacturer: 'Stern')
FactoryBot.create(:machine, id: 67, name: 'Bawb', manufacturer: 'Williams')
FactoryBot.create(:machine, id: 66, name: 'Cleo', manufacturer: 'Stern', machine_group_id: 1)
FactoryBot.create(:machine, id: 67, name: 'Bawb', manufacturer: 'Williams', machine_group_id: 2)
end

it 'returns all machines in the database' do
Expand All @@ -21,6 +21,13 @@
expect(response.body).to_not include('Bawb')
end

it 'respects machine_group+id param' do
get '/api/v1/machines.json?machine_group_id=1'

expect(response.body).to include('Cleo')
expect(response.body).to_not include('Bawb')
end

it 'respects no_details param' do
get '/api/v1/machines.json?no_details=1'

Expand Down

0 comments on commit 1ef0bd4

Please sign in to comment.