suggestion_api.rb 4.42 KB
Newer Older
Florian Hübsch's avatar
Florian Hübsch committed
1
2
module Chemotion
  class SuggestionAPI < Grape::API
3
    helpers do
4
      def search_possibilities_to_suggestions(search_possibilities)
5
6
        suggestions = []
        search_possibilities.each do |k,v|
7
          suggestions += v.map {|x| {name: x, search_by_method: k}}
8
9
10
        end
        suggestions
      end
11
12
13
14
15

      def search_possibilities_by_type(type)
        case type
        when 'sample'
          {
Florian Hübsch's avatar
Florian Hübsch committed
16
17
18
            sample_name: Sample.by_name(params[:query]).pluck(:name).uniq,
            sum_formula: Molecule.by_formula(params[:query]).map(&:sum_formular).uniq,
            iupac_name: Molecule.by_iupac_name(params[:query]).map(&:iupac_name).uniq
19
20
21
          }
        when 'reaction'
          {
Florian Hübsch's avatar
Florian Hübsch committed
22
23
24
            reaction_name: Reaction.by_name(params[:query]).pluck(:name).uniq,
            sample_name: Sample.with_reactions.by_name(params[:query]).pluck(:name).uniq,
            iupac_name: Molecule.with_reactions.by_iupac_name(params[:query]).map(&:iupac_name).uniq
25
26
27
          }
        when 'wellplate'
          {
Florian Hübsch's avatar
Florian Hübsch committed
28
29
30
            wellplate_name: Wellplate.by_name(params[:query]).pluck(:name).uniq,
            sample_name: Sample.with_wellplates.by_name(params[:query]).pluck(:name).uniq,
            iupac_name: Molecule.with_wellplates.by_iupac_name(params[:query]).map(&:iupac_name).uniq
31
32
33
          }
        when 'screen'
          {
Florian Hübsch's avatar
Florian Hübsch committed
34
35
36
            screen_name: Screen.by_name(params[:query]).pluck(:name).uniq,
            conditions: Screen.by_conditions(params[:query]).pluck(:conditions).uniq,
            requirements: Screen.by_requirements(params[:query]).pluck(:requirements).uniq
37
38
39
          }
        else
          {
Florian Hübsch's avatar
Florian Hübsch committed
40
41
42
43
44
45
46
47
            sample_name: Sample.by_name(params[:query]).pluck(:name).uniq,
            sum_formula: Molecule.by_formula(params[:query]).map(&:sum_formular).uniq,
            iupac_name: Molecule.by_iupac_name(params[:query]).map(&:iupac_name).uniq,
            reaction_name: Reaction.by_name(params[:query]).pluck(:name).uniq,
            wellplate_name: Wellplate.by_name(params[:query]).pluck(:name).uniq,
            screen_name: Screen.by_name(params[:query]).pluck(:name).uniq,
            conditions: Screen.by_conditions(params[:query]).pluck(:conditions).uniq,
            requirements: Screen.by_requirements(params[:query]).pluck(:requirements).uniq
48
49
50
          }
        end
      end
51
52
    end

Florian Hübsch's avatar
Florian Hübsch committed
53
54
    resource :suggestions do

55
56
57
58
59
60
61
62
63
64
65
66
67
      namespace :all do
        desc 'Return all suggestions for AutoCompleteInput'
        params do
          requires :query, type: String, desc: 'Search query'
        end
        route_param :query do
          get do
            search_possibilities = search_possibilities_by_type('all')
            {suggestions: search_possibilities_to_suggestions(search_possibilities)}
          end
        end
      end

Florian Hübsch's avatar
Florian Hübsch committed
68
      namespace :samples do
69
        desc 'Return sample suggestions for AutoCompleteInput'
Florian Hübsch's avatar
Florian Hübsch committed
70
71
72
73
74
        params do
          requires :query, type: String, desc: 'Search query'
        end
        route_param :query do
          get do
75
76
            search_possibilities = search_possibilities_by_type('sample')
            {suggestions: search_possibilities_to_suggestions(search_possibilities)}
77
78
79
80
81
82
83
84
85
86
87
          end
        end
      end

      namespace :reactions do
        desc 'Return reaction suggestions for AutoCompleteInput'
        params do
          requires :query, type: String, desc: 'Search query'
        end
        route_param :query do
          get do
88
89
            search_possibilities = search_possibilities_by_type('reaction')
            {suggestions: search_possibilities_to_suggestions(search_possibilities)}
90
91
92
93
94
95
96
97
98
99
100
          end
        end
      end

      namespace :wellplates do
        desc 'Return wellplate suggestions for AutoCompleteInput'
        params do
          requires :query, type: String, desc: 'Search query'
        end
        route_param :query do
          get do
101
102
            search_possibilities = search_possibilities_by_type('wellplate')
            {suggestions: search_possibilities_to_suggestions(search_possibilities)}
103
104
105
106
107
108
109
110
111
112
113
          end
        end
      end

      namespace :screens do
        desc 'Return screen suggestions for AutoCompleteInput'
        params do
          requires :query, type: String, desc: 'Search query'
        end
        route_param :query do
          get do
114
115
            search_possibilities = search_possibilities_by_type('screen')
            {suggestions: search_possibilities_to_suggestions(search_possibilities)}
Florian Hübsch's avatar
Florian Hübsch committed
116
117
118
119
120
121
122
          end
        end
      end

    end
  end
end