From 6cdad2b67aa6293f673aac660efb4dd62c154408 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 16:52:41 -0600 Subject: [PATCH 1/4] merging --- lib/repos/DB.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index 50cee362..95207285 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -7,17 +7,17 @@ def self.get_shops(db) def self.get_cats(db, shop_id) mappings = {"shop_id" => "shopId", "cat_id" => "id", "name" => "name", "imageurl" => "imageUrl", "adoption_status" => "adopted"} - response = db.exec("SELECT * FROM cats WHERE shop_id = $1", [shop_id]).entries - cats = [] - response.each do |cat| + response = db.exec("SELECT * FROM cats WHERE shop_id = $1", [shop_id]).entries + cats = [] + response.each do |cat| if cat['adoption_status'] == "t" cat['adoption_status'] = "true" else cat['adoption_status'] = "false" end cats << Hash[cat.map {|k, v| [mappings[k], v] }] - end - cats + end + cats end From a05460266eeb24be9beff3102bcbea0f5ca54976 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Fri, 12 Dec 2014 10:42:10 -0600 Subject: [PATCH 2/4] merge prep --- lib/repos/DB.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index dd9c5b80..7daad2ed 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -11,9 +11,9 @@ def self.get_cats(db, shop_id) cats = [] response.each do |cat| if cat['adoption_status'] == "t" - cat['adoption_status'] = "true" + cat['adoption_status'] = true else - cat['adoption_status'] = "false" + cat['adoption_status'] = false end cats << Hash[cat.map {|k, v| [mappings[k], v] }] end @@ -26,9 +26,9 @@ def self.get_dogs(db, shop_id) dogs = [] response.each do |dog| if dog['adoption_status'] == "t" - dog['adoption_status'] = "true" + dog['adoption_status'] = true else - dog['adoption_status'] = "false" + dog['adoption_status'] = false end dogs << Hash[dog.map {|k, v| [mappings[k], v] }] end From 1ae89e6084eb6fe74552cf06fdc8a3774a7c777f Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Fri, 12 Dec 2014 11:55:22 -0600 Subject: [PATCH 3/4] merge prep --- server.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/server.rb b/server.rb index 21948e00..041dffef 100644 --- a/server.rb +++ b/server.rb @@ -10,6 +10,21 @@ # # # This is our only html view... # +configure do + set :bind, '0.0.0.0' + enable :sessions +end + +before do + if session['user_id'] + user_id = session['user_id'] + db = PetShop::Database.dbconnect + @current_user = PetShop::DB.find db, user_id + else + @current_user = {'username' => 'anonymous', 'id' => 1} + end +end + get '/' do if session[:user_id] # TODO: Grab user from database @@ -40,7 +55,6 @@ # user = { 'username' => 'alice', 'password' => '123' } db = PetShop::Database.dbconnect user = PetShop::DB.find_by_name(db, username) - if password == user['password'] headers['Content-Type'] = 'application/json' From 101997ab45f8379e63e973bd89d0c37d0e307d3c Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Fri, 12 Dec 2014 12:29:23 -0600 Subject: [PATCH 4/4] Adopted cats and dogs can be added to user --- lib/repos/DB.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++--- server.rb | 9 ++++++--- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index 0e1ad4a6..e2dfc88b 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -45,10 +45,28 @@ def self.adopt_dog(db, dog_id) def self.find db, user_id sql = %q[SELECT * FROM users WHERE id = $1] - result = db.exec(sql, [user_id]) - result.first - end + result = db.exec(sql, [user_id]).first + + dog_sql = %q[select dogs.shop_id, dogs.dog_id, dogs.name, dogs.imageurl, dogs.happiness, dogs.adoption_status + from dogs + INNER JOIN adoptions on dogs.dog_id=adoptions.dog_id where user_id = $1; + ] + cat_sql = %q[select cats.shop_id, cats.cat_id, cats.name, cats.imageurl, cats.adoption_status + from cats + INNER JOIN adoptions on cats.cat_id=adoptions.cat_id where user_id = $1; + ] + dogs = db.exec(dog_sql, [user_id]) + dogs = dogs.entries + result['dogs'] = map_dogs(dogs) + + + cats = db.exec(cat_sql, [user_id]) + cats = cats.entries + result['cats'] = map_cats(cats) + + p result + end # find user by username. Intended to be used when # someone tries to sign in. def self.find_by_name db, username @@ -57,6 +75,34 @@ def self.find_by_name db, username result.first end + def self.map_cats(cats) + mappings = {"shop_id" => "shopId", "cat_id" => "id", "name" => "name", "imageurl" => "imageUrl", "adoption_status" => "adopted"} + mapped_cats = [] + cats.each do |cat| + if cat['adoption_status'] == "t" + cat['adoption_status'] = true + else + cat['adoption_status'] = false + end + mapped_cats << Hash[cat.map {|k, v| [mappings[k], v] }] + end + mapped_cats + end + + def self.map_dogs(dogs) + mappings = {"shop_id" => "shopId", "dog_id" => "id", "name" => "name", "imageurl" => "imageUrl", "adoption_status" => "adopted"} + mapped_dogs = [] + dogs.each do |dog| + if dog['adoption_status'] == "t" + dog['adoption_status'] = true + else + dog['adoption_status'] = false + end + mapped_dogs << Hash[dog.map {|k, v| [mappings[k], v] }] + end + mapped_dogs + end + end #end DB end#end module diff --git a/server.rb b/server.rb index 041dffef..8aa6d7fa 100644 --- a/server.rb +++ b/server.rb @@ -20,8 +20,6 @@ user_id = session['user_id'] db = PetShop::Database.dbconnect @current_user = PetShop::DB.find db, user_id - else - @current_user = {'username' => 'anonymous', 'id' => 1} end end @@ -94,6 +92,11 @@ # TODO (after you create users table): Attach new cat to logged in user end +get '/logout' do + session.delete('user_id') + redirect to '/' +end + # # # # # Dogs # @@ -131,4 +134,4 @@ dogs: [ { shopId: 1, name: "Leaf Pup", imageUrl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted: "true" } ] -} +} \ No newline at end of file