Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/PetDatabaseRepo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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/"
Expand Down Expand Up @@ -196,4 +196,4 @@ def allpets
end

end #end of class
end #end of module
end #end of module
25 changes: 23 additions & 2 deletions lib/repos/DB.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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}
8 changes: 5 additions & 3 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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...
#
Expand Down Expand Up @@ -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

Expand All @@ -79,16 +81,16 @@
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
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')
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

Expand Down