Skip to content

Commit

Permalink
Add device description api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
beque committed Jan 20, 2025
1 parent 319ef62 commit fd56b5b
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def to_json_camel_case(val)
mount Chemotion::AdminDeviceAPI
mount Chemotion::AdminDeviceMetadataAPI
mount Chemotion::ChemicalAPI
mount Chemotion::DeviceDescriptionAPI

if Rails.env.development?
add_swagger_documentation(info: {
Expand Down
106 changes: 106 additions & 0 deletions app/api/chemotion/device_description_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# frozen_string_literal: true

module Chemotion
class DeviceDescriptionAPI < Grape::API
resource :device_descriptions do
# create a device description
params do
optional :name, type: String
optional :short_label, type: String
optional :vendor_name, type: String
optional :vendor_id, type: String
optional :vendor_url, type: String
optional :serial_number, type: String
optional :doi, type: String
optional :doi_url, type: String
optional :device_type, type: String
optional :device_type_detail, type: String
optional :operation_mode, type: String
optional :installation_start_date, type: DateTime, allow_blank: true
optional :installation_end_date, type: DateTime, allow_blank: true
optional :description_and_comments, type: String
optional :technical_operator, type: Hash
optional :administrative_operator, type: Hash
optional :university_campus, type: String
optional :institute, type: String
optional :building, type: String
optional :room, type: String
optional :infrastructure_assignment, type: String
optional :access_options, type: String
optional :comments, type: String
optional :size, type: String
optional :weight, type: String
optional :application_name, type: String
optional :application_version, type: String
optional :description_for_method_part, type: String
end
post do
attributes = declared(params, include_missing: false)
device_description = DeviceDescription.create!(attributes)

present device_description, with: Entities::DeviceDescriptionEntity, root: :device_description
end

# Return serialized device description by id
params do
requires :id, type: Integer, desc: 'Device description id'
end
route_param :id do
get do
device_description = DeviceDescription.find(params[:id])

present device_description, with: Entities::DeviceDescriptionEntity, root: :device_description
end
end

# update a device description
params do
requires :id, type: Integer
optional :device_id, type: Integer, description: 'Linked device'
optional :name, type: String
optional :short_label, type: String
optional :vendor_name, type: String
optional :vendor_id, type: String
optional :vendor_url, type: String
optional :serial_number, type: String
optional :doi, type: String
optional :doi_url, type: String
optional :device_type, type: String
optional :device_type_detail, type: String
optional :operation_mode, type: String
optional :installation_start_date, type: DateTime, allow_blank: true
optional :installation_end_date, type: DateTime, allow_blank: true
optional :description_and_comments, type: String
optional :technical_operator, type: Hash
optional :administrative_operator, type: Hash
optional :university_campus, type: String
optional :institute, type: String
optional :building, type: String
optional :room, type: String
optional :infrastructure_assignment, type: String
optional :access_options, type: String
optional :comments, type: String
optional :size, type: String
optional :weight, type: String
optional :application_name, type: String
optional :application_version, type: String
optional :description_for_methods_part, type: String
end
put ':id' do
device_description = DeviceDescription.find(params[:id])
attributes = declared(params, include_missing: false)
device_description.update!(attributes)

present device_description, with: Entities::DeviceDescriptionEntity, root: :device_description
end

# delete a device description
delete ':id' do
device_description = DeviceDescription.find(params[:id])
error!('Device could not be deleted', 400) unless device_description.present? && device_description.destroy

{ deleted: device_description.id }
end
end
end
end
36 changes: 36 additions & 0 deletions app/api/entities/device_description_entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module Entities
class DeviceDescriptionEntity < Grape::Entity
expose :id
expose :device_id
expose :name
expose :short_label
expose :vendor_name
expose :vendor_id
expose :vendor_url
expose :serial_number
expose :doi
expose :doi_url
expose :device_type
expose :device_type_detail
expose :operation_mode
expose :installation_start_date
expose :installation_end_date
expose :description_and_comments
expose :technical_operator
expose :administrative_operator
expose :university_campus
expose :institute
expose :building
expose :room
expose :infrastructure_assignment
expose :access_options
expose :comments
expose :size
expose :weight
expose :application_name
expose :application_version
expose :description_for_methods_part
end
end
54 changes: 54 additions & 0 deletions spec/api/chemotion/device_description_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

describe Chemotion::DeviceDescriptionAPI do
include_context 'api request authorization context'

let(:device_description) { create(:device_description) }

describe 'POST /api/v1/device_descriptions' do
let(:device_description_params) { attributes_for(:device_description) }

it 'creates a device description' do
post '/api/v1/device_descriptions', params: device_description_params

expect(parsed_json_response['device_description']['short_label']).to include('DD')
end
end

describe 'GET /api/v1/device_descriptions/:id' do
before do
device_description
end

it 'fetches an device description by id' do
get "/api/v1/device_descriptions/#{device_description.id}"

expect(parsed_json_response['device_description']['name']).to eql(device_description.name)
end
end

describe 'PUT /api/v1/device_descriptions/:id' do
context 'when updating an device description' do
let(:params) do
{
name: 'new name',
short_label: 'DD1-2',
}
end

it 'returns the updated device description' do
put "/api/v1/device_descriptions/#{device_description.id}", params: params

expect(parsed_json_response['device_description']).to include(params.stringify_keys)
end
end
end

describe 'DELETE /api/v1/device_descriptions/:id' do
it 'deletes a device description' do
delete "/api/v1/device_descriptions/#{device_description.id}"

expect(parsed_json_response).to include('deleted' => device_description.id)
end
end
end
9 changes: 9 additions & 0 deletions spec/factories/device_descriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

FactoryBot.define do
factory :device_description do
sequence(:name) { |i| "Device description #{i}" }
sequence(:short_label) { |i| "DD#{i}" }
serial_number { '123abc456def' }
end
end

0 comments on commit fd56b5b

Please sign in to comment.