Skip to content
Merged
62 changes: 54 additions & 8 deletions lib/repos/DB.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

def self.get_dogs(db, shop_id)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
21 changes: 19 additions & 2 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
# #
# 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
end
end

get '/' do
if session[:user_id]
# TODO: Grab user from database
Expand Down Expand Up @@ -40,7 +53,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'
Expand Down Expand Up @@ -80,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 #
Expand Down Expand Up @@ -117,4 +134,4 @@
dogs: [
{ shopId: 1, name: "Leaf Pup", imageUrl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted: "true" }
]
}
}