1
0
Fork 0
oldhaven/app/controllers/artist_controller.rb

54 lines
1.6 KiB
Ruby

class ArtistController < ApplicationController
def data
# Bad params
if params[:artist].nil? or params[:artist].empty?
return render json: { status: 'loading_failed' }, status: 404
end
# Searching for artist
artist_name = get_artist_name_from_query
@artist = Artist.find_by_name(artist_name)
# Artist not found in DB
unless @artist
results = MusicBrainz::Artist.search(artist_name)
if results.empty?
return render json: { status: 'not_found' }
end
best_match = results[0][:sort_name]
if best_match != artist_name and similar_names(artist_name, best_match)
return render json: { status: 'corrected', correct_name: best_match }
elsif best_match == artist_name
queue_loading(artist_name, results[0][:mbid])
else
@suggestions = results.take(5)
return render json: { status: 'suggestions', html: render_compact_partial(:suggestions) }
end
end
compile_page(
data: @artist,
partial: "artist/page",
title: @artist.name,
status: @artist.status_str,
callback: {object: :player, action: :updateLibrary},
cache_for: (1.day if @artist.status_str == "ok")
)
end
def search
redirect_to "/#/artist/#{params[:q].gsub(/\s/, '+')}"
end
private
def queue_loading artist_name, mbid
@artist = Artist.create( name: artist_name, mbid: mbid, status: 0 )
Delayed::Job.enqueue(LoadArtistJob.new(artist_name))
end
def similar_names name1, name2
(name2.downcase == name1.downcase or name2.downcase == 'the '+ name1.downcase)
end
end