From 95b61bc444f71139cb3367bc56aa6b2286add2e0 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 14:04:23 -0600 Subject: [PATCH 01/20] initial commit --- Gemfile | 1 + Gemfile.lock | 10 ++++++++-- server.rb | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 20e422c9..66526d43 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ ruby '2.0.0' gem 'rspec', '~> 2.14.1' gem 'sinatra', '~> 1.4.5' gem 'sinatra-contrib', '~> 1.4.2' +gem 'rest-client' # Testing gem 'pry-byebug' diff --git a/Gemfile.lock b/Gemfile.lock index 92b9a02a..39e03203 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,7 +11,9 @@ GEM debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) + mime-types (2.4.3) multi_json (1.10.1) + netrc (0.10.0) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -24,6 +26,9 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) + rest-client (1.7.2) + mime-types (>= 1.16, < 3.0) + netrc (~> 0.7) rspec (2.14.1) rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) @@ -51,6 +56,7 @@ PLATFORMS DEPENDENCIES pry-byebug + rest-client rspec (~> 2.14.1) - sinatra - sinatra-contrib + sinatra (~> 1.4.5) + sinatra-contrib (~> 1.4.2) diff --git a/server.rb b/server.rb index 9ae90b34..70433179 100644 --- a/server.rb +++ b/server.rb @@ -2,6 +2,7 @@ require 'sinatra/reloader' require 'rest-client' +set :bind, '0.0.0.0' # # # This is our only html view... # From c5c311d3c7596d4ff762651947ae0b635f0ed5fd Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Thu, 11 Dec 2014 14:06:00 -0600 Subject: [PATCH 02/20] gemfile --- Gemfile | 2 +- Gemfile.lock | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 20e422c9..4324772d 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,6 @@ ruby '2.0.0' gem 'rspec', '~> 2.14.1' gem 'sinatra', '~> 1.4.5' gem 'sinatra-contrib', '~> 1.4.2' - +gem 'rest-client' # Testing gem 'pry-byebug' diff --git a/Gemfile.lock b/Gemfile.lock index 92b9a02a..39e03203 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,7 +11,9 @@ GEM debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) + mime-types (2.4.3) multi_json (1.10.1) + netrc (0.10.0) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -24,6 +26,9 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) + rest-client (1.7.2) + mime-types (>= 1.16, < 3.0) + netrc (~> 0.7) rspec (2.14.1) rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) @@ -51,6 +56,7 @@ PLATFORMS DEPENDENCIES pry-byebug + rest-client rspec (~> 2.14.1) - sinatra - sinatra-contrib + sinatra (~> 1.4.5) + sinatra-contrib (~> 1.4.2) From 4e9c981f89bdc7572219da4beb5bfeaea4dcd40a Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 14:08:04 -0600 Subject: [PATCH 03/20] initial commit --- Gemfile.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0bfa104b..0c111e30 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,15 +11,15 @@ GEM debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) -<<<<<<< HEAD + mime-types (2.4.3) multi_json (1.10.1) netrc (0.10.0) -======= + mime-types (1.25.1) multi_json (1.10.1) netrc (0.8.0) ->>>>>>> 82e9a3d74cc1a7f9e00c05553481111f3e8a119a + pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) From de13cb3921bf3918532f46d075302d0e97d46ecc Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Thu, 11 Dec 2014 14:33:46 -0600 Subject: [PATCH 04/20] add database repo --- lib/PetDatabaseRepo.rb | 196 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 lib/PetDatabaseRepo.rb diff --git a/lib/PetDatabaseRepo.rb b/lib/PetDatabaseRepo.rb new file mode 100644 index 00000000..1d150032 --- /dev/null +++ b/lib/PetDatabaseRepo.rb @@ -0,0 +1,196 @@ +require 'pg' +require 'rest-client' +require 'json' + +# Table "public.shops" +# Column | Type | Modifiers +# --------+-------------------+----------- +# id | numeric | +# name | character varying | + + +# Table "public.dogs" +# Column | Type | Modifiers +# -----------------+-------------------+----------- +# shop_id | integer | +# dog_id | integer | +# name | character varying | +# imageurl | character varying | +# happiness | integer | +# adoption_status | boolean | + + +# Table "public.cats" +# Column | Type | Modifiers +# -----------------+-------------------+----------- +# shop_id | integer | +# cat_id | integer | +# name | character varying | +# imageurl | character varying | +# adoption_status | boolean | + +module PetShop +class Database + def initialize + @db = PG.connect(host: 'localhost', dbname:'petshop_db') + end + + def addData + url = 'pet-shop.api.mks.io/shops/' + tempshops = RestClient.get(url) + shops = JSON.parse(tempshops) #this is our hash of shop names and id's! + shops.each { |shop| + shopname = shop["name"] + shopid = shop["id"] + sql = %q[ + INSERT INTO shops (name, id) + VALUES ($1, $2) + ] + @db.exec_params(sql, [shopname, shopid]) #enter shop info to shops table + + url = "pet-shop.api.mks.io/shops/" + shopid.to_s + "/dogs/" + tempdogs = RestClient.get(url) + dogs = JSON.parse(tempdogs) + dogs.each { |dog| #each dog + shop = dog["shopId"] + name = dog["name"] + happiness = dog["happiness"] + image = dog["imageUrl"] + id = dog["id"] + adopt = dog["adopted"] + sql = %q[ + INSERT INTO dogs (shop_id, dog_id, name, imageurl, happiness, adoption_status) + VALUES ($1, $2, $3, $4, $5, $6) + ] + @db.exec_params(sql, [shop, id, name, image, happiness, adopt]) + }#end each dog + + url = "pet-shop.api.mks.io/shops/" + shopid.to_s + "/cats/" + tempcats = RestClient.get(url) + cats = JSON.parse(tempcats) + cats.each { |cat| #each cat + shop = cat["shopId"] + name = cat["name"] + image = cat["imageUrl"] + id = cat["id"] + adopt = cat["adopted"] + sql = %q[ + INSERT INTO cats (shop_id, cat_id, name, imageurl, adoption_status) + VALUES ($1, $2, $3, $4, $5) + ] + @db.exec_params(sql, [shop, id, name, image, adopt]) + }#end each cat + }#end each shop + end + + def print_shops + sql = %q[ + SELECT * FROM shops] + success = @db.exec(sql) + shops = success.entries #hashes of shop info + puts "ID | Name" + puts "-----------------------------" + shops.each { |shop| + if shop["id"].to_i < 10 + puts "#{shop["id"].to_i} | #{shop["name"]}" + else + puts "#{shop["id"].to_i} | #{shop["name"]}" + end + } + end + + + def print_store_dogs(store_id) + sql1 = %q[ + SELECT name FROM shops WHERE id = $1 + ] + success1 = @db.exec_params(sql1, [store_id]) + name = success1.entries[0]["name"] + + sql2 = %q[ + SELECT * FROM dogs WHERE shop_id = $1 + ] + success = @db.exec_params(sql2, [store_id]) + dogs = success.entries #hashes of dog info + puts "Dogs in store #{name}:" + puts "------------------" + dogs.each {|dog| + puts "Name: #{dog["name"]}" + puts "Happiness: #{dog["happiness"]}" + if dog["adopted"] + puts "Adopted: Yes!" + else + puts "Adopted: No" + end + puts "------------------" + } + end + + def print_store_dogs(store_id) + sql1 = %q[ + SELECT name FROM shops WHERE id = $1 + ] + success1 = @db.exec_params(sql1, [store_id]) + name = success1.entries[0]["name"] + + sql2 = %q[ + SELECT * FROM dogs WHERE shop_id = $1 + ] + success = @db.exec_params(sql2, [store_id]) + dogs = success.entries #hashes of dog info + puts "Dogs in store #{name}:" + puts "------------------" + dogs.each {|dog| + puts "Name: #{dog["name"]}" + puts "Happiness: #{dog["happiness"]}" + if dog["adopted"] + puts "Adopted: Yes!" + else + puts "Adopted: No" + end + puts "------------------" + } + end + + def happiestdogs + sql = %q[ + SELECT name, happiness FROM dogs ORDER BY happiness + ] + alldogs = @db.exec(sql) + happiest = alldogs.entries[-5..-1] + puts "Happiest Dogs:" + happiest.each{|dog| + puts "#{dog["name"]}: #{dog["happiness"]}" + } + end + + def allpets + sqldogs = %q[ + SELECT dogs.name AS name, dogs.happiness AS happiness, shops.name AS shop + FROM dogs + JOIN shops + ON dogs.shop_id = shops.id + ] + tempdogs = @db.exec(sqldogs) + dogs = tempdogs.entries + puts "All Pets:" + dogs.each {|dog| + puts "#{dog["name"]}, #{dog["happiness"]}, @ #{dog["shop"]}" + } + sqlcats = %q[ + SELECT cats.name AS name, shops.name AS shop + FROM cats + JOIN shops + ON cats.shop_id = shops.id + ] + tempcats = @db.exec(sqlcats) + cats = tempcats.entries + cats.each {|cat| + puts "#{cat["name"]} @ #{cat["shop"]}" + } +end + +end #end of class + + + From 0c14e098f20cb138b431afb954524c778f869213 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 14:36:57 -0600 Subject: [PATCH 05/20] Preparing to merge --- Gemfile.lock | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 0c111e30..78cd3d34 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,15 +11,11 @@ GEM debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) - + mime-types (1.25.1) mime-types (2.4.3) multi_json (1.10.1) netrc (0.10.0) - - mime-types (1.25.1) - multi_json (1.10.1) netrc (0.8.0) - pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) From a71f0c7a93e67cb86deaf2a20bfa5190c880c82b Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Thu, 11 Dec 2014 15:32:06 -0600 Subject: [PATCH 06/20] added new repos --- Gemfile | 2 ++ Gemfile.lock | 2 ++ lib/PetDatabaseRepo.rb | 13 ++++++++----- lib/repos/DB | 10 ++++++++++ lib/repos/DB.rb | 10 ++++++++++ server.rb | 21 ++++++++++++++------- 6 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 lib/repos/DB create mode 100644 lib/repos/DB.rb diff --git a/Gemfile b/Gemfile index 4324772d..1446f3d9 100644 --- a/Gemfile +++ b/Gemfile @@ -5,5 +5,7 @@ gem 'rspec', '~> 2.14.1' gem 'sinatra', '~> 1.4.5' gem 'sinatra-contrib', '~> 1.4.2' gem 'rest-client' +gem 'pg' + # Testing gem 'pry-byebug' diff --git a/Gemfile.lock b/Gemfile.lock index 49a90396..1bf7ce17 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,6 +14,7 @@ GEM mime-types (2.4.3) multi_json (1.10.1) netrc (0.10.0) + pg (0.17.1) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -55,6 +56,7 @@ PLATFORMS ruby DEPENDENCIES + pg pry-byebug rest-client rspec (~> 2.14.1) diff --git a/lib/PetDatabaseRepo.rb b/lib/PetDatabaseRepo.rb index 1d150032..f7315bd8 100644 --- a/lib/PetDatabaseRepo.rb +++ b/lib/PetDatabaseRepo.rb @@ -31,8 +31,8 @@ module PetShop class Database - def initialize - @db = PG.connect(host: 'localhost', dbname:'petshop_db') + def self.dbconnect + PG.connect(host: 'localhost', dbname:'petshop_db') end def addData @@ -83,6 +83,11 @@ def addData }#end each shop end + + + +##################################################### + def print_shops sql = %q[ SELECT * FROM shops] @@ -191,6 +196,4 @@ def allpets end end #end of class - - - +end #end of module \ No newline at end of file diff --git a/lib/repos/DB b/lib/repos/DB new file mode 100644 index 00000000..4c4d4d4c --- /dev/null +++ b/lib/repos/DB @@ -0,0 +1,10 @@ +module PetShop +class DB + + def self.get_shops(db) + db.exec("SELECT * FROM shops").entries + end + + +end +end \ No newline at end of file diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb new file mode 100644 index 00000000..4c4d4d4c --- /dev/null +++ b/lib/repos/DB.rb @@ -0,0 +1,10 @@ +module PetShop +class DB + + def self.get_shops(db) + db.exec("SELECT * FROM shops").entries + end + + +end +end \ No newline at end of file diff --git a/server.rb b/server.rb index 07df95f4..fee19c49 100644 --- a/server.rb +++ b/server.rb @@ -2,6 +2,9 @@ require 'sinatra/reloader' require 'rest-client' require 'json' +require 'pry-byebug' +require_relative 'lib/PetDatabaseRepo.rb' +require_relative 'lib/repos/DB.rb' # # # This is our only html view... @@ -19,7 +22,11 @@ # get '/shops' do headers['Content-Type'] = 'application/json' - RestClient.get("http://pet-shop.api.mks.io/shops") + # RestClient.get("http://pet-shop.api.mks.io/shops") + db = PetShop::Database.dbconnect + shops = PetShop::DB.get_shops(db) + shops.to_json + end post '/signin' do @@ -48,7 +55,7 @@ headers['Content-Type'] = 'application/json' id = params[:id] # TODO: Grab from database instead - RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") + # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") end put '/shops/:shop_id/cats/:id/adopt' do @@ -56,8 +63,8 @@ shop_id = params[:shop_id] id = params[:id] # TODO: Grab from database instead - RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/cats/#{id}", - { adopted: true }, :content_type => 'application/json') + # RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/cats/#{id}", + # { adopted: true }, :content_type => 'application/json') # TODO (after you create users table): Attach new cat to logged in user end @@ -69,7 +76,7 @@ headers['Content-Type'] = 'application/json' id = params[:id] # TODO: Update database instead - RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") + # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") end put '/shops/:shop_id/dogs/:id/adopt' do @@ -77,8 +84,8 @@ shop_id = params[:shop_id] id = params[:id] # TODO: Update database instead - RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", - { adopted: true }, :content_type => 'application/json') + # RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", + # { adopted: true }, :content_type => 'application/json') # TODO (after you create users table): Attach new dog to logged in user end From ca84ea337b2da610486d9e22bc3bb9839d46f3f9 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 15:32:18 -0600 Subject: [PATCH 07/20] Prepping to merge --- Gemfile | 1 + Gemfile.lock | 2 ++ lib/PetDatabaseRepo.rb | 6 +++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 4324772d..637d5332 100644 --- a/Gemfile +++ b/Gemfile @@ -7,3 +7,4 @@ gem 'sinatra-contrib', '~> 1.4.2' gem 'rest-client' # Testing gem 'pry-byebug' +gem 'pg' diff --git a/Gemfile.lock b/Gemfile.lock index 49a90396..1bf7ce17 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,6 +14,7 @@ GEM mime-types (2.4.3) multi_json (1.10.1) netrc (0.10.0) + pg (0.17.1) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -55,6 +56,7 @@ PLATFORMS ruby DEPENDENCIES + pg pry-byebug rest-client rspec (~> 2.14.1) diff --git a/lib/PetDatabaseRepo.rb b/lib/PetDatabaseRepo.rb index 1d150032..f2a66e6a 100644 --- a/lib/PetDatabaseRepo.rb +++ b/lib/PetDatabaseRepo.rb @@ -29,7 +29,7 @@ # imageurl | character varying | # adoption_status | boolean | -module PetShop + class Database def initialize @db = PG.connect(host: 'localhost', dbname:'petshop_db') @@ -51,7 +51,7 @@ def addData url = "pet-shop.api.mks.io/shops/" + shopid.to_s + "/dogs/" tempdogs = RestClient.get(url) dogs = JSON.parse(tempdogs) - dogs.each { |dog| #each dog + dogs.each { |dog| #each dogs shop = dog["shopId"] name = dog["name"] happiness = dog["happiness"] @@ -62,7 +62,7 @@ def addData INSERT INTO dogs (shop_id, dog_id, name, imageurl, happiness, adoption_status) VALUES ($1, $2, $3, $4, $5, $6) ] - @db.exec_params(sql, [shop, id, name, image, happiness, adopt]) + @db.exec(sql, [shop, id, name, image, happiness, adopt]) }#end each dog url = "pet-shop.api.mks.io/shops/" + shopid.to_s + "/cats/" From 9085c8c3a63ceabf5bc6a5c3623fea59ab8bcbe6 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 15:38:53 -0600 Subject: [PATCH 08/20] updated after merge --- Gemfile | 1 - lib/PetDatabaseRepo.rb | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index f27610a2..1446f3d9 100644 --- a/Gemfile +++ b/Gemfile @@ -9,4 +9,3 @@ gem 'pg' # Testing gem 'pry-byebug' -gem 'pg' diff --git a/lib/PetDatabaseRepo.rb b/lib/PetDatabaseRepo.rb index c4ee25be..2e23fffc 100644 --- a/lib/PetDatabaseRepo.rb +++ b/lib/PetDatabaseRepo.rb @@ -29,7 +29,7 @@ # imageurl | character varying | # adoption_status | boolean | - +module PetShop class Database def self.dbconnect PG.connect(host: 'localhost', dbname:'petshop_db') From 480f3543b7afd5f9ea68ff94b3b6dba66a2404f0 Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Thu, 11 Dec 2014 15:48:37 -0600 Subject: [PATCH 09/20] add changes --- lib/repos/DB.rb | 4 ++++ server.rb | 3 +++ 2 files changed, 7 insertions(+) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index 4c4d4d4c..9b750345 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -5,6 +5,10 @@ def self.get_shops(db) db.exec("SELECT * FROM shops").entries end + def self.get_cats(db, shop_id) + db.exec("SELECT * FROM cats WHERE shop_id = $1", [shop_id]).entries + end + end end \ No newline at end of file diff --git a/server.rb b/server.rb index fee19c49..b2fbb5d8 100644 --- a/server.rb +++ b/server.rb @@ -56,6 +56,9 @@ id = params[:id] # TODO: Grab from database instead # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") + db = PetShop::Database.dbconnect + cats = PetShop::DB.get_cats(db, id) + cats.to_json end put '/shops/:shop_id/cats/:id/adopt' do From 37296201be68f05b8c2d1d2d182c7b9cebf8d84d Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Thu, 11 Dec 2014 16:39:04 -0600 Subject: [PATCH 10/20] minor changes --- lib/repos/DB.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index 9b750345..3c71c420 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -6,9 +6,9 @@ def self.get_shops(db) end def self.get_cats(db, shop_id) - db.exec("SELECT * FROM cats WHERE shop_id = $1", [shop_id]).entries + db.exec("SELECT * FROM cats WHERE shopid = $1", [shop_id]).entries end -end -end \ No newline at end of file +end #end DB +end#end module \ No newline at end of file From 6850de198beaa0c01dfebd4ee1e630bfea6e976b Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 16:39:21 -0600 Subject: [PATCH 11/20] Cats db work --- lib/repos/DB.rb | 25 +++++++++++++++++++++++-- server.rb | 7 ++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index 9b750345..50cee362 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -6,9 +6,30 @@ def self.get_shops(db) end def self.get_cats(db, shop_id) - db.exec("SELECT * FROM cats WHERE shop_id = $1", [shop_id]).entries + 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| + 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 end -end \ No newline at end of file +end +# {"shop_id":"3", +# "cat_id":"5", +# "name":"karthurian", +# "imageurl":"http://cucinatestarossa.blogs.com/weblog/images/cat.jpg", +# "adoption_status":"t"} +# {"shopId":1, +# "name":"Scaredy Cat", +# "imageUrl":"http://i.imgur.com/TOEskNX.jpg", +# "adopted":true, +# "id":1} \ No newline at end of file diff --git a/server.rb b/server.rb index 2efed647..12913cb1 100644 --- a/server.rb +++ b/server.rb @@ -59,6 +59,7 @@ # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") db = PetShop::Database.dbconnect cats = PetShop::DB.get_cats(db, id) + puts cats.to_json cats.to_json end @@ -80,7 +81,7 @@ headers['Content-Type'] = 'application/json' id = params[:id] # TODO: Update database instead - # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") + RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") end put '/shops/:shop_id/dogs/:id/adopt' do @@ -88,8 +89,8 @@ shop_id = params[:shop_id] id = params[:id] # TODO: Update database instead - # RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", - # { adopted: true }, :content_type => 'application/json') + RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", + { adopted: true }, :content_type => 'application/json') # TODO (after you create users table): Attach new dog to logged in user end From 6cdad2b67aa6293f673aac660efb4dd62c154408 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Thu, 11 Dec 2014 16:52:41 -0600 Subject: [PATCH 12/20] 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 27fe54dbd9c30ba4e5aedff737ca05f347d9092a Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Thu, 11 Dec 2014 17:01:18 -0600 Subject: [PATCH 13/20] got cats and dogs db working --- lib/repos/DB.rb | 15 +++++++++++++++ server.rb | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index c05ee637..05869d8a 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -20,6 +20,21 @@ def self.get_cats(db, shop_id) cats end + def self.get_dogs(db, shop_id) + mappings = {"shop_id" => "shopId", "dog_id" => "id", "name" => "name", "imageurl" => "imageUrl", "adoption_status" => "adopted"} + response = db.exec("SELECT * FROM dogs WHERE shop_id = $1", [shop_id]).entries + dogs = [] + response.each do |dog| + if dog['adoption_status'] == "t" + dog['adoption_status'] = "true" + else + dog['adoption_status'] = "false" + end + dogs << Hash[dog.map {|k, v| [mappings[k], v] }] + end + dogs + end + end #end DB end#end module diff --git a/server.rb b/server.rb index 94d1c909..054518fa 100644 --- a/server.rb +++ b/server.rb @@ -79,8 +79,11 @@ get '/shops/:id/dogs' do headers['Content-Type'] = 'application/json' id = params[:id] + db = PetShop::Database.dbconnect + dogs = PetShop::DB.get_dogs(db, id) + dogs.to_json # TODO: Update database instead - RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") + # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") end put '/shops/:shop_id/dogs/:id/adopt' do From b38d1686af7c546af9febfcda7025e52eee2647c Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Fri, 12 Dec 2014 10:41:14 -0600 Subject: [PATCH 14/20] pet adoption methods --- lib/repos/DB.rb | 17 ++++++++++++----- server.rb | 10 +++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index 05869d8a..c61529d7 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -11,30 +11,37 @@ 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 cats end - def self.get_dogs(db, shop_id) + def self.get_dogs(db, shop_id) mappings = {"shop_id" => "shopId", "dog_id" => "id", "name" => "name", "imageurl" => "imageUrl", "adoption_status" => "adopted"} response = db.exec("SELECT * FROM dogs WHERE shop_id = $1", [shop_id]).entries 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 dogs end + def self.adopt_cat(db, cat_id) + db.exec("UPDATE cats set adoption_status = 'true' where cat_id = $1", [cat_id]) + end + + def self.adopt_dog(db, dog_id) + db.exec("UPDATE dogs set adoption_status = 'true' where dog_id = $1", [dog_id]) + end end #end DB end#end module diff --git a/server.rb b/server.rb index 054518fa..6e15108d 100644 --- a/server.rb +++ b/server.rb @@ -69,6 +69,8 @@ # TODO: Grab from database instead # RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/cats/#{id}", # { adopted: true }, :content_type => 'application/json') + db = PetShop::Database.dbconnect + PetShop::DB.adopt_cat(db, id) # TODO (after you create users table): Attach new cat to logged in user end @@ -79,7 +81,7 @@ get '/shops/:id/dogs' do headers['Content-Type'] = 'application/json' id = params[:id] - db = PetShop::Database.dbconnect + db = PetShop::Database.dbconnect dogs = PetShop::DB.get_dogs(db, id) dogs.to_json # TODO: Update database instead @@ -91,8 +93,10 @@ shop_id = params[:shop_id] id = params[:id] # TODO: Update database instead - RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", - { adopted: true }, :content_type => 'application/json') + # RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", + # { adopted: true }, :content_type => 'application/json') + db = PetShop::Database.dbconnect + PetShop::DB.adopt_dog(db, id) # TODO (after you create users table): Attach new dog to logged in user end From a05460266eeb24be9beff3102bcbea0f5ca54976 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Fri, 12 Dec 2014 10:42:10 -0600 Subject: [PATCH 15/20] 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 a7b731f7c2c13a19891c6b400890bdd43727f942 Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Fri, 12 Dec 2014 11:05:43 -0600 Subject: [PATCH 16/20] got signing working --- lib/repos/DB.rb | 14 ++++++++++++++ server.rb | 12 +++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index c61529d7..f95cb2db 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -43,6 +43,20 @@ def self.adopt_dog(db, dog_id) db.exec("UPDATE dogs set adoption_status = 'true' where dog_id = $1", [dog_id]) end + def self.find db, user_id + sql = %q[SELECT * FROM users WHERE id = $1] + result = db.exec(sql, [user_id]) + result.first + end + + # find user by username. Intended to be used when + # someone tries to sign in. + def self.find_by_name db, username + sql = %q[SELECT * FROM users WHERE username = $1] + result = db.exec(sql, [username]) + result.first + end + end #end DB end#end module diff --git a/server.rb b/server.rb index 6e15108d..21948e00 100644 --- a/server.rb +++ b/server.rb @@ -13,7 +13,7 @@ get '/' do if session[:user_id] # TODO: Grab user from database - @current_user = $sample_user + # @current_user = $sample_user end erb :index end @@ -37,13 +37,19 @@ password = params['password'] # TODO: Grab user by username from database and check password - user = { 'username' => 'alice', 'password' => '123' } + # 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' + # TODO: Return all pets adopted by this user # TODO: Set session[:user_id] so the server will remember this user has logged in - $sample_user.to_json + # $sample_user.to_json + session[:user_id] = user['id'] + user.to_json else status 401 end From db000b16b5228e8e65089b286cf1c43bf8def692 Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Fri, 12 Dec 2014 11:54:30 -0600 Subject: [PATCH 17/20] changes --- lib/repos/DB.rb | 6 ++++-- server.rb | 24 +++++++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/repos/DB.rb b/lib/repos/DB.rb index f95cb2db..bbd32d6b 100644 --- a/lib/repos/DB.rb +++ b/lib/repos/DB.rb @@ -35,12 +35,14 @@ def self.get_dogs(db, shop_id) dogs end - def self.adopt_cat(db, cat_id) + def self.adopt_cat(db, cat_id, user_id) db.exec("UPDATE cats set adoption_status = 'true' where cat_id = $1", [cat_id]) + db.exec("INSERT INTO adoptions (user_id, catid) VALUES ($1, $2)", [user_id, cat_id]) end - def self.adopt_dog(db, dog_id) + def self.adopt_dog(db, dog_id, user_id) db.exec("UPDATE dogs set adoption_status = 'true' where dog_id = $1", [dog_id]) + db.exec("INSERT INTO adoptions (user_id, dogid) VALUES ($1, $2)", [user_id, dog_id]) end def self.find db, user_id diff --git a/server.rb b/server.rb index 21948e00..ea24b59c 100644 --- a/server.rb +++ b/server.rb @@ -9,15 +9,33 @@ set :bind, '0.0.0.0' # # # This is our only html view... -# + +configure do + 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 + end + end + + get '/' do - if session[:user_id] + if session['user_id'] # TODO: Grab user from database - # @current_user = $sample_user + db = PetShop::Database.dbconnect + @current_user = PetShop::DB.find(db, session[:user_id]) end erb :index end +get '/logout' do + session.delete('user_id') + redirect to '/' +end # # # ...the rest are JSON endpoints # From 1ae89e6084eb6fe74552cf06fdc8a3774a7c777f Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Fri, 12 Dec 2014 11:55:22 -0600 Subject: [PATCH 18/20] 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 19/20] 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 From 2f0ddb6abdf5c722220b7b95040e4ed668e15d54 Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Fri, 12 Dec 2014 12:32:49 -0600 Subject: [PATCH 20/20] whatever --- lib/repos/DB | 10 ---------- server.rb | 3 --- 2 files changed, 13 deletions(-) delete mode 100644 lib/repos/DB diff --git a/lib/repos/DB b/lib/repos/DB deleted file mode 100644 index 4c4d4d4c..00000000 --- a/lib/repos/DB +++ /dev/null @@ -1,10 +0,0 @@ -module PetShop -class DB - - def self.get_shops(db) - db.exec("SELECT * FROM shops").entries - end - - -end -end \ No newline at end of file diff --git a/server.rb b/server.rb index ea24b59c..ad95d615 100644 --- a/server.rb +++ b/server.rb @@ -116,9 +116,6 @@ headers['Content-Type'] = 'application/json' shop_id = params[:shop_id] id = params[:id] - # TODO: Update database instead - # RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs/#{id}", - # { adopted: true }, :content_type => 'application/json') db = PetShop::Database.dbconnect PetShop::DB.adopt_dog(db, id) # TODO (after you create users table): Attach new dog to logged in user