Skip to content
Open
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rspec', '~> 2.14.1'
gem 'sinatra', '~> 1.4.5'
gem 'sinatra-contrib', '~> 1.4.2'
gem 'rest-client'
gem 'pg'

# Testing
gem 'pry-byebug'
2 changes: 2 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 Down Expand Up @@ -55,6 +56,7 @@ PLATFORMS
ruby

DEPENDENCIES
pg
pry-byebug
rest-client
rspec (~> 2.14.1)
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bundle exec rake db:create
bundle exec rake db:create_tables
bundle exec rake db:seed

bundle exec rspec

bundle exec rackup -p 4567
61 changes: 61 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

task :environment do
require './lib/petshop.rb'
end

task :console => :environment do
require 'irb'
ARGV.clear
IRB.start
end


namespace :db do

task :create do
`createdb petshop_dev`
`createdb petshop_test`
puts "Created."
end

task :drop do
`dropdb petshop_dev`
`dropdb petshop_test`
puts "Dropped."
end

task :create_tables => :environment do
db1 = PetShop.create_db_connection('petshop_test')
db2 = PetShop.create_db_connection('petshop_dev')
PetShop.create_tables(db1)
PetShop.create_tables(db2)
puts "Created tables."
end

task :drop_tables => :environment do
db1 = PetShop.create_db_connection('petshop_test')
db2 = PetShop.create_db_connection('petshop_dev')
PetShop.drop_tables(db1)
PetShop.drop_tables(db2)
puts "Dropped tables."
end

task :clear => :environment do
# The test db clears all the time, so there's no point in doing it here.
db = PetShop.create_db_connection('petshop_dev')
db1 = PetShop.create_db_connection('petshop_test')
PetShop.drop_tables(db)
PetShop.drop_tables(db1)
PetShop.create_tables(db)
PetShop.create_tables(db1)
puts "Cleared tables."
end

task :seed => :environment do
db = PetShop.create_db_connection('petshop_dev')
db1 = PetShop.create_db_connection('petshop_test')
PetShop.seed_db(db)
puts "Seeded."
end

end
8 changes: 8 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require './server'

# set up db tables
db = PetShop.create_db_connection 'petshop_dev'
PetShop.create_tables db

# run web app
run PetShop::Server
98 changes: 98 additions & 0 deletions lib/petshop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'pg'

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

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

def self.create_tables(db)
db.exec <<-SQL
CREATE TABLE IF NOT EXISTS shops(
id SERIAL PRIMARY KEY,
name VARCHAR
);
CREATE TABLE IF NOT EXISTS owners(
id SERIAL PRIMARY KEY,
username VARCHAR,
password VARCHAR
);
CREATE TABLE IF NOT EXISTS cats(
id SERIAL PRIMARY KEY,
name VARCHAR,
shopid INT references shops (id),
owner_id INT references owners (id),
imageurl TEXT,
adopted VARCHAR
);
CREATE TABLE IF NOT EXISTS dogs(
id SERIAL PRIMARY KEY,
name VARCHAR,
shopid INT references shops (id),
owner_id INT references owners (id),
imageurl TEXT,
adopted VARCHAR
);

SQL
end

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

def self.seed_db(db)
db.exec <<-SQL
-- Insert Shops
INSERT INTO shops (name) values ('Store Prime');
INSERT INTO shops (name) values ('Canineite');
INSERT INTO shops (name) values ('Poochy Paradise');
SQL

db.exec <<-SQL
-- Insert Owners
INSERT INTO owners (username, password) values ('Jason', 'pass');
INSERT INTO owners (username, password) values ('Alex', 'pass');
SQL

db.exec <<-SQL
-- Insert Dogs
INSERT INTO dogs (name, shopid, imageurl, adopted) values
('Sam The Pup', 2, 'https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSMp3Zb-s_O38HlY4x9xPI0k0cJ8_DtEH4zJ4mKCt_4sGapVOOYrw', 'false');
INSERT INTO dogs (name, shopid, imageurl, adopted) values
('Bark', 1, 'http://animal-backgrounds.com/download/1092/1920x1200/crop/cute-and-funny-small-dog-wallpaper-1920x1200.jpg', 'false');
INSERT INTO dogs (name, shopid, imageurl, adopted) values
('Woof', 3, 'http://www.bobtail-chiens.com/medias/images/24-04-2011-gustave.jpg', 'false');
INSERT INTO dogs (name, shopid, imageurl, adopted) values
('Growl', 2, 'http://www.smalldogbreedsdb.com/wp-content/uploads/EasyRotatorStorage/user-content/erc_66_1381848811/content/assets/Beagle-3.jpg-0.jpg', 'false');
SQL

db.exec <<-SQL
-- Cats
INSERT INTO cats (name, shopid, imageurl, adopted) values
('Lammie', 1, 'https://c2.staticflickr.com/8/7418/9738381563_0ac7da6cdc_b.jpg', 'false');
INSERT INTO cats (name, shopid, imageurl, adopted) values
('Joja', 2, 'http://wallpaper-download.net/wallpapers/animal-wallpapers-african-lion-king-wallpaper-31870.jpg', 'false');
INSERT INTO cats (name, shopid, imageurl, adopted) values
('Biboo', 3, 'http://alabamapioneers.com/ap2/wp-content/uploads/2014/06/black_panther-t2.jpg', 'false');
SQL
end
end

require_relative 'petshop/shop_repo'
require_relative 'petshop/owner_repo'
require_relative 'petshop/dog_repo'
require_relative 'petshop/cat_repo'
56 changes: 56 additions & 0 deletions lib/petshop/cat_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'pry-byebug'

module PetShop
class CatRepo

def self.all(db)
result = db.exec("SELECT * FROM cats").to_a
end

def self.find(db, cat_id)
cat = db.exec("SELECT * FROM cats WHERE id=$1", [cat_id]).first
end

def self.find_all_by_shop(db, shopid)
cats = db.exec("SELECT * FROM cats WHERE shopid=$1", [shopid]).to_a
end

def self.find_all_by_owner(db, owner_id)
cats = db.exec("SELECT * FROM cats WHERE owner_id=$1", [owner_id]).to_a
end

def self.save(db, cat_data)
if cat_data['id'] # Update an Existing Cat

# Ensure cat exists
cat = find(db, cat_data['id'])
raise "A valid cat_id is required." if cat.nil?

owner = find(db, cat_data['owner_id'])
raise "A valid owner_id is required." if owner.nil?

#Assign owner
if cat_data['adopted'] && cat_data['name']
result = db.exec("UPDATE cats SET owner_id = $2, adopted = $3, name = $4 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['adopted'], cat_data['name']])
elsif cat_data['name']
result = db.exec("UPDATE cats SET owner_id = $2, name = $3 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['name']])
elsif cat_data['adopted']
result = db.exec("UPDATE cats SET owner_id = $2, adopted = $3 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['adopted']])
end
self.find(db, cat_data['id'])
else # Create a New Cat
raise "name is required." if cat_data['name'].nil? || cat_data['name'] == ''
raise "shopid is required." if cat_data['shopid'].nil? || cat_data['shopid'] == ''
raise "imageurl is required." if cat_data['imageurl'].nil? || cat_data['imageurl'] == ''
result = db.exec("INSERT INTO cats (name, shopid, imageurl, adopted) values ($1, $2, $3, 'false') RETURNING id", [cat_data['name'], cat_data['shopid'], cat_data['imageurl']])
self.find(db, result.entries.first['id'])
end
end

def self.destroy(db, cat_id)
# Delete SQL statement
db.exec("DELETE FROM cats WHERE id = $1", [cat_id])
end

end
end
54 changes: 54 additions & 0 deletions lib/petshop/dog_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'pry-byebug'

module PetShop
class DogRepo

def self.all(db)
result = db.exec("SELECT * FROM dogs").to_a
end

def self.find(db, dog_id)
dog = db.exec("SELECT * FROM dogs WHERE id=$1", [dog_id]).first
end

def self.find_all_by_shop(db, shopid)
dogs = db.exec("SELECT * FROM dogs WHERE shopid=$1", [shopid]).to_a
end

def self.find_all_by_owner(db, owner_id)
dogs = db.exec("SELECT * FROM dogs WHERE owner_id=$1", [owner_id]).to_a
end

def self.save(db, dog_data)
if dog_data['id'] # Update

# Ensure dog exists
dog = find(db, dog_data['id'])
raise "A valid dog_id is required." if dog.nil?
raise "A valid owner_id is required." if dog.nil?

#Assign owner
if dog_data['adopted'] && dog_data['name']
result = db.exec("UPDATE dogs SET owner_id = $2, adopted = $3, name = $4 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted'], dog_data['name']])
elsif dog_data['name']
result = db.exec("UPDATE dogs SET owner_id = $2, name = $3 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['name']])
elsif dog_data['adopted']
result = db.exec("UPDATE dogs SET owner_id = $2, adopted = $3 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted']])
end
self.find(db, dog_data['id'])
else
raise "name is required." if dog_data['name'].nil? || dog_data['name'] == ''
raise "shopid is required." if dog_data['shopid'].nil? || dog_data['shopid'] == ''
raise "imageurl is required." if dog_data['imageurl'].nil? || dog_data['imageurl'] == ''
result = db.exec("INSERT INTO dogs (name, shopid, imageurl, adopted) values ($1, $2, $3, 'false') RETURNING id", [dog_data['name'], dog_data['shopid'], dog_data['imageurl']])
self.find(db, result.entries.first['id'])
end
end

def self.destroy(db, dog_id)
# Delete SQL statement
db.exec("DELETE FROM dogs WHERE id = $1", [dog_id])
end

end
end
55 changes: 55 additions & 0 deletions lib/petshop/owner_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'pry-byebug'

module PetShop
class OwnerRepo

def self.all(db)
# Other code should not have to deal with the PG:Result.
# Therefore, convert the results into a plain array.
result = db.exec("SELECT * FROM owners").to_a
end

def self.find(db, owner_id)
owner = db.exec("SELECT * FROM owners WHERE id=$1", [owner_id]).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 owners WHERE username = $1]
result = db.exec(sql, [username])
result.first
end

def self.save(db, owner_data)
if owner_data['id'] # Edit owner

# Ensure owner exists
owner = find(db, owner_data['id'])
raise "A valid owner id is required." if owner.nil?

if owner_data['username'] # Update Owner Name
result = db.exec("UPDATE owners SET username = $2 WHERE id = $1", [owner_data['id'], owner_data['username']])
end

if owner_data['password'] # Update Owner Password
result = db.exec("UPDATE owners SET password = $2 WHERE id = $1", [owner_data['id'], owner_data['password']])
end
self.find(db, owner_data['id'])
else
raise "username is required." if owner_data['username'].nil? || owner_data['username'] == ''
raise "password is required." if owner_data['password'].nil? || owner_data['password'] == ''
result = db.exec("INSERT INTO owners (username, password) VALUES ($1, $2) RETURNING *", [owner_data['username'], owner_data['password']])
self.find(db, result.entries.first['id'])
end
end

def self.destroy(db, owner_id)
# Delete SQL statement
db.exec("UPDATE cats SET owner_id = null, adopted = false WHERE owner_id = $1", [owner_id])
db.exec("UPDATE dogs SET owner_id = null, adopted = false WHERE owner_id = $1", [owner_id])
db.exec("DELETE FROM owners WHERE id = $1", [owner_id])
end

end
end
Loading