wellplate_api.rb 5.46 KB
Newer Older
1
2
3
4
5
module Chemotion
  class WellplateAPI < Grape::API
    include Grape::Kaminari

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

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

        delete do
21
          Wellplate.for_user(current_user.id).for_ui_state(params[:ui_state]).destroy_all
22
23
        end
      end
24
25
26
27
28
29
30
31
32
33
34

      desc "Return serialized wellplates"
      params do
        optional :collection_id, type: Integer, desc: "Collection id"
      end
      paginate per_page: 5, max_per_page: 25, offset: 0

      get do
        scope = if params[:collection_id]
          Collection.belongs_to_or_shared_by(current_user.id).find(params[:collection_id]).wellplates
        else
35
          # All collection of current_user
36
          Wellplate.joins(:collections).where('collections.user_id = ?', current_user.id).uniq
37
38
39
40
41
42
43
44
45
46
        end.order("created_at DESC")

        paginate(scope)
      end

      desc "Return serialized wellplate by id"
      params do
        requires :id, type: Integer, desc: "Wellplate id"
      end
      route_param :id do
47
48
49
50
        before do
          error!('401 Unauthorized', 401) unless ElementPolicy.new(@current_user, Wellplate.find(params[:id])).read?
        end

51
52
53
54
55
        get do
          Wellplate.find(params[:id])
        end
      end

56
57
58
59
      desc "Delete a wellplate by id"
      params do
        requires :id, type: Integer, desc: "Wellplate id"
      end
60
      route_param :id do
61
62
63
64
        before do
          error!('401 Unauthorized', 401) unless ElementPolicy.new(@current_user, Wellplate.find(params[:id])).destroy?
        end

65
66
67
68
69
        delete do
          Wellplate.find(params[:id]).destroy
        end
      end

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
      desc "Update wellplate by id"
      params do
        requires :id, type: Integer
        optional :name, type: String
        optional :size, type: Integer
        optional :description, type: String
        optional :wells, type: Array
        optional :collection_id, type: Integer
      end
      route_param :id do
        before do
          error!('401 Unauthorized', 401) unless ElementPolicy.new(@current_user, Wellplate.find(params[:id])).update?
        end

        put do
          attributes = {
              name: params[:name],
              size: params[:size],
              description: params[:description]
          }

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
          ActiveRecord::Base.transaction do
            wellplate = Wellplate.find(params[:id])
            wellplate.update(attributes)

            params[:wells].each do |well|
              sample = well.sample
              sample_id = (sample) ? sample.id : nil
              if well.id
                Well.find(well.id).update(
                    sample_id: sample_id,
                    readout: well.readout,
                    additive: well.additive,
                    position_x: well.position.x,
                    position_y: well.position.y,
                )
              else
                Well.create(
                  wellplate_id: wellplate.id,
                  sample_id: sample_id,
                  readout: well.readout,
                  additive: well.additive,
                  position_x: well.position.x,
                  position_y: well.position.y,
                )
              end
            end
            wellplate
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
          end
        end
      end

      desc "Create a wellplate"
      params do
        requires :name, type: String
        optional :size, type: Integer
        optional :description, type: String
        optional :wells, type: Array
        optional :collection_id, type: Integer
      end
      post do
        attributes = {
            name: params[:name],
            size: params[:size],
            description: params[:description]
        }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

        ActiveRecord::Base.transaction do
          wellplate = Wellplate.create(attributes)
          wellplate.reload
          collection = Collection.find(params[:collection_id])
          CollectionsWellplate.create(wellplate: wellplate, collection: collection)

          params[:wells].each do |well|
            sample = well.sample
            sample_id = (sample) ? sample.id : nil
            Well.create(
                sample_id: sample_id,
                readout: well.readout,
                additive: well.additive,
                position_x: well.position.x,
                position_y: well.position.y,
                wellplate_id: wellplate.id
            )
          end
          wellplate
156
157
158
159
160
161
162
163
164
165
166
167
168
169
        end
      end

      namespace :ui_state do
        desc "Delete screens by UI state"
        params do
          requires :ui_state, type: Hash, desc: "Selected screens from the UI" do
            optional :all, type: Boolean
            optional :included_ids, type: Array
            optional :excluded_ids, type: Array
          end
        end

        before do
170
          error!('401 Unauthorized', 401) unless ElementsPolicy.new(@current_user, Screen.for_user(current_user.id).for_ui_state(params[:ui_state])).destroy?
171
172
173
        end

        delete do
174
          Screen.for_user(current_user.id).for_ui_state(params[:ui_state]).destroy_all
175
176
177
        end
      end

178
179
180
    end
  end
end