Created
January 6, 2017 13:06
-
-
Save phensalves/62f3e1c6bf16304b893e938b34cfe5ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list | |
## FILTER | |
@bars = Bar.where("latitude IS NOT NULL").where("longitude IS NOT NULL") | |
@bars = @bars.running_in(true) | |
if params[:search_term] | |
search_name = @bars.like_name(params[:search_term]) | |
search_district = @bars.district_by_name(params[:search_term]) | |
joined = search_name + search_district | |
joined_ids = joined.uniq.map{|bar| bar.id} | |
@bars = Bar.where(id:joined_ids) | |
end | |
@bars = @bars.where_city(params[:city_id]) if params[:city_id].present? | |
@bars = @bars.district(params[:bairro]) if params[:bairro].present? | |
@bars = @bars.parking(params[:parking]) if params[:parking].present? | |
@bars = @bars.booking(params[:booking]) if params[:booking].present? | |
@bars = @bars.credit(params[:credit]) if params[:credit].present? | |
@bars = @bars.happyhour(params[:happyhour]) if params[:happyhour].present? | |
@bars = @bars.wifi(params[:wifi]) if params[:wifi].present? | |
if params[:bar_id].present? | |
bar_array = params[:bar_id].split(',') | |
binding.pry | |
@bars = @bars.where(id:bar_array) | |
end | |
if params[:beers].present? | |
beer_array = params[:beers].split(',') | |
array = @bars.reject{|r| (beer_array - r.beers_id) == beer_array} | |
bar_ids = array.map{|bar| bar.id} | |
@bars = Bar.where(id:bar_ids) | |
end | |
## ORDER | |
me = [params[:lat],params[:lng]] | |
me = [params[:centerlatitude],params[:centerlongitude]] if me.include?(nil) | |
case params[:ordertype] | |
when 'rate' | |
@bars = @bars.order("star_rating ASC") if params[:orderlevel] == 'lower' | |
@bars = @bars.order("star_rating DESC") if params[:orderlevel] == 'upper' | |
when 'distance' | |
if !me.include?(nil) | |
@bars = @bars.by_distance(origin: me) if params[:orderlevel] != 'upper' | |
@bars = @bars.by_distance({:reverse => true}.merge(:origin => me)) if params[:orderlevel] == 'upper' | |
end | |
when 'price' | |
@bars = @bars.order('price_rating ASC') if params[:orderlevel] == 'lower' | |
@bars = @bars.order('price_rating DESC') if params[:orderlevel] == 'upper' | |
end | |
@bars = @bars.page(params[:page]).per(5) | |
resp_json = [] | |
@bars.each do |bar| | |
begin | |
photos = bar.bar_photos.map {|photo| photo.image.url} | |
deals = Deal.bar_eq(bar).map {|item| {id:item.id, name:item.name, empty:item.is_empty?, expired:item.is_expired?}} | |
deal_weekday = bar.deal_weekdays.pluck(:id) | |
coord_bar = [bar.latitude,bar.longitude] | |
distance = calculate_distance(coord_bar , me) | |
my_hash = Hash.new | |
my_hash = { | |
id: bar.id, | |
distance: distance, | |
price: bar.price_rating, | |
rate: bar.star_rating, | |
photo: photos, | |
name: bar.name, | |
bairro: bar.district.name, | |
telefone: bar.phone, | |
address: bar.address, | |
short_description: bar.short_description, | |
latitude: bar.latitude, | |
longitude: bar.longitude, | |
deals: deals, | |
deal_weekdays: deal_weekday | |
} | |
rescue | |
puts "ERROR BUILDING HASH FOR BAR #{bar.id}" | |
end | |
resp_json << my_hash | |
end | |
respond_to do |format| | |
format.json { render :json => resp_json} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment