screen_api.rb 4.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Chemotion
  class ScreenAPI < Grape::API
    include Grape::Kaminari

    resource :screens do

      desc "Return serialized screens"
      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]
15
16
17
18
19
          Collection.belongs_to_or_shared_by(current_user.id).find(params[:collection_id]).screens
        else
          # All collection of current_user
          Screen.joins(:collections).where('collections.user_id = ?', current_user.id).uniq
        end.order("created_at DESC")
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

        paginate(scope)
      end

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

        get do
          Screen.find(params[:id])
        end
      end

Jan-Philipp Willem's avatar
Jan-Philipp Willem committed
38
39
40
41
42
43
44
45
46
      desc "Update screen by id"
      params do
        requires :id, type: Integer, desc: "screen id"
        optional :name, type: String
        optional :collaborator, type: String
        optional :requirements, type: String
        optional :conditions, type: String
        optional :result, type: String
        optional :description, type: String
47
        requires :wellplate_ids, type: Array
Jan-Philipp Willem's avatar
Jan-Philipp Willem committed
48
49
50
51
52
53
54
      end
      route_param :id do
        before do
          error!('401 Unauthorized', 401) unless ElementPolicy.new(@current_user, Screen.find(params[:id])).update?
        end

        put do
55
56
57
58
59
60
61
62
63
64
          attributes = {
              name: params[:name],
              collaborator: params[:collaborator],
              requirements: params[:requirements],
              conditions: params[:conditions],
              result: params[:result],
              description: params[:description]
          }
          screen = Screen.find(params[:id])
          screen.update(attributes)
Jan-Philipp Willem's avatar
Jan-Philipp Willem committed
65

66
67
68
69
70
71
72
          Wellplate.all.each do |wellplate|
            if (params[:wellplate_ids].include? wellplate.id)
              wellplate.update(screen_id: params[:id])
            elsif (params[:id] == wellplate.screen_id)
              # remove wellplate from screen
              wellplate.update(screen_id: nil)
            end
Jan-Philipp Willem's avatar
Jan-Philipp Willem committed
73
          end
74
          screen
Jan-Philipp Willem's avatar
Jan-Philipp Willem committed
75
76
77
        end
      end

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
      desc "Create a screen"
      params do
        requires :name, type: String
        optional :collaborator, type: String
        optional :requirements, type: String
        optional :conditions, type: String
        optional :result, type: String
        optional :description, type: String
        optional :collection_id, type: Integer
        requires :wellplate_ids, type: Array
      end
      post do
        attributes = {
            name: params[:name],
            collaborator: params[:collaborator],
            requirements: params[:requirements],
            conditions: params[:conditions],
            result: params[:result],
            description: params[:description]
        }
        screen = Screen.create(attributes)

        collection = Collection.find(params[:collection_id])
        CollectionsScreen.create(screen: screen, collection: collection)

        Wellplate.all.each do |wellplate|
          if (params[:wellplate_ids].include? wellplate.id)
            wellplate.update(screen_id: screen.id)
          end
        end
        screen
      end

111
112
113
114
115
116
117
118
119
120
121
      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
122
          error!('401 Unauthorized', 401) unless ElementsPolicy.new(@current_user, Screen.for_user(current_user.id).for_ui_state(params[:ui_state])).destroy?
123
124
125
        end

        delete do
126
          Screen.for_user(current_user.id).for_ui_state(params[:ui_state]).destroy_all
127
128
129
        end
      end

130
131
132
    end
  end
end