lib/beerdb/server.rb in beerdb-0.7.2 vs lib/beerdb/server.rb in beerdb-0.8.0
- old
+ new
@@ -57,9 +57,68 @@
get '/' do
erb :index
end
+ get '/drinks/:key' do |key|
+
+ puts " handle GET /drinks/:key"
+
+ if ['l', 'latest'].include?( key )
+ # get latest drinks (w/ ratings)
+ drinks = Drink.order( 'updated_at DESC' ).limit(10).all
+ elsif ['t', 'top'].include?( key )
+ drinks = Drink.order( 'rating DESC, updated_at DESC' ).limit(10).all
+ else
+ # assume it's a user key
+ user = User.find_by_key!( key )
+ drinks = Drink.order( 'rating DESC, updated_at DESC' ).where( user_id: user.id ).all
+ end
+
+ data = []
+ drinks.each do |drink|
+ data << {
+ beer: { title: drink.beer.title,
+ key: drink.beer.key },
+ rating: drink.rating,
+ user: { name: drink.user.name,
+ key: drink.user.key },
+ comments: drink.comments,
+ place: drink.place,
+ created_at: drink.created_at,
+ updated_at: drink.updated_at
+ }
+ end
+
+ json_or_jsonp( data.to_json )
+ end
+
+
+ get '/drinks' do
+ if params[:method] == 'post'
+
+ puts " handle GET /drinks?method=post"
+
+ user = User.find_by_key!( params[:user] )
+ beer = Beer.find_by_key!( params[:beer] )
+ rating = params[:rating].to_i
+ place = params[:place] # assumes for now a string or nil / pass through as is
+
+ attribs = {
+ user_id: user.id,
+ beer_id: beer.id,
+ rating: rating,
+ place: place
+ }
+
+ drink = Drink.new
+ drink.update_attributes!( attribs )
+ end
+
+ json_or_jsonp( { status: 'ok' }.to_json )
+ end
+
+
get '/d*' do
erb :debug
end
## todo: add support for beer of the day, of the week, of the month, of the year?