Class: Api::V2::PickListResource

Inherits:
BaseResource
  • Object
show all
Defined in:
app/resources/api/v2/pick_list_resource.rb

Overview

Provides a JSON API representation of PickList See: jsonapi-resources.com/ for JSONAPI::Resource documentation

Constant Summary collapse

PERMITTED_PICK_ATTRIBUTES =

Constants…

%i[source_receptacle_id study_id project_id].freeze
PERMITTED_LABWARE_PICK_ATTRIBUTES =
%i[source_labware_id source_labware_barcode study_id project_id].freeze

Instance Method Summary collapse

Methods inherited from BaseResource

apply_includes, default_includes, #fetchable_fields, inclusions, resolve_relationship_names_to_relations, updatable_fields

Instance Method Details

#labware_pick_attributes=(labware_picks) ⇒ Object

This provides an alternative API for passing in a list of labware, either by ids or barcodes. This avoids the need to make additional requests for the receptacle ids. We keep this as a separate accessor to avoid the confusion of passing in a list of 12 picks, and receiving more back.


54
55
56
57
58
59
60
61
62
63
64
# File 'app/resources/api/v2/pick_list_resource.rb', line 54

def labware_pick_attributes=(labware_picks)
  # Extract and look up records here before passing through
  cache = PickList::RecordCache::ByLabware.new(labware_picks)
  @model.pick_attributes = labware_picks.flat_map do |pick|
    cache.convert(pick.permit(PERMITTED_LABWARE_PICK_ATTRIBUTES))
  end
rescue KeyError => e
  # We'll see this if one of the attributes passed in doesn't match an actual record,
  # such as a non-existent study id.
  raise JSONAPI::Exceptions::BadRequest, e.message
end

#pick_attributesObject


66
67
68
69
70
71
72
73
74
# File 'app/resources/api/v2/pick_list_resource.rb', line 66

def pick_attributes
  @model.pick_attributes.map do |pick|
    {
      source_receptacle_id: pick[:source_receptacle].id,
      study_id: pick[:study]&.id,
      project_id: pick[:project]&.id
    }
  end
end

#pick_attributes=(picks) ⇒ Object

JSON API v1.0 doesn't have native support for creating nested resources in a single request. In addition, as picks are not backed by real database records yet we could expect to run into issues anyway. So we just expose our pick attributes. However, as we can't expect to receive actual receptacles/studies/projects over the API, we convert them from the ids. The RecordCache allows us to do this with a single database query.


40
41
42
43
44
45
46
47
48
# File 'app/resources/api/v2/pick_list_resource.rb', line 40

def pick_attributes=(picks)
  # Extract and look up records here before passing through
  cache = PickList::RecordCache::ByReceptacle.new(picks)
  @model.pick_attributes = picks.map { |pick| cache.convert(pick.permit(PERMITTED_PICK_ATTRIBUTES)) }
rescue KeyError => e
  # We'll see this if one of the attributes passed in doesn't match an actual record,
  # such as a non-existent study id.
  raise JSONAPI::Exceptions::BadRequest, e.message
end