| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | module Api |
| 4 | module V1 |
| 5 | class SearchController < ApiController |
| 6 | QUERY_LIMIT_AUTOCOMPLETE = 5 |
| 7 | |
| 8 | def index |
| 9 | keyword = params[:q].downcase |
| 10 | client = params[:client].to_sym |
| 11 | render json: { |
| 12 | results: merge_search(keyword, client, QUERY_LIMIT_AUTOCOMPLETE) |
| 13 | } |
| 14 | end |
| 15 | |
| 16 | # Save search history |
| 17 | def post |
| 18 | search_term = params[:term] |
| 19 | logger.info "Search term: #{search_term}" |
| 20 | # Append cound if search term exists |
| 21 | search_history = SearchTerm.find_by(term: search_term) |
| 22 | if search_history |
| 23 | search_history.increment!(:count) |
| 24 | else |
| 25 | SearchTerm.new(term: search_term, count: 1).save |
| 26 | end |
| 27 | |
| 28 | render json: { message: 'Ok.' }, status: :ok |
| 29 | end |
| 30 | |
| 31 | private |
| 32 | |
| 33 | def merge_search(keyword, client, limit) |
| 34 | case client |
| 35 | when :web_beta |
| 36 | merge_search_web_beta(keyword, limit) |
| 37 | when :web_v1, :web_v1_localhost |
| 38 | merge_search_web_v1(keyword, limit) |
| 39 | when :splitfire |
| 40 | merge_search_splitfire(keyword, limit) |
| 41 | else |
| 42 | merge_search_splitfire(keyword, limit) # Remove this after we add client param to splitfire |
| 43 | end |
| 44 | end |
| 45 | |
| 46 | def merge_search_web_beta(keywords, limit) |
| 47 | search_gending_notation(keywords, limit) + search_gending(keywords, limit) + |
| 48 | search_artist(keywords, limit) + search_song(keywords, limit) + search_album(keywords, limit) |
| 49 | end |
| 50 | |
| 51 | def merge_search_web_v1(keywords, limit) |
| 52 | logger.debug "merge_search_web_v1: Searching for #{keywords}" |
| 53 | search_gending_notation(keywords, limit) + search_gending(keywords, limit) |
| 54 | end |
| 55 | |
| 56 | def merge_search_splitfire(keywords, limit) |
| 57 | search_song(keywords, limit) |
| 58 | end |
| 59 | |
| 60 | def search_song(keywords, limit) |
| 61 | SongProvider |
| 62 | .where('lower(name) LIKE ? AND provider_type = 0', "%#{keywords}%") |
| 63 | .limit(limit) |
| 64 | .as_json(only: %i[id name]) |
| 65 | end |
| 66 | |
| 67 | def search_gending_notation(keywords, limit) |
| 68 | JavaneseGendingNotation |
| 69 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 70 | .limit(limit) |
| 71 | .as_json(only: %i[id name]) |
| 72 | end |
| 73 | |
| 74 | def search_artist(keywords, limit) |
| 75 | Artist |
| 76 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 77 | .limit(limit) |
| 78 | .as_json(only: %i[id name]) + |
| 79 | search_artist_provider(keywords, limit) |
| 80 | end |
| 81 | |
| 82 | def search_album(keywords, limit) |
| 83 | Album |
| 84 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 85 | .limit(limit) |
| 86 | .as_json(only: %i[id name]) + |
| 87 | search_album_provider(keywords, limit) |
| 88 | end |
| 89 | |
| 90 | def search_album_provider(keywords, limit) |
| 91 | AlbumProvider |
| 92 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 93 | .limit(limit) |
| 94 | .as_json(only: %i[id name]) |
| 95 | end |
| 96 | |
| 97 | def search_artist_provider(keywords, limit) |
| 98 | ArtistProvider |
| 99 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 100 | .limit(limit) |
| 101 | .as_json(only: %i[id name]) |
| 102 | end |
| 103 | |
| 104 | def search_song_provider(keywords, limit) |
| 105 | SongProvider |
| 106 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 107 | .limit(limit) |
| 108 | .as_json(only: %i[id name]) |
| 109 | end |
| 110 | |
| 111 | def search_gending(keywords, limit) |
| 112 | JavaneseGending |
| 113 | .where('lower(name) LIKE ?', "%#{keywords}%") |
| 114 | .limit(limit) |
| 115 | .as_json(only: %i[id name]) |
| 116 | end |
| 117 | end |
| 118 | end |
| 119 | end |