sample_api.rb 6.03 KB
Newer Older
1
2
module Chemotion
  class SampleAPI < Grape::API
Marco Sehrer's avatar
Marco Sehrer committed
3
4
    include Grape::Kaminari

5
    resource :samples do
6

7
8
9
10
11
12
13
14
15
16
17
      namespace :ui_state do
        desc "Delete samples by UI state"
        params do
          requires :ui_state, type: Hash, desc: "Selected samples from the UI" do
            optional :all, type: Boolean
            optional :included_ids, type: Array
            optional :excluded_ids, type: Array
          end
        end

        before do
18
          error!('401 Unauthorized', 401) unless ElementsPolicy.new(@current_user, Sample.for_user(current_user.id).for_ui_state(params[:ui_state])).destroy?
19
20
21
        end

        delete do
22
          Sample.for_user(current_user.id).for_ui_state(params[:ui_state]).destroy_all
23
24
25
        end
      end

26
27
28
29
30
31
32
33
      namespace :subsamples do
        desc "Split Samples into Subsamples"
        params do
          requires :ui_state, type: Hash, desc: "Selected samples from the UI"
        end
        post do
          ui_state = params[:ui_state]
          currentCollectionId = ui_state[:currentCollectionId]
34
          sample_ids = Sample.for_user(current_user.id).for_ui_state_with_collection(ui_state[:sample], CollectionsSample, currentCollectionId)
35
          Sample.where(id: sample_ids).each do |sample|
36
            #todo: extract method into Sample
37
38
            subsample = sample.dup
            subsample.parent = sample
39
40
41
42
43
44
            subsample.save
            CollectionsSample.create(collection_id: currentCollectionId, sample_id: subsample.id)
          end
        end
      end

45
      desc "Return serialized samples of current user"
Marco Sehrer's avatar
Marco Sehrer committed
46
47
48
      params do
        optional :collection_id, type: Integer, desc: "Collection id"
      end
49
      paginate per_page: 7, max_per_page: 25, offset: 0
Marco Sehrer's avatar
Marco Sehrer committed
50

Marco Sehrer's avatar
Marco Sehrer committed
51
      get do
Marco Sehrer's avatar
Marco Sehrer committed
52
        scope = if params[:collection_id]
53
          Collection.belongs_to_or_shared_by(current_user.id).find(params[:collection_id]).samples.includes(:molecule)
Marco Sehrer's avatar
Marco Sehrer committed
54
        else
55
          # All collection
56
          Sample.for_user(current_user.id).includes(:molecule).uniq
57
58
        end.order("created_at DESC")

Marco Sehrer's avatar
Marco Sehrer committed
59
        paginate(scope)
Marco Sehrer's avatar
Marco Sehrer committed
60
61
      end

62
63
64
65
66
      desc "Return serialized sample by id"
      params do
        requires :id, type: Integer, desc: "Sample id"
      end
      route_param :id do
67
68
69
70
        before do
          error!('401 Unauthorized', 401) unless ElementPolicy.new(@current_user, Sample.find(params[:id])).read?
        end

71
        get do
72
          #Sample.find(params[:id])
Florian Hübsch's avatar
Florian Hübsch committed
73
          SampleProxy.new(current_user).find(params[:id])
74
75
        end
      end
76
77
78
79

      desc "Update sample by id"
      params do
        requires :id, type: Integer, desc: "Sample id"
80
        optional :name, type: String, desc: "Sample name"
81
        optional :external_label, type: String, desc: "Sample external label"
82
83
84
85
86
87
88
        optional :amount_value, type: Float, desc: "Sample amount_value"
        optional :amount_unit, type: String, desc: "Sample amount_unit"
        optional :description, type: String, desc: "Sample description"
        optional :purity, type: Float, desc: "Sample purity"
        optional :solvent, type: String, desc: "Sample solvent"
        optional :impurities, type: String, desc: "Sample impurities"
        optional :location, type: String, desc: "Sample location"
89
        optional :molfile, type: String, desc: "Sample molfile"
90
91
        #optional :molecule, type: Hash, desc: "Sample molecule"
        optional :is_top_secret, type: Boolean, desc: "Sample is marked as top secret?"
92
      end
93
      route_param :id do
94
95
96
        before do
          error!('401 Unauthorized', 401) unless ElementPolicy.new(@current_user, Sample.find(params[:id])).update?
        end
97

98
        put do
99
          attributes = declared(params, include_missing: false)
100

101
102
103
          attributes.merge!(
            molecule_attributes: params[:molecule]
          ) unless params[:molecule].blank?
104

105
106
107
108
          if sample = Sample.find(params[:id])
            sample.update(attributes)
            sample
          end
109
        end
110
      end
Marco Sehrer's avatar
Marco Sehrer committed
111

Fernando D'Agostino's avatar
Fernando D'Agostino committed
112
113
114
      desc "Create a sample"
      params do
        requires :name, type: String, desc: "Sample name"
115
        optional :external_label, type: String, desc: "Sample external label"
Fernando D'Agostino's avatar
Fernando D'Agostino committed
116
117
118
119
120
121
122
123
124
        requires :amount_value, type: Float, desc: "Sample amount_value"
        requires :amount_unit, type: String, desc: "Sample amount_unit"
        requires :description, type: String, desc: "Sample description"
        requires :purity, type: Float, desc: "Sample purity"
        requires :solvent, type: String, desc: "Sample solvent"
        requires :impurities, type: String, desc: "Sample impurities"
        requires :location, type: String, desc: "Sample location"
        optional :molfile, type: String, desc: "Sample molfile"
        optional :molecule, type: Hash, desc: "Sample molecule"
125
        optional :collection_id, type: Integer, desc: "Collection id"
126
        requires :is_top_secret, type: Boolean, desc: "Sample is marked as top secret?"
Fernando D'Agostino's avatar
Fernando D'Agostino committed
127
128
129
130
131
132
133
134
135
136
137
      end
      post do
        attributes = {
          name: params[:name],
          amount_value: params[:amount_value],
          amount_unit: params[:amount_unit],
          description: params[:description],
          purity: params[:purity],
          solvent: params[:solvent],
          impurities: params[:impurities],
          location: params[:location],
138
139
          molfile: params[:molfile],
          is_top_secret: params[:is_top_secret]
Fernando D'Agostino's avatar
Fernando D'Agostino committed
140
141
142
143
        }
        attributes.merge!(
          molecule_attributes: params[:molecule]
        ) unless params[:molecule].blank?
144
145
146
        sample = Sample.create(attributes)
        if collection_id = params[:collection_id]
          collection = Collection.find(collection_id)
147
          CollectionsSample.create(sample: sample, collection: collection)
148
149
        end
        sample
Fernando D'Agostino's avatar
Fernando D'Agostino committed
150
      end
151
152
153
154
155

      desc "Delete a sample by id"
      params do
        requires :id, type: Integer, desc: "Sample id"
      end
156
      route_param :id do
157
158
159
160
        before do
          error!('401 Unauthorized', 401) unless ElementPolicy.new(@current_user, Sample.find(params[:id])).destroy?
        end

161
162
163
164
165
        delete do
          Sample.find(params[:id]).destroy
        end
      end

166
167
168
    end
  end
end