diff --git a/lib/PetDatabaseRepo.rb b/lib/PetDatabaseRepo.rb index f7315bd8..2e23fffc 100644 --- a/lib/PetDatabaseRepo.rb +++ b/lib/PetDatabaseRepo.rb @@ -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/" @@ -196,4 +196,4 @@ def allpets end end #end of class -end #end of module \ No newline at end of file +end #end of module 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 b2fbb5d8..12913cb1 100644 --- a/server.rb +++ b/server.rb @@ -6,6 +6,7 @@ require_relative 'lib/PetDatabaseRepo.rb' require_relative 'lib/repos/DB.rb' +set :bind, '0.0.0.0' # # # This is our only html view... # @@ -58,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 @@ -79,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 @@ -87,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