Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jessica, Nora and Cristina' s Pet Shop Server #461

Open
wants to merge 28 commits into
base: pet-shop-server
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
91f4958
first commit
JessicaMeyer Dec 12, 2014
8d43b78
add server.rb and lib etc
RenaissanceAI Dec 12, 2014
8620e53
change name into seed_db cristia->jessica and password 1234->123
RenaissanceAI Dec 12, 2014
2b868cc
fix merging issues
RenaissanceAI Dec 12, 2014
b25949d
fix merging issues
RenaissanceAI Dec 12, 2014
a09dc5b
fix merging issues
RenaissanceAI Dec 12, 2014
d714426
fix merge issues
LadyNorm Dec 12, 2014
fc6f835
fix merge conflicts
LadyNorm Dec 12, 2014
97d5485
add updated cat/dog tables
JessicaMeyer Dec 12, 2014
6dfe362
Merge branch 'pet-shop-server' of https://github.com/JessicaMeyer/rub…
LadyNorm Dec 12, 2014
ce6b6ad
update dog/cat/shop repos
LadyNorm Dec 12, 2014
a52496b
remove restclient and add seed_shops
RenaissanceAI Dec 12, 2014
3373f99
fix merging issues
RenaissanceAI Dec 12, 2014
acc1d45
add shops_repo
RenaissanceAI Dec 12, 2014
c25fdad
test petshop
RenaissanceAI Dec 12, 2014
6e02a8d
change update tables dogs and cats
RenaissanceAI Dec 12, 2014
6deabf5
setup rspec and add spec files
JessicaMeyer Dec 12, 2014
0345d8f
set up rspec and add spec files
JessicaMeyer Dec 12, 2014
7c6684a
correct repo location
LadyNorm Dec 12, 2014
448712d
change update tables dogs and cats
RenaissanceAI Dec 12, 2014
a7eb2e0
Merge branch 'pet-shop-server' of https://github.com/JessicaMeyer/rub…
RenaissanceAI Dec 12, 2014
b30dfae
load cats and dogs
RenaissanceAI Dec 12, 2014
6ac3663
make shops cats dogs viewable to all
RenaissanceAI Dec 12, 2014
fd87abf
add cats and dogs db requests
RenaissanceAI Dec 12, 2014
ad1eea6
fixed adopted link and added
RenaissanceAI Dec 12, 2014
3f612a5
fix server.rb
RenaissanceAI Dec 15, 2014
73e3b67
fix petshopserver.rb add adopt and find pets by user_id
RenaissanceAI Dec 15, 2014
38dbcd8
clean up server.rb
RenaissanceAI Dec 15, 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
9 changes: 8 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ gem 'rspec', '~> 2.14.1'
gem 'sinatra', '~> 1.4.5'
gem 'sinatra-contrib', '~> 1.4.2'
gem 'rest-client'
gem 'rack-flash3'
gem 'pg'
# added for rspec portion group block

group :test do
gem 'rack-test'
end

# Testing
gem 'pry-byebug'
gem 'pry-byebug'
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ GEM
mime-types (1.25.1)
multi_json (1.10.1)
netrc (0.8.0)
pg (0.17.1)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
Expand All @@ -22,6 +23,8 @@ GEM
byebug (~> 3.4)
pry (~> 0.10)
rack (1.5.2)
rack-flash3 (1.0.5)
rack
rack-protection (1.5.3)
rack
rack-test (0.6.2)
Expand Down Expand Up @@ -55,7 +58,10 @@ PLATFORMS
ruby

DEPENDENCIES
pg
pry-byebug
rack-flash3
rack-test
rest-client
rspec (~> 2.14.1)
sinatra (~> 1.4.5)
Expand Down
151 changes: 151 additions & 0 deletions lib/petshopserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
require 'pg'

require_relative 'petshopserver/repos/shops_repo.rb'
require_relative 'petshopserver/repos/cats_repo.rb'
require_relative 'petshopserver/repos/dogs_repo.rb'
require_relative 'petshopserver/repos/users_repo.rb'

module Petshopserver
def self.create_db_connection(dbname)
PG.connect(host: 'localhost', dbname: dbname)
end

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM shops;
DELETE FROM dogs;
DELETE FROM cats;
DELETE FROM users;
SQL
end

def self.create_tables(db)
db.exec <<-SQL
CREATE TABLE IF NOT EXISTS users(
id SERIAL PRIMARY KEY,
username VARCHAR,
password VARCHAR
);
CREATE TABLE IF NOT EXISTS shops(
id SERIAL PRIMARY KEY,
name VARCHAR
);
CREATE TABLE IF NOT EXISTS dogs(
id SERIAL PRIMARY KEY,
"shopId" int references shops(id),
name varchar,
"imageUrl" varchar,
happiness int,
adopted boolean
);
CREATE TABLE IF NOT EXISTS cats(
id SERIAL PRIMARY KEY,
"shopId" int references shops(id),
name varchar,
"imageUrl" varchar,
adopted boolean
);
CREATE TABLE IF NOT EXISTS userPets(
id SERIAL PRIMARY KEY,
"user_id" INTEGER references users(id),
"cat_id" INTEGER references cats(id),
"dog_id" INTEGER references dogs(id)
);
SQL
end

def self.seed_db(db)
db.exec <<-SQL
INSERT INTO users (username, password) values ('anonymous', 'anonymous')
# INSERT INTO users (username, password) values ('Jessica', '123')
SQL
end

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

def self.seed_all_db(db)
shops = JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops")
shops.shift
cats = shops.map do |shop|
shop_id = shop['id']
JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/cats")
end

dogs = shops.map do |shop|
shop_id = shop['id']
JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs")
end

save_shops_to_db(shops,db)
save_cats_to_db(cats.flatten,db)
save_dogs_to_db(dogs.flatten,db)
end

def self.save_shops_to_db(shops,db)
sq = []
# "(1, 'Jo's Palace'), (2, Nicks Shop)"
shops.each do |shop|
shop["name"].gsub(/'/, "///'")
end
values = shops.map do |shop|
id = shop['id'].to_i
name = shop['name']
db.escape_string(name.to_s)
["(#{id}, '#{name}')"]
end
values = values.flatten.join(',')
sql = %Q[
insert into shops (id, name) values #{values}
]

db.exec(sql)
end

def self.save_cats_to_db(cats,db)
sql = %q[
INSERT INTO cats (shopId, name, imageUrl, adopted)
VALUES ($1, $2, $3, $4)
]
cats.each do |cat|
name = cat['name']
imageurl = cat['imageUrl']
adopted = cat['adopted']
shopid = cat['shopId']

db.exec(sql, [shopid, name, imageurl, adopted])
end
end

def self.save_dogs_to_db(dogs,db)
sql = %q[
INSERT INTO dogs (shopId, name, imageUrl, happiness, adopted)
VALUES ($1, $2, $3, $4, $5)
]
dogs.each do |dog|
name = dog['name']
imageurl = dog['imageUrl']
happiness = dog['happiness'].to_i
x ||= dog['adopted']
shopid = dog['shopId']

db.exec(sql, [shopid, name, imageurl, happiness, adopted])
end
end

def self.boolean_type_cast object, key
if object[key] == 't'
object[key] = true
elsif object[key] == 'f'
object[key] = false
end
object
end

end
42 changes: 42 additions & 0 deletions lib/petshopserver/repos/cats_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Petshopserver
class CatsRepo

def self.all db
sql = %q[SELECT * FROM cats]
result = db.exec(sql)
result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') }
end

def self.all_by_shop db, id
sql = %q[SELECT * FROM cats WHERE "shopId" = $1]
result = db.exec(sql, [id])
result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') }

end

def self.find db, id
sql = %q[SELECT * FROM cats WHERE id = $1]
result = db.exec(sql, [id])
Petshopserver.boolean_type_cast(result.first, 'adopted')
#added class
end

def self.save db, cat_data
if cat_data["id"]
id = cat_data["id"].to_i
sql = %q[UPDATE cats SET adopted = $1 WHERE id = $2 RETURNING *]
result = db.exec(sql, [cat_data["adopted"], cat_data["id"]])
else
sql = %q[INSERT INTO cats ("shopId", name, "imageUrl", adopted) VALUES ($1,$2,$3,$4) RETURNING *]
result = db.exec(sql, [cat_data["shopId"], cat_data["name"], cat_data["imageUrl"],cat_data["adopted"]])
end
result.first
end

def self.destroy db, id
sql = %q[DELETE FROM cats WHERE id = $1]
db.exec(sql, [id])
end

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

def self.all db
sql = %q[SELECT * FROM dogs]
result = db.exec(sql)
result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') }
end

def self.all_by_shop db, id
sql = %q[SELECT * FROM dogs WHERE "shopId" = $1]
result = db.exec(sql, [id])
result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') }
end

def self.find db, id
sql = %q[SELECT * FROM dogs WHERE id = $1]
result = db.exec(sql, [id])
Petshopserver.boolean_type_cast(result.first, 'adopted')
end

def self.save db, dog_data
if dog_data["id"]
id = dog_data["id"].to_i
sql = %q[UPDATE dogs SET adopted = $1 WHERE id = $2 RETURNING *]
result = db.exec(sql, [dog_data["adopted"], dog_data["id"]])
else
sql = %q[INSERT INTO dogs ("shopId", name, "imageUrl", happiness, adopted) VALUES ($1,$2,$3,$4, $5) RETURNING *]
result = db.exec(sql, [dog_data["shopId"], dog_data["name"], dog_data["imageUrl"],dog_data["happiness"], dog_data["adopted"]])
end
result.first
end

def self.destroy db, id
sql = %q[DELETE FROM dogs WHERE id = $1]
db.exec(sql, [id])
end

end
end
37 changes: 37 additions & 0 deletions lib/petshopserver/repos/shops_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Petshopserver

class ShopsRepo
# 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, shop_id
sql = %q[SELECT * FROM shops WHERE id = $1]
result = db.exec(sql, [shop_id])
result.first
end

def self.find_by_name db, name
sql = %q[SELECT * FROM shops WHERE name = $1]
result = db.exec(sql, [name])
result.first
end

def self.all(db)
db.exec("SELECT * FROM shops").to_a
end

# save shop info
def self.save db, shop_data
sql = %q[INSERT INTO shops (name) VALUES ($1) RETURNING *]
result = db.exec(sql, [shop_data[:name]])
result.first
end

def self.destroy db, id
sql = %q[DELETE FROM shops WHERE id = $1]
db.exec(sql, [id])
end

end
end
58 changes: 58 additions & 0 deletions lib/petshopserver/repos/users_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Petshopserver

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])
result.first
end

# find user by username. Intended to be used when
# someone tries to sign in.
def self.find_by_username db, username
sql = %q[SELECT * FROM users WHERE username = $1]
result = db.exec(sql, [username])
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_cat db, user_id, cat_id
sql = %q[INSERT INTO userPets (user_id, cat_id) VALUES ($1, $2) RETURNING *]
sql1 = %q[UPDATE cats SET adopted = $1 WHERE id = $2 RETURNING *]
db.exec(sql1, [true, cat_id])
result = db.exec(sql, [user_id, cat_id])
result.first
end

def self.find_all_cats_by_user_id db, user_id
sql = %q[SELECT c.id, c."shopId", c.name, c."imageUrl", c.adopted FROM userPets u INNER JOIN cats c ON c.id = u.cat_id WHERE user_id = $1]
result = db.exec(sql, [user_id])
result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') }
end

def self.adopt_dog db, user_id, dog_id
sql = %q[INSERT INTO userPets (user_id, dog_id) VALUES ($1, $2) RETURNING *]
sql1 = %q[UPDATE dogs SET adopted = $1 WHERE id = $2 RETURNING *]
db.exec(sql1, [true, dog_id])
result = db.exec(sql, [user_id, dog_id])
result.first
end

def self.find_all_dogs_by_user_id db, user_id
sql = %q[SELECT d.id, d."shopId", d.name, d."imageUrl", d.happiness, d.adopted FROM userPets u INNER JOIN dogs d ON d.id = u.dog_id WHERE user_id = $1]
result = db.exec(sql, [user_id])
result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') }
end

end
end
Loading