Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3208a46
initial commit
gmikeska Dec 11, 2014
700ea53
adding repos
gmikeska Dec 11, 2014
a038b79
set up BCrypt for user authentication
gmikeska Dec 11, 2014
52c1b2e
set up BCrypt for user authentication 2
gmikeska Dec 11, 2014
8b4743c
set session id upon succesful password and collect pet data
colemandavid55 Dec 11, 2014
86a82fb
petshop query
JMGoldsmith Dec 11, 2014
4c24c27
change login name from default to current user
colemandavid55 Dec 11, 2014
7f35743
petshop query
JMGoldsmith Dec 11, 2014
68a5391
no clue
JMGoldsmith Dec 11, 2014
3cf37af
Merge branch 'pet-shop-server' of https://github.com/gmikeska/ruby-co…
JMGoldsmith Dec 11, 2014
454bae9
cats repo
JMGoldsmith Dec 11, 2014
1d92bbb
petshop repo extended
JMGoldsmith Dec 11, 2014
dcc983e
add dogs repo function
colemandavid55 Dec 11, 2014
156b4b9
Merge branch 'pet-shop-server' of https://github.com/gmikeska/ruby-co…
colemandavid55 Dec 11, 2014
0022d1f
adjust dog endpoints
colemandavid55 Dec 11, 2014
8b1a40d
change class to module for petshop-dogsrepo
colemandavid55 Dec 11, 2014
fe3142f
fixing bugs with the user repo spec
gmikeska Dec 11, 2014
8c2ab4d
Merge branch 'pet-shop-server' of github.com:gmikeska/ruby-course int…
gmikeska Dec 11, 2014
7eb4b89
removing HEAD thngy
gmikeska Dec 11, 2014
e09dc97
Merge correction
colemandavid55 Dec 11, 2014
b0f63a5
Merge branch 'pet-shop-server' of https://github.com/gmikeska/ruby-co…
colemandavid55 Dec 11, 2014
3d9755b
got rid of spec closes #7
gmikeska Dec 11, 2014
cf8873b
Merge branch 'pet-shop-server' of github.com:gmikeska/ruby-course int…
gmikeska Dec 11, 2014
b092222
removed .rspec file. OUT DAMNED SPEC!!!!
gmikeska Dec 11, 2014
1aacd35
remove rest-client, require json and render reterned hashes into json
colemandavid55 Dec 11, 2014
4d8c760
create db connection in dog sections
colemandavid55 Dec 11, 2014
5e2abbd
set up database call for shops.all
gmikeska Dec 11, 2014
1234e41
Merge branch 'pet-shop-server' of github.com:gmikeska/ruby-course int…
gmikeska Dec 11, 2014
370e8ee
adds requires for bcrypt dependency run bundle install when pulling!
gmikeska Dec 12, 2014
70dc152
corrected typo in server.rb
gmikeska Dec 12, 2014
0bfbeae
did some cleanup on repos added bind on server.rb for the mac folks
gmikeska Dec 12, 2014
b7b5bbc
fixed several typos
gmikeska Dec 12, 2014
e075c9b
refactored some database queries and the way "adopted" is stored
gmikeska Dec 12, 2014
ea43e11
fixed cat & dog adoption methods
gmikeska Dec 12, 2014
b709dd7
fixed a typo
gmikeska Dec 12, 2014
aa64856
finished the adopted animals return func
gmikeska Dec 12, 2014
1d7ac7b
refactored all adopt and find by user methods to reduce redundancy ad…
gmikeska Dec 12, 2014
92814af
refactored all adopt and find by user methods to reduce redundancy ad…
gmikeska Dec 12, 2014
cc64d88
added unadopt link to view
gmikeska Dec 12, 2014
dc77bec
completed the unadopt
gmikeska Dec 12, 2014
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
1 change: 0 additions & 1 deletion .rspec

This file was deleted.

6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ ruby '2.0.0'
gem 'rspec', '~> 2.14.1'
gem 'sinatra', '~> 1.4.5'
gem 'sinatra-contrib', '~> 1.4.2'

gem 'rest-client'
gem 'netrc'
# Testing
gem 'pry-byebug'
gem 'pg'
gem 'json'
gem 'bcrypt'
17 changes: 15 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ GEM
remote: https://rubygems.org/
specs:
backports (3.6.3)
bcrypt (3.1.9)
byebug (3.5.1)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
Expand All @@ -10,8 +11,12 @@ GEM
columnize (0.8.9)
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
json (1.8.1)
method_source (0.8.2)
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)
Expand All @@ -24,6 +29,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)
Expand All @@ -50,7 +58,12 @@ PLATFORMS
ruby

DEPENDENCIES
bcrypt
json
netrc
pg
pry-byebug
rest-client
rspec (~> 2.14.1)
sinatra
sinatra-contrib
sinatra (~> 1.4.5)
sinatra-contrib (~> 1.4.2)
8 changes: 8 additions & 0 deletions lib/cats_repo/cats_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module PetShop
class CatsRepo
def self.all_from_shop(db, id)
db.exec(%q[SELECT id, shopid, name, imageurl AS "imageUrl", adopted FROM cats WHERE shopid = $1],[id]).to_a

end
end
end
9 changes: 9 additions & 0 deletions lib/dogs_repo/dogs_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module PetShop
class DogsRepo

def self.all_from_shop(db, id)
sql = %q[SELECT id, shopid, name, imageurl AS "imageUrl", adopted, happiness FROM dogs WHERE shopid = $1]
db.exec(sql, [id]).to_a
end
end
end
86 changes: 86 additions & 0 deletions lib/pet-shop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
require 'pg'
require 'bcrypt'

require_relative 'petshops_repo/petshops_repo.rb'
require_relative 'cats_repo/cats_repo.rb'
require_relative 'dogs_repo/dogs_repo.rb'
require_relative 'users_repo/users_repo.rb'

module PetShop
def self.create_db_connection()
PG.connect(host: 'localhost', dbname: "petshop_db", user: "ruby",
password: "rubyRailsJS")
end

def self.create_tables(db)
db.exec <<-SQL
CREATE TABLE IF NOT EXISTS petshops(
id SERIAL PRIMARY KEY,
name VARCHAR
);
CREATE TABLE IF NOT EXISTS cats(
id SERIAL PRIMARY KEY,
shopid INTEGER references petshops(id),
imageUrl VARCHAR,
name VARCHAR,
adopted VARCHAR
);
CREATE TABLE IF NOT EXISTS dogs(
id SERIAL PRIMARY KEY,
shopid INTEGER references petshops(id),
imageUrl VARCHAR,
name VARCHAR,
happiness VARCHAR,
adopted VARCHAR
);
CREATE TABLE IF NOT EXISTS users(
id SERIAL PRIMARY KEY,
username VARCHAR,
password VARCHAR
);
CREATE TABLE IF NOT EXISTS pet_adoptions(
id SERIAL PRIMARY KEY,
type VARCHAR,
user_id INTEGER references users(id),
pet_id INTEGER
);
SQL
end

def self.drop_tables(db)
db.exec <<-SQL
DROP TABLE petshops CASCADE;
DROP TABLE cats CASCADE;
DROP TABLE dogs CASCADE;
DROP TABLE users CASCADE;
DROP TABLE pet_adoptions;
SQL
end

def self.seed_db(db)
r = db.exec <<-SQL
INSERT INTO petshops (name) values ('My Pet Shop') RETURNING id
SQL

dogurl = "http://www.oldyelladogranch.com/puppies.jpg"
caturl = "http://nextranks.com/data_images/main/cats/cats-04.jpg"
shopid = r[0]['id']
q = "INSERT INTO cats (shopid, imageUrl, name) values ($1, $2, 'cat1')"
db.exec(q,[shopid, caturl])
q = "INSERT INTO dogs (shopid, imageUrl, name, happiness) values ($1, $2, 'dog1', '5')"
db.exec(q,[shopid, dogurl])
password_hash = BCrypt::Password.create('ilovepets')
q = "INSERT INTO users (username, password) values ('ilovepets', '#{password_hash}')"
db.exec(q)
password_hash = BCrypt::Password.create('someuser')
q = "INSERT INTO users (username, password) values ('someuser', '#{password_hash}')"
db.exec(q)
password_hash = BCrypt::Password.create('someotherperson')
q = "INSERT INTO users (username, password) values ('someotherperson', '#{password_hash}')"
db.exec(q)

end



end
8 changes: 8 additions & 0 deletions lib/petshops_repo/petshops_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module PetShop
class Shops
def self.get_all_shops(db)
shops = db.exec('SELECT * FROM petshops').to_a
end

end
end
80 changes: 80 additions & 0 deletions lib/users_repo/users_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module PetShop
class UsersRepo
# find a user by user ID. Intended to be used when
# someone is already authenticated. We keep their
# user id in the cookie.
def self.find db, user_id
sql = %q[SELECT * FROM users WHERE id = $1]
result = db.exec(sql, [user_id]).to_a
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]).to_a
result.first
end

# when someone signs up use this method to save their
# information in the db. we're not encrypting passwords.
def self.save db, user_data
sql = %q[INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *]
result = db.exec(sql, [user_data[:username], user_data[:password]])
result.first
end

def self.adopt db, user_data
sql = %q[INSERT INTO pet_adoptions (type, user_id, pet_id) VALUES ($1, $2, $3) RETURNING *]
result = db.exec(sql, [user_data[:type], user_data[:user_id], user_data[:pet_id]])

if(user_data[:type] == "cat")
sql = %q[UPDATE cats SET adopted = 'true' WHERE id = $1]
result = db.exec(sql, [user_data[:pet_id]])
end

if(user_data[:type] == "dog")
sql = %q[UPDATE dogs SET adopted = 'true' WHERE id = $1]
result = db.exec(sql, [user_data[:pet_id]])
end
return "Adopted: true"
end

def self.unadopt db, user_data
sql = %q[DELETE FROM pet_adoptions WHERE type = $1 and user_id = $2 and pet_id = $3]
result = db.exec(sql, [user_data[:type], user_data[:user_id], user_data[:pet_id]])

if(user_data[:type] == "cat")
sql = %q[UPDATE cats SET adopted = NULL WHERE id = $1]
result = db.exec(sql, [user_data[:pet_id]])
end

if(user_data[:type] == "dog")
sql = %q[UPDATE dogs SET adopted = NULL WHERE id = $1]
result = db.exec(sql, [user_data[:pet_id]])
end
return "something to prevent server err."
end

def self.get_dog_by_id db, user_data
sql = %q[SELECT * FROM dogs WHERE id = $1]
db.exec(sql, [user_data]).to_a.first
end

def self.get_cat_by_id db, user_data
sql = %q[SELECT * FROM cats WHERE id = $1]
db.exec(sql, [user_data]).to_a.first
end


def self.get_adoptions db, user_data
user_id = user_data['id']
sql = %q[SELECT d.id, d.name, d.imageurl AS "imageUrl" FROM pet_adoptions a JOIN dogs d ON d.id = a.pet_id WHERE user_id = $1 and type = 'dog']
user_data["dogs"] = db.exec(sql, [user_id]).to_a
sql = %q[SELECT c.id, c.name, c.imageurl AS "imageUrl" FROM pet_adoptions a JOIN cats c ON c.id = a.pet_id WHERE a.user_id = $1 and type = 'cat']
user_data["cats"] = db.exec(sql, [user_id]).to_a
return user_data
end
end
end
7 changes: 6 additions & 1 deletion public/js/pet-shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
var pets = shop[type]
var adoptUrl = "/shops/" + shop.id + "/" + type + "/" + petId + "/adopt"


m.request({ method: 'put', url: adoptUrl }).then(function() {
var pet = pets().find(function(p){ return p.id == petId })
pet.adopted = true
Expand Down Expand Up @@ -46,6 +47,10 @@
onclick: ctrl.adopt.coldCurry(type, pet.id),
href: '#'
}, 'Adopt this pet')
var unAdoptLink = m('a', {
onclick: ctrl.adopt.coldCurry(type, pet.id),
href: '#'
}, 'Ditch this loser pet.')

return m('.pet', [
m('.photo',
Expand All @@ -56,7 +61,7 @@
m('b', "Adopted: "),
m('span', pet.adopted ? "Yes!" : "No..."),
m('br'),
pet.adopted ? null : adoptLink
pet.adopted ? unAdoptLink : adoptLink
])
])
})
Expand Down
Loading