From 7faba317b06d3b8d735c2d1e7d21790d604c164a Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Thu, 11 Dec 2014 21:17:56 +0000 Subject: [PATCH 01/12] Create DB, Create Tables, Seed tables --- Gemfile | 1 + Gemfile.lock | 2 + Rakefile | 51 ++++++++++++++++++++ lib/petshop.rb | 95 +++++++++++++++++++++++++++++++++++++ lib/petshop/petshop_repo.rb | 62 ++++++++++++++++++++++++ server.rb | 2 + 6 files changed, 213 insertions(+) create mode 100644 Rakefile create mode 100644 lib/petshop.rb create mode 100644 lib/petshop/petshop_repo.rb diff --git a/Gemfile b/Gemfile index 66526d43..1446f3d9 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index bbeebb1e..f4cf3bf0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -55,6 +56,7 @@ PLATFORMS ruby DEPENDENCIES + pg pry-byebug rest-client rspec (~> 2.14.1) diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..f94b2fee --- /dev/null +++ b/Rakefile @@ -0,0 +1,51 @@ + +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` + puts "Created." + end + + task :drop do + `dropdb petshop_dev` + puts "Dropped." + end + + task :create_tables => :environment do + db2 = Petshop.create_db_connection('petshop_dev') + Petshop.create_tables(db2) + puts "Created tables." + end + + task :drop_tables => :environment do + db2 = Petshop.create_db_connection('petshop_dev') + 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') + Petshop.drop_tables(db) + Petshop.create_tables(db) + puts "Cleared tables." + end + + task :seed => :environment do + db = Petshop.create_db_connection('petshop_dev') + Petshop.seed_db(db) + puts "Seeded." + end + +end diff --git a/lib/petshop.rb b/lib/petshop.rb new file mode 100644 index 00000000..1fa18560 --- /dev/null +++ b/lib/petshop.rb @@ -0,0 +1,95 @@ +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, + title VARCHAR + ); + CREATE TABLE IF NOT EXISTS owners( + id SERIAL PRIMARY KEY, + name VARCHAR, + password VARCHAR + ); + CREATE TABLE IF NOT EXISTS cats( + id SERIAL PRIMARY KEY, + name VARCHAR, + shop_id INT references shops (id), + owner_id INT references owners (id), + imageUrl TEXT, + adopted_status BOOLEAN + ); + CREATE TABLE IF NOT EXISTS dogs( + id SERIAL PRIMARY KEY, + name VARCHAR, + shop_id INT references shops (id), + owner_id INT references owners (id), + imageUrl TEXT, + adopted_status BOOLEAN + ); + + 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 (title) values ('Store Prime'); + INSERT INTO shops (title) values ('Canineite'); + INSERT INTO shops (title) values ('Poochy Paradise'); + SQL + + db.exec <<-SQL + -- Insert Owners + INSERT INTO owners (name, password) values ('Jason', 'pass'); + INSERT INTO owners (name, password) values ('Alex', 'pass'); + SQL + + db.exec <<-SQL + -- Insert Dogs + INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values + ('Sam The Pup', 2, 'https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSMp3Zb-s_O38HlY4x9xPI0k0cJ8_DtEH4zJ4mKCt_4sGapVOOYrw', false); + INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values + ('Bark', 1, 'http://animal-backgrounds.com/download/1092/1920x1200/crop/cute-and-funny-small-dog-wallpaper-1920x1200.jpg', false); + INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values + ('Woof', 3, 'http://www.bobtail-chiens.com/medias/images/24-04-2011-gustave.jpg', false); + INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) 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, shop_id, imageUrl, adopted_status) values + ('Lammie', 1, 'https://c2.staticflickr.com/8/7418/9738381563_0ac7da6cdc_b.jpg', false); + INSERT INTO cats (name, shop_id, imageUrl, adopted_status) values + ('Joja', 2, 'http://wallpaper-download.net/wallpapers/animal-wallpapers-african-lion-king-wallpaper-31870.jpg', false); + INSERT INTO cats (name, shop_id, imageUrl, adopted_status) values + ('Biboo', 3, 'http://alabamapioneers.com/ap2/wp-content/uploads/2014/06/black_panther-t2.jpg', false); + SQL + end +end + +require_relative 'petshop/petshop_repo' diff --git a/lib/petshop/petshop_repo.rb b/lib/petshop/petshop_repo.rb new file mode 100644 index 00000000..91b0e2ab --- /dev/null +++ b/lib/petshop/petshop_repo.rb @@ -0,0 +1,62 @@ +require 'pry-byebug' + +module Songify + class AlbumRepo + + def self.all(db) + # Other code should not have to deal with the PG:Result. + # Therefore, convert the results into a plain array. + # TODO: This has to be a JOIN table + result = db.exec("SELECT * FROM albums").to_a + end + + def self.find(db, album_id) + # TODO: This has to be a JOIN table + sql_all_genres_for_album = %Q[ + SELECT + g.id, + g.name + FROM album_genres ag + JOIN genres g + ON g.id = ag.genre_id + WHERE ag.album_id = $1 + ] + + album = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + return album if album.nil? + + album['genres'] = db.exec(sql_all_genres_for_album, [album_id]).to_a + album + end + + def self.save(db, album_data) + if album_data['id'] + + # Ensure album exists + album = find(db, album_data['id']) + raise "A valid album_id is required." if album.nil? + + result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) + self.find(db, album_data['id']) + else + raise "title is required." if album_data['title'].nil? || album_data['title'] == '' + result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) + album_data['id'] = result.entries.first['id'] + if album_data['genre_ids'] + album_data['genre_ids'].each do |genre_id| + result = db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre_id]) + end + end + album_data + end + end + + def self.destroy(db, album_id) + # Delete SQL statement + db.exec("DELETE FROM songs WHERE album_id = $1", [album_id]) + db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id]) + db.exec("DELETE FROM albums WHERE id = $1", [album_id]) + end + + end +end diff --git a/server.rb b/server.rb index 07df95f4..d0a3f070 100644 --- a/server.rb +++ b/server.rb @@ -3,6 +3,8 @@ require 'rest-client' require 'json' +set :bind, '0.0.0.0' # This is needed for Vagrant + # # # This is our only html view... # From 15d1f4308397a04238c98c465ec69f774973ec66 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Thu, 11 Dec 2014 21:24:00 +0000 Subject: [PATCH 02/12] Add Repo destination for all tables --- lib/petshop/{petshop_repo.rb => cat_repo.rb} | 29 ++++-------- lib/petshop/dog_repo.rb | 49 ++++++++++++++++++++ lib/petshop/owner_repo.rb | 49 ++++++++++++++++++++ lib/petshop/shop_repo.rb | 49 ++++++++++++++++++++ 4 files changed, 155 insertions(+), 21 deletions(-) rename lib/petshop/{petshop_repo.rb => cat_repo.rb} (70%) create mode 100644 lib/petshop/dog_repo.rb create mode 100644 lib/petshop/owner_repo.rb create mode 100644 lib/petshop/shop_repo.rb diff --git a/lib/petshop/petshop_repo.rb b/lib/petshop/cat_repo.rb similarity index 70% rename from lib/petshop/petshop_repo.rb rename to lib/petshop/cat_repo.rb index 91b0e2ab..36dec23b 100644 --- a/lib/petshop/petshop_repo.rb +++ b/lib/petshop/cat_repo.rb @@ -1,36 +1,23 @@ require 'pry-byebug' -module Songify - class AlbumRepo +module Petshop + class ShopRepo def self.all(db) # Other code should not have to deal with the PG:Result. # Therefore, convert the results into a plain array. # TODO: This has to be a JOIN table - result = db.exec("SELECT * FROM albums").to_a + result = db.exec("SELECT * FROM shops").to_a end - def self.find(db, album_id) + def self.find(db, shop_id) # TODO: This has to be a JOIN table - sql_all_genres_for_album = %Q[ - SELECT - g.id, - g.name - FROM album_genres ag - JOIN genres g - ON g.id = ag.genre_id - WHERE ag.album_id = $1 - ] - - album = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first - return album if album.nil? - - album['genres'] = db.exec(sql_all_genres_for_album, [album_id]).to_a - album + + shop = db.exec("SELECT * FROM shops WHERE id=$1", [shop_id]).first end - def self.save(db, album_data) - if album_data['id'] + def self.save(db, shop_data) + if shop_data['id'] # Edit Shop # Ensure album exists album = find(db, album_data['id']) diff --git a/lib/petshop/dog_repo.rb b/lib/petshop/dog_repo.rb new file mode 100644 index 00000000..36dec23b --- /dev/null +++ b/lib/petshop/dog_repo.rb @@ -0,0 +1,49 @@ +require 'pry-byebug' + +module Petshop + class ShopRepo + + def self.all(db) + # Other code should not have to deal with the PG:Result. + # Therefore, convert the results into a plain array. + # TODO: This has to be a JOIN table + result = db.exec("SELECT * FROM shops").to_a + end + + def self.find(db, shop_id) + # TODO: This has to be a JOIN table + + shop = db.exec("SELECT * FROM shops WHERE id=$1", [shop_id]).first + end + + def self.save(db, shop_data) + if shop_data['id'] # Edit Shop + + # Ensure album exists + album = find(db, album_data['id']) + raise "A valid album_id is required." if album.nil? + + result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) + self.find(db, album_data['id']) + else + raise "title is required." if album_data['title'].nil? || album_data['title'] == '' + result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) + album_data['id'] = result.entries.first['id'] + if album_data['genre_ids'] + album_data['genre_ids'].each do |genre_id| + result = db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre_id]) + end + end + album_data + end + end + + def self.destroy(db, album_id) + # Delete SQL statement + db.exec("DELETE FROM songs WHERE album_id = $1", [album_id]) + db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id]) + db.exec("DELETE FROM albums WHERE id = $1", [album_id]) + end + + end +end diff --git a/lib/petshop/owner_repo.rb b/lib/petshop/owner_repo.rb new file mode 100644 index 00000000..36dec23b --- /dev/null +++ b/lib/petshop/owner_repo.rb @@ -0,0 +1,49 @@ +require 'pry-byebug' + +module Petshop + class ShopRepo + + def self.all(db) + # Other code should not have to deal with the PG:Result. + # Therefore, convert the results into a plain array. + # TODO: This has to be a JOIN table + result = db.exec("SELECT * FROM shops").to_a + end + + def self.find(db, shop_id) + # TODO: This has to be a JOIN table + + shop = db.exec("SELECT * FROM shops WHERE id=$1", [shop_id]).first + end + + def self.save(db, shop_data) + if shop_data['id'] # Edit Shop + + # Ensure album exists + album = find(db, album_data['id']) + raise "A valid album_id is required." if album.nil? + + result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) + self.find(db, album_data['id']) + else + raise "title is required." if album_data['title'].nil? || album_data['title'] == '' + result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) + album_data['id'] = result.entries.first['id'] + if album_data['genre_ids'] + album_data['genre_ids'].each do |genre_id| + result = db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre_id]) + end + end + album_data + end + end + + def self.destroy(db, album_id) + # Delete SQL statement + db.exec("DELETE FROM songs WHERE album_id = $1", [album_id]) + db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id]) + db.exec("DELETE FROM albums WHERE id = $1", [album_id]) + end + + end +end diff --git a/lib/petshop/shop_repo.rb b/lib/petshop/shop_repo.rb new file mode 100644 index 00000000..36dec23b --- /dev/null +++ b/lib/petshop/shop_repo.rb @@ -0,0 +1,49 @@ +require 'pry-byebug' + +module Petshop + class ShopRepo + + def self.all(db) + # Other code should not have to deal with the PG:Result. + # Therefore, convert the results into a plain array. + # TODO: This has to be a JOIN table + result = db.exec("SELECT * FROM shops").to_a + end + + def self.find(db, shop_id) + # TODO: This has to be a JOIN table + + shop = db.exec("SELECT * FROM shops WHERE id=$1", [shop_id]).first + end + + def self.save(db, shop_data) + if shop_data['id'] # Edit Shop + + # Ensure album exists + album = find(db, album_data['id']) + raise "A valid album_id is required." if album.nil? + + result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) + self.find(db, album_data['id']) + else + raise "title is required." if album_data['title'].nil? || album_data['title'] == '' + result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) + album_data['id'] = result.entries.first['id'] + if album_data['genre_ids'] + album_data['genre_ids'].each do |genre_id| + result = db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre_id]) + end + end + album_data + end + end + + def self.destroy(db, album_id) + # Delete SQL statement + db.exec("DELETE FROM songs WHERE album_id = $1", [album_id]) + db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id]) + db.exec("DELETE FROM albums WHERE id = $1", [album_id]) + end + + end +end From b7c71b0df98a9fcf54ce96957d1196313a0c050a Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Thu, 11 Dec 2014 21:50:49 +0000 Subject: [PATCH 03/12] Add Owner Repo and Shop Repo Methods find, save, destory --- lib/petshop/owner_repo.rb | 52 +++++++++++++++++++-------------------- lib/petshop/shop_repo.rb | 30 +++++++++------------- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/lib/petshop/owner_repo.rb b/lib/petshop/owner_repo.rb index 36dec23b..ec640021 100644 --- a/lib/petshop/owner_repo.rb +++ b/lib/petshop/owner_repo.rb @@ -1,48 +1,46 @@ require 'pry-byebug' module Petshop - class ShopRepo + 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. - # TODO: This has to be a JOIN table - result = db.exec("SELECT * FROM shops").to_a + result = db.exec("SELECT * FROM owners").to_a end - def self.find(db, shop_id) - # TODO: This has to be a JOIN table - - shop = db.exec("SELECT * FROM shops WHERE id=$1", [shop_id]).first + def self.find(db, owner_id) + owner = db.exec("SELECT * FROM owners WHERE id=$1", [owner_id]).first end - def self.save(db, shop_data) - if shop_data['id'] # Edit Shop + def self.save(db, owner_data) + if owner_data['id'] # Edit owner - # Ensure album exists - album = find(db, album_data['id']) - raise "A valid album_id is required." if album.nil? + # Ensure owner exists + owner = find(db, owner_data['id']) + raise "A valid owner_id is required." if owner.nil? - result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) - self.find(db, album_data['id']) - else - raise "title is required." if album_data['title'].nil? || album_data['title'] == '' - result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) - album_data['id'] = result.entries.first['id'] - if album_data['genre_ids'] - album_data['genre_ids'].each do |genre_id| - result = db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre_id]) - end + if owner_data['name'] # Update Owner Name + result = db.exec("UPDATE owners SET name = $2 WHERE id = $1", [owner_data['id'], owner_data['name']]) end - album_data + + 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 "name is required." if owner_data['name'].nil? || owner_data['name'] == '' + raise "password is required." if owner_data['password'].nil? || owner_data['password'] == '' + result = db.exec("INSERT INTO owners (name, password) VALUES ($1, $2) RETURNING *", [owner_data['name'], owner_data['password']]) + self.find(db, result.entries.first['id']) end end - def self.destroy(db, album_id) + def self.destroy(db, owner_id) # Delete SQL statement - db.exec("DELETE FROM songs WHERE album_id = $1", [album_id]) - db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id]) - db.exec("DELETE FROM albums WHERE id = $1", [album_id]) + db.exec("UPDATE cats SET owner_id = null, adopted_status = false WHERE owner_id = $1", [owner_id]) + db.exec("UPDATE dogs SET owner_id = null, adopted_status = false WHERE owner_id = $1", [owner_id]) + db.exec("DELETE FROM owners WHERE id = $1", [owner_id]) end end diff --git a/lib/petshop/shop_repo.rb b/lib/petshop/shop_repo.rb index 36dec23b..bff82929 100644 --- a/lib/petshop/shop_repo.rb +++ b/lib/petshop/shop_repo.rb @@ -19,30 +19,24 @@ def self.find(db, shop_id) def self.save(db, shop_data) if shop_data['id'] # Edit Shop - # Ensure album exists - album = find(db, album_data['id']) - raise "A valid album_id is required." if album.nil? + # Ensure shop exists + shop = find(db, shop_data['id']) + raise "A valid shop_id is required." if shop.nil? - result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) - self.find(db, album_data['id']) + result = db.exec("UPDATE shops SET name = $2 WHERE id = $1", [shop_data['id'], shop_data['name']]) + self.find(db, shop_data['id']) else - raise "title is required." if album_data['title'].nil? || album_data['title'] == '' - result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) - album_data['id'] = result.entries.first['id'] - if album_data['genre_ids'] - album_data['genre_ids'].each do |genre_id| - result = db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre_id]) - end - end - album_data + raise "name is required." if shop_data['name'].nil? || shop_data['name'] == '' + result = db.exec("INSERT INTO shops (name) VALUES ($1) RETURNING *", [shop_data['name']]) + self.find(db, result.entries.first['id']) end end - def self.destroy(db, album_id) + def self.destroy(db, shop_id) # Delete SQL statement - db.exec("DELETE FROM songs WHERE album_id = $1", [album_id]) - db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id]) - db.exec("DELETE FROM albums WHERE id = $1", [album_id]) + db.exec("DELETE FROM cats WHERE shop_id = $1", [shop_id]) + db.exec("DELETE FROM dogs WHERE shop_id = $1", [shop_id]) + db.exec("DELETE FROM shops WHERE id = $1", [shop_id]) end end From fccd3c4aff28e079a0b4fb82e028081bc4677e4a Mon Sep 17 00:00:00 2001 From: Jason Hatchett Date: Thu, 11 Dec 2014 15:51:59 -0600 Subject: [PATCH 04/12] Add dog and cat repo validation --- lib/petshop/cat_repo.rb | 47 +++++++++++++++++++---------------------- lib/petshop/dog_repo.rb | 45 ++++++++++++++++++--------------------- 2 files changed, 43 insertions(+), 49 deletions(-) diff --git a/lib/petshop/cat_repo.rb b/lib/petshop/cat_repo.rb index 36dec23b..ceb8aa67 100644 --- a/lib/petshop/cat_repo.rb +++ b/lib/petshop/cat_repo.rb @@ -1,49 +1,46 @@ require 'pry-byebug' module Petshop - class ShopRepo + class CatsRepo def self.all(db) # Other code should not have to deal with the PG:Result. # Therefore, convert the results into a plain array. # TODO: This has to be a JOIN table - result = db.exec("SELECT * FROM shops").to_a + result = db.exec("SELECT * FROM cats").to_a end - def self.find(db, shop_id) + def self.find(db, cat_id) # TODO: This has to be a JOIN table - shop = db.exec("SELECT * FROM shops WHERE id=$1", [shop_id]).first + shop = db.exec("SELECT * FROM cats WHERE id=$1", [cat_id]).first end - def self.save(db, shop_data) - if shop_data['id'] # Edit Shop + def self.save(db, cat_data) + if cat_data['id'] # Update - # Ensure album exists - album = find(db, album_data['id']) - raise "A valid album_id is required." if album.nil? + # Ensure cat exists + cat = find(db, cat_data['id']) + raise "A valid cat_id is required." if cat.nil? + raise "A valid owner_id is required." if cat.nil? - result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) - self.find(db, album_data['id']) + #Assign owner + result = db.exec("UPDATE cats SET owner_id = $2, adopted_status = $3 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['adopted_status']]) + self.find(db, cat_data['id']) else - raise "title is required." if album_data['title'].nil? || album_data['title'] == '' - result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) - album_data['id'] = result.entries.first['id'] - if album_data['genre_ids'] - album_data['genre_ids'].each do |genre_id| - result = db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre_id]) - end - end - album_data + raise "name is required." if cat_data['name'].nil? || cat_data['name'] == '' + raise "shop_id is required." if cat_data['shop_id'].nil? || cat_data['shop_id'] == '' + raise "imageUrl is required." if cat_data['imageUrl'].nil? || cat_data['imageUrl'] == '' + result = db.exec("INSERT INTO cats (name, shop_id, imageUrl, adopted_status) values ($1, $2, $3, false); RETURNING id", [dog_data['id'], dog_data['shop_id'], dog_data['imageUrl']]) + cat_data['id'] = result.entries.first['id'] + cat_data end end - def self.destroy(db, album_id) + def self.destroy(db, cat_id) # Delete SQL statement - db.exec("DELETE FROM songs WHERE album_id = $1", [album_id]) - db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id]) - db.exec("DELETE FROM albums WHERE id = $1", [album_id]) + db.exec("DELETE FROM cats WHERE id = $1", [cat_id]) end end -end +end \ No newline at end of file diff --git a/lib/petshop/dog_repo.rb b/lib/petshop/dog_repo.rb index 36dec23b..7f92052f 100644 --- a/lib/petshop/dog_repo.rb +++ b/lib/petshop/dog_repo.rb @@ -1,48 +1,45 @@ require 'pry-byebug' module Petshop - class ShopRepo + class DogRepo def self.all(db) # Other code should not have to deal with the PG:Result. # Therefore, convert the results into a plain array. # TODO: This has to be a JOIN table - result = db.exec("SELECT * FROM shops").to_a + result = db.exec("SELECT * FROM dogs").to_a end - def self.find(db, shop_id) + def self.find(db, dog_id) # TODO: This has to be a JOIN table - shop = db.exec("SELECT * FROM shops WHERE id=$1", [shop_id]).first + shop = db.exec("SELECT * FROM dogs WHERE id=$1", [dog_id]).first end - def self.save(db, shop_data) - if shop_data['id'] # Edit Shop + def self.save(db, dog_data) + if dog_data['id'] # Update - # Ensure album exists - album = find(db, album_data['id']) - raise "A valid album_id is required." if album.nil? + # 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? - result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) - self.find(db, album_data['id']) + #Assign owner + result = db.exec("UPDATE dogs SET owner_id = $2, adopted_status = $3 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted_status']]) + self.find(db, dog_data['id']) else - raise "title is required." if album_data['title'].nil? || album_data['title'] == '' - result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) - album_data['id'] = result.entries.first['id'] - if album_data['genre_ids'] - album_data['genre_ids'].each do |genre_id| - result = db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre_id]) - end - end - album_data + raise "name is required." if dog_data['name'].nil? || dog_data['name'] == '' + raise "shop_id is required." if dog_data['shop_id'].nil? || dog_data['shop_id'] == '' + raise "imageUrl is required." if dog_data['imageUrl'].nil? || dog_data['imageUrl'] == '' + result = db.exec("INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values ($1, $2, $3, false); RETURNING id", [dog_data['id'], dog_data['shop_id'], dog_data['imageUrl']]) + dog_data['id'] = result.entries.first['id'] + dog_data end end - def self.destroy(db, album_id) + def self.destroy(db, dog_id) # Delete SQL statement - db.exec("DELETE FROM songs WHERE album_id = $1", [album_id]) - db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id]) - db.exec("DELETE FROM albums WHERE id = $1", [album_id]) + db.exec("DELETE FROM dogs WHERE id = $1", [dog_id]) end end From d45183ee1fb2ded9439a6b03ce51ef9ab4eefeb2 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Thu, 11 Dec 2014 21:55:57 +0000 Subject: [PATCH 05/12] Add (incomplete) rspec repos for shops, owners, pets --- spec/repos/owner_repo.rb | 93 ++++++++++++++++++++++++++++++++++++++++ spec/repos/pet_repo.rb | 93 ++++++++++++++++++++++++++++++++++++++++ spec/repos/shop_repo.rb | 93 ++++++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 13 ++++++ 4 files changed, 292 insertions(+) create mode 100644 spec/repos/owner_repo.rb create mode 100644 spec/repos/pet_repo.rb create mode 100644 spec/repos/shop_repo.rb create mode 100644 spec/spec_helper.rb diff --git a/spec/repos/owner_repo.rb b/spec/repos/owner_repo.rb new file mode 100644 index 00000000..5b77dfe2 --- /dev/null +++ b/spec/repos/owner_repo.rb @@ -0,0 +1,93 @@ +require 'spec_helper' + +describe Songify::SongRepo do + + def song_count + repo.all(db).count + end + + let(:repo) { Songify::SongRepo } + let(:db) { Songify.create_db_connection('songify_test') } + + before(:each) do + Songify.clear_db(db) + @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] + end + + it "gets all songs" do + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) + + songs = repo.all(db) + expect(songs).to be_a Array + expect(songs.count).to eq 2 + + titles = songs.map {|u| u['title'] } + expect(titles).to include "The Ally", "Barnway Blues" + end + + it "creates songs" do + expect(song_count).to eq 0 + + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + expect(song['id']).to_not be_nil + expect(song['album_id']).to_not be_nil + expect(song['title']).to eq "The Ally" + + # Check for persistence + expect(song_count).to eq 1 + + song_count_by_album = repo.songs_per_album(db) + song_count_by_album.each do |album_id| + expect(album_id['tracks'].to_i).to eq 1 + end + + song = repo.all(db).first + expect(song['title']).to eq "The Ally" + expect(song['id']).to_not be_nil + expect(song['album_id']).to_not be_nil + + end + + it "requires a title" do + expect { repo.save(db, {}) }.to raise_error {|e| + expect(e.message).to match /title/ + } + end + + it "requires an album id" do + expect { + repo.save(db, { 'title' => "The Ally" }) + } + .to raise_error {|e| + expect(e.message).to match /album_id/ + } + end + + it "requires an album id that exists" do + expect { + repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) + } + .to raise_error {|e| + expect(e.message).to match /album_id/ + } + end + + it "finds songs" do + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + retrieved_song = repo.find(db, song['id']) + expect(retrieved_song['title']).to eq "The Ally" + end + + it "updates songs" do + song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) + expect(song2['id']).to eq(song1['id']) + expect(song2['title']).to eq "Alicia" + + # Check for persistence + song3 = repo.find(db, song1['id']) + expect(song3['title']).to eq "Alicia" + end + +end diff --git a/spec/repos/pet_repo.rb b/spec/repos/pet_repo.rb new file mode 100644 index 00000000..5b77dfe2 --- /dev/null +++ b/spec/repos/pet_repo.rb @@ -0,0 +1,93 @@ +require 'spec_helper' + +describe Songify::SongRepo do + + def song_count + repo.all(db).count + end + + let(:repo) { Songify::SongRepo } + let(:db) { Songify.create_db_connection('songify_test') } + + before(:each) do + Songify.clear_db(db) + @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] + end + + it "gets all songs" do + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) + + songs = repo.all(db) + expect(songs).to be_a Array + expect(songs.count).to eq 2 + + titles = songs.map {|u| u['title'] } + expect(titles).to include "The Ally", "Barnway Blues" + end + + it "creates songs" do + expect(song_count).to eq 0 + + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + expect(song['id']).to_not be_nil + expect(song['album_id']).to_not be_nil + expect(song['title']).to eq "The Ally" + + # Check for persistence + expect(song_count).to eq 1 + + song_count_by_album = repo.songs_per_album(db) + song_count_by_album.each do |album_id| + expect(album_id['tracks'].to_i).to eq 1 + end + + song = repo.all(db).first + expect(song['title']).to eq "The Ally" + expect(song['id']).to_not be_nil + expect(song['album_id']).to_not be_nil + + end + + it "requires a title" do + expect { repo.save(db, {}) }.to raise_error {|e| + expect(e.message).to match /title/ + } + end + + it "requires an album id" do + expect { + repo.save(db, { 'title' => "The Ally" }) + } + .to raise_error {|e| + expect(e.message).to match /album_id/ + } + end + + it "requires an album id that exists" do + expect { + repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) + } + .to raise_error {|e| + expect(e.message).to match /album_id/ + } + end + + it "finds songs" do + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + retrieved_song = repo.find(db, song['id']) + expect(retrieved_song['title']).to eq "The Ally" + end + + it "updates songs" do + song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) + expect(song2['id']).to eq(song1['id']) + expect(song2['title']).to eq "Alicia" + + # Check for persistence + song3 = repo.find(db, song1['id']) + expect(song3['title']).to eq "Alicia" + end + +end diff --git a/spec/repos/shop_repo.rb b/spec/repos/shop_repo.rb new file mode 100644 index 00000000..5b77dfe2 --- /dev/null +++ b/spec/repos/shop_repo.rb @@ -0,0 +1,93 @@ +require 'spec_helper' + +describe Songify::SongRepo do + + def song_count + repo.all(db).count + end + + let(:repo) { Songify::SongRepo } + let(:db) { Songify.create_db_connection('songify_test') } + + before(:each) do + Songify.clear_db(db) + @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] + end + + it "gets all songs" do + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) + + songs = repo.all(db) + expect(songs).to be_a Array + expect(songs.count).to eq 2 + + titles = songs.map {|u| u['title'] } + expect(titles).to include "The Ally", "Barnway Blues" + end + + it "creates songs" do + expect(song_count).to eq 0 + + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + expect(song['id']).to_not be_nil + expect(song['album_id']).to_not be_nil + expect(song['title']).to eq "The Ally" + + # Check for persistence + expect(song_count).to eq 1 + + song_count_by_album = repo.songs_per_album(db) + song_count_by_album.each do |album_id| + expect(album_id['tracks'].to_i).to eq 1 + end + + song = repo.all(db).first + expect(song['title']).to eq "The Ally" + expect(song['id']).to_not be_nil + expect(song['album_id']).to_not be_nil + + end + + it "requires a title" do + expect { repo.save(db, {}) }.to raise_error {|e| + expect(e.message).to match /title/ + } + end + + it "requires an album id" do + expect { + repo.save(db, { 'title' => "The Ally" }) + } + .to raise_error {|e| + expect(e.message).to match /album_id/ + } + end + + it "requires an album id that exists" do + expect { + repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) + } + .to raise_error {|e| + expect(e.message).to match /album_id/ + } + end + + it "finds songs" do + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + retrieved_song = repo.find(db, song['id']) + expect(retrieved_song['title']).to eq "The Ally" + end + + it "updates songs" do + song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) + expect(song2['id']).to eq(song1['id']) + expect(song2['title']).to eq "Alicia" + + # Check for persistence + song3 = repo.find(db, song1['id']) + expect(song3['title']).to eq "Alicia" + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..b15d3bbc --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,13 @@ +require 'songify' + +RSpec.configure do |config| + config.treat_symbols_as_metadata_keys_with_true_values = true + config.run_all_when_everything_filtered = true + config.filter_run :focus + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = 'random' +end From d5a2422525f78cb7bd5f64cee2fc0085bc84253a Mon Sep 17 00:00:00 2001 From: Jason Hatchett Date: Thu, 11 Dec 2014 16:55:32 -0600 Subject: [PATCH 06/12] Tests for adding Dogs --- lib/petshop/cat_repo.rb | 9 ++-- lib/petshop/dog_repo.rb | 11 ++--- spec/repos/pet_repo.rb | 93 ------------------------------------ spec/repos/pet_repo_spec.rb | 95 +++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 104 deletions(-) delete mode 100644 spec/repos/pet_repo.rb create mode 100644 spec/repos/pet_repo_spec.rb diff --git a/lib/petshop/cat_repo.rb b/lib/petshop/cat_repo.rb index ceb8aa67..51a43e0c 100644 --- a/lib/petshop/cat_repo.rb +++ b/lib/petshop/cat_repo.rb @@ -4,18 +4,17 @@ module Petshop class CatsRepo def self.all(db) - # Other code should not have to deal with the PG:Result. - # Therefore, convert the results into a plain array. - # TODO: This has to be a JOIN table result = db.exec("SELECT * FROM cats").to_a end def self.find(db, cat_id) - # TODO: This has to be a JOIN table - shop = db.exec("SELECT * FROM cats WHERE id=$1", [cat_id]).first end + def self.find_all_by_shop(db, shop_id) + cats = db.exec("SELECT * FROM cats WHERE shop_id=$1", [shop_id]).to_a + end + def self.save(db, cat_data) if cat_data['id'] # Update diff --git a/lib/petshop/dog_repo.rb b/lib/petshop/dog_repo.rb index 7f92052f..06d6988a 100644 --- a/lib/petshop/dog_repo.rb +++ b/lib/petshop/dog_repo.rb @@ -4,16 +4,15 @@ module Petshop class DogRepo def self.all(db) - # Other code should not have to deal with the PG:Result. - # Therefore, convert the results into a plain array. - # TODO: This has to be a JOIN table result = db.exec("SELECT * FROM dogs").to_a end - def self.find(db, dog_id) - # TODO: This has to be a JOIN table + def self.find_by_id(db, dog_id) + dog = db.exec("SELECT * FROM dogs WHERE id=$1", [dog_id]).first + end - shop = db.exec("SELECT * FROM dogs WHERE id=$1", [dog_id]).first + def self.find_all_by_shop(db, shop_id) + dogs = db.exec("SELECT * FROM dogs WHERE shop_id=$1", [shop_id]).to_a end def self.save(db, dog_data) diff --git a/spec/repos/pet_repo.rb b/spec/repos/pet_repo.rb deleted file mode 100644 index 5b77dfe2..00000000 --- a/spec/repos/pet_repo.rb +++ /dev/null @@ -1,93 +0,0 @@ -require 'spec_helper' - -describe Songify::SongRepo do - - def song_count - repo.all(db).count - end - - let(:repo) { Songify::SongRepo } - let(:db) { Songify.create_db_connection('songify_test') } - - before(:each) do - Songify.clear_db(db) - @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] - end - - it "gets all songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) - - songs = repo.all(db) - expect(songs).to be_a Array - expect(songs.count).to eq 2 - - titles = songs.map {|u| u['title'] } - expect(titles).to include "The Ally", "Barnway Blues" - end - - it "creates songs" do - expect(song_count).to eq 0 - - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - expect(song['id']).to_not be_nil - expect(song['album_id']).to_not be_nil - expect(song['title']).to eq "The Ally" - - # Check for persistence - expect(song_count).to eq 1 - - song_count_by_album = repo.songs_per_album(db) - song_count_by_album.each do |album_id| - expect(album_id['tracks'].to_i).to eq 1 - end - - song = repo.all(db).first - expect(song['title']).to eq "The Ally" - expect(song['id']).to_not be_nil - expect(song['album_id']).to_not be_nil - - end - - it "requires a title" do - expect { repo.save(db, {}) }.to raise_error {|e| - expect(e.message).to match /title/ - } - end - - it "requires an album id" do - expect { - repo.save(db, { 'title' => "The Ally" }) - } - .to raise_error {|e| - expect(e.message).to match /album_id/ - } - end - - it "requires an album id that exists" do - expect { - repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) - } - .to raise_error {|e| - expect(e.message).to match /album_id/ - } - end - - it "finds songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - retrieved_song = repo.find(db, song['id']) - expect(retrieved_song['title']).to eq "The Ally" - end - - it "updates songs" do - song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) - expect(song2['id']).to eq(song1['id']) - expect(song2['title']).to eq "Alicia" - - # Check for persistence - song3 = repo.find(db, song1['id']) - expect(song3['title']).to eq "Alicia" - end - -end diff --git a/spec/repos/pet_repo_spec.rb b/spec/repos/pet_repo_spec.rb new file mode 100644 index 00000000..fed52038 --- /dev/null +++ b/spec/repos/pet_repo_spec.rb @@ -0,0 +1,95 @@ +require 'spec_helper' + +describe Petshop::DogRepo do + + def dog_count + repo.all(db).count + end + + let(:repo) { Petshop::DogRepo } + let(:db) { Petshop.create_db_connection('petshop_test') } + + before(:each) do + Petshop.clear_db(db) + @shop_id = Petshop::ShopRepo.save(db, { 'name' => "SuperShop" })['id'] + end + + xit "gets all dogs" do + dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Giles", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Goji", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Berry", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + + dogs = repo.all(db) + expect(songs).to be_a Array + expect(songs.count).to eq 4 + + names = dogs.map {|u| u['name'] } + expect(names).to include "Barnaby", "Giles", "Goji", "Berry" + end + + xit "creates dogs" do + expect(dog_count).to eq 0 + + dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + expect(dog['id']).to_not be_nil + expect(dog['shop_id']).to_not be_nil + expect(song['name']).to eq "Barnaby" + + # Check for persistence + expect(dog_count).to eq 1 + + dog_count_by_store = repo.find_all_by_shop(db) + dog_count_by_store.each do |dog| + expect(dog['id'].to_i).to eq 1 + end + + dog = repo.all(db).first + expect(dog['id']).to_not be_nil + expect(dog['shop_id']).to_not be_nil + expect(song['name']).to eq "Barnaby" + + end + + xit "requires a name" do + expect { repo.save(db, {}) }.to raise_error {|e| + expect(e.message).to match /name/ + } + end + + xit "requires an shop id" do + expect { + repo.save(db, { 'name' => "Barnaby" }) + } + .to raise_error {|e| + expect(e.message).to match /shop_id/ + } + end + + xit "requires an imageUrl" do + expect { + repo.save(db, { 'name' => "Barnaby", 'shop_id' => @shop_id }) + } + .to raise_error {|e| + expect(e.message).to match /imageUrl/ + } + end + + xit "finds dogs" do + dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + retrieved_dog = repo.find(db, dog['id']) + expect(retrieved_dog['name']).to eq "Barnaby" + end + + xit "updates dogs" do + dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + dog2 = repo.save(db, { 'id' => dog['id'], 'name' => "Funky" }) + expect(dog2['id']).to eq(dog['id']) + expect(dog2['name']).to eq "Funky" + + # Check for persistence + dog3 = repo.find(db, dog['id']) + expect(dog3['name']).to eq "Funky" + end + +end From f05a74951e725530af1a316bcaa66a8e1a1316a6 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Thu, 11 Dec 2014 22:56:12 +0000 Subject: [PATCH 07/12] Add Spec Test (All Disabled) for Shops and Owners --- Rakefile | 2 + lib/petshop.rb | 5 +- lib/petshop/shop_repo.rb | 6 +-- spec/repos/owner_repo.rb | 93 ----------------------------------- spec/repos/owner_repo_spec.rb | 77 +++++++++++++++++++++++++++++ spec/repos/pet_repo.rb | 93 ----------------------------------- spec/repos/shop_repo.rb | 93 ----------------------------------- spec/repos/shop_repo_spec.rb | 73 +++++++++++++++++++++++++++ spec/spec_helper.rb | 2 +- 9 files changed, 160 insertions(+), 284 deletions(-) delete mode 100644 spec/repos/owner_repo.rb create mode 100644 spec/repos/owner_repo_spec.rb delete mode 100644 spec/repos/pet_repo.rb delete mode 100644 spec/repos/shop_repo.rb create mode 100644 spec/repos/shop_repo_spec.rb diff --git a/Rakefile b/Rakefile index f94b2fee..69b00f71 100644 --- a/Rakefile +++ b/Rakefile @@ -14,11 +14,13 @@ 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 diff --git a/lib/petshop.rb b/lib/petshop.rb index 1fa18560..b735a9f0 100644 --- a/lib/petshop.rb +++ b/lib/petshop.rb @@ -92,4 +92,7 @@ def self.seed_db(db) end end -require_relative 'petshop/petshop_repo' +require_relative 'petshop/shop_repo' +require_relative 'petshop/owner_repo' +# require_relative 'petshop/dog_repo' +# require_relative 'petshop/cat_repo' diff --git a/lib/petshop/shop_repo.rb b/lib/petshop/shop_repo.rb index bff82929..1760538b 100644 --- a/lib/petshop/shop_repo.rb +++ b/lib/petshop/shop_repo.rb @@ -23,11 +23,11 @@ def self.save(db, shop_data) shop = find(db, shop_data['id']) raise "A valid shop_id is required." if shop.nil? - result = db.exec("UPDATE shops SET name = $2 WHERE id = $1", [shop_data['id'], shop_data['name']]) + result = db.exec("UPDATE shops SET title = $2 WHERE id = $1", [shop_data['id'], shop_data['title']]) self.find(db, shop_data['id']) else - raise "name is required." if shop_data['name'].nil? || shop_data['name'] == '' - result = db.exec("INSERT INTO shops (name) VALUES ($1) RETURNING *", [shop_data['name']]) + raise "title is required." if shop_data['title'].nil? || shop_data['title'] == '' + result = db.exec("INSERT INTO shops (title) VALUES ($1) RETURNING *", [shop_data['title']]) self.find(db, result.entries.first['id']) end end diff --git a/spec/repos/owner_repo.rb b/spec/repos/owner_repo.rb deleted file mode 100644 index 5b77dfe2..00000000 --- a/spec/repos/owner_repo.rb +++ /dev/null @@ -1,93 +0,0 @@ -require 'spec_helper' - -describe Songify::SongRepo do - - def song_count - repo.all(db).count - end - - let(:repo) { Songify::SongRepo } - let(:db) { Songify.create_db_connection('songify_test') } - - before(:each) do - Songify.clear_db(db) - @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] - end - - it "gets all songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) - - songs = repo.all(db) - expect(songs).to be_a Array - expect(songs.count).to eq 2 - - titles = songs.map {|u| u['title'] } - expect(titles).to include "The Ally", "Barnway Blues" - end - - it "creates songs" do - expect(song_count).to eq 0 - - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - expect(song['id']).to_not be_nil - expect(song['album_id']).to_not be_nil - expect(song['title']).to eq "The Ally" - - # Check for persistence - expect(song_count).to eq 1 - - song_count_by_album = repo.songs_per_album(db) - song_count_by_album.each do |album_id| - expect(album_id['tracks'].to_i).to eq 1 - end - - song = repo.all(db).first - expect(song['title']).to eq "The Ally" - expect(song['id']).to_not be_nil - expect(song['album_id']).to_not be_nil - - end - - it "requires a title" do - expect { repo.save(db, {}) }.to raise_error {|e| - expect(e.message).to match /title/ - } - end - - it "requires an album id" do - expect { - repo.save(db, { 'title' => "The Ally" }) - } - .to raise_error {|e| - expect(e.message).to match /album_id/ - } - end - - it "requires an album id that exists" do - expect { - repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) - } - .to raise_error {|e| - expect(e.message).to match /album_id/ - } - end - - it "finds songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - retrieved_song = repo.find(db, song['id']) - expect(retrieved_song['title']).to eq "The Ally" - end - - it "updates songs" do - song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) - expect(song2['id']).to eq(song1['id']) - expect(song2['title']).to eq "Alicia" - - # Check for persistence - song3 = repo.find(db, song1['id']) - expect(song3['title']).to eq "Alicia" - end - -end diff --git a/spec/repos/owner_repo_spec.rb b/spec/repos/owner_repo_spec.rb new file mode 100644 index 00000000..e1a48bb3 --- /dev/null +++ b/spec/repos/owner_repo_spec.rb @@ -0,0 +1,77 @@ +require 'spec_helper' + +describe Petshop::OwnerRepo do + + def owner_count + repo.all(db).count + end + + let(:repo) { Petshop::OwnerRepo } + let(:db) { Petshop.create_db_connection('petshop_test') } + + before(:each) do + Petshop.clear_db(db) + @owner_id1 = Petshop::OwnerRepo.save(db, { 'name' => "Giovanni", 'password' => 'Swordfish' })['id'] + @owner_id2 = Petshop::OwnerRepo.save(db, { 'name' => "Leonardo", 'password' => 'Swordfish' })['id'] + end + + xit "gets all owners" do + + owners = repo.all(db) + expect(owners).to be_a Array + expect(owners.count).to eq 2 + + names = owners.map {|u| u['name'] } + expect(names).to include "Leonardo", "Giovanni" + end + + xit "creates owners" do + expect(owner_count).to eq 2 + + owner = repo.save(db, { 'name' => "Brian", 'password' => 'puppyfan102' }) + expect(owner['id']).to_not be_nil + expect(owner['name']).to eq "Brian" + expect(owner['password']).to eq "puppyfan102" + + # Check for persistence + expect(owner_count).to eq 3 + + owner = repo.all(db).first + expect(owner['id']).to_not be_nil + expect(owner['name']).to eq "Giovanni" + expect(owner['password']).to eq "Swordfish" + + end + + xit "requires a name" do + expect { repo.save(db, {}) }.to raise_error {|e| + expect(e.message).to match /name/ + } + end + + xit "requires an owner id that exists" do + expect { + repo.save(db, { 'id' => 999, 'name' => "Mr FunGuy" }) + } + .to raise_error {|e| + expect(e.message).to match /owner id/ + } + end + + xit "finds owners" do + retrieved_owner = repo.find(db, @owner_id1) + expect(retrieved_owner['name']).to eq "Giovanni" + end + + xit "updates owners" do + owner1 = repo.save(db, { 'id' => @owner_id1, 'name' => "Billy Boy" }) + owner2 = repo.save(db, { 'id' => @owner_id1, 'password' => "bigDogsRCool14" }) + expect(owner2['id']).to eq(owner1['id']) + expect(owner2['password']).to eq "bigDogsRCool14" + + # Check for persistence + owner3 = repo.find(db, owner1['id']) + expect(owner3['name']).to eq "Billy Boy" + end + +end diff --git a/spec/repos/pet_repo.rb b/spec/repos/pet_repo.rb deleted file mode 100644 index 5b77dfe2..00000000 --- a/spec/repos/pet_repo.rb +++ /dev/null @@ -1,93 +0,0 @@ -require 'spec_helper' - -describe Songify::SongRepo do - - def song_count - repo.all(db).count - end - - let(:repo) { Songify::SongRepo } - let(:db) { Songify.create_db_connection('songify_test') } - - before(:each) do - Songify.clear_db(db) - @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] - end - - it "gets all songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) - - songs = repo.all(db) - expect(songs).to be_a Array - expect(songs.count).to eq 2 - - titles = songs.map {|u| u['title'] } - expect(titles).to include "The Ally", "Barnway Blues" - end - - it "creates songs" do - expect(song_count).to eq 0 - - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - expect(song['id']).to_not be_nil - expect(song['album_id']).to_not be_nil - expect(song['title']).to eq "The Ally" - - # Check for persistence - expect(song_count).to eq 1 - - song_count_by_album = repo.songs_per_album(db) - song_count_by_album.each do |album_id| - expect(album_id['tracks'].to_i).to eq 1 - end - - song = repo.all(db).first - expect(song['title']).to eq "The Ally" - expect(song['id']).to_not be_nil - expect(song['album_id']).to_not be_nil - - end - - it "requires a title" do - expect { repo.save(db, {}) }.to raise_error {|e| - expect(e.message).to match /title/ - } - end - - it "requires an album id" do - expect { - repo.save(db, { 'title' => "The Ally" }) - } - .to raise_error {|e| - expect(e.message).to match /album_id/ - } - end - - it "requires an album id that exists" do - expect { - repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) - } - .to raise_error {|e| - expect(e.message).to match /album_id/ - } - end - - it "finds songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - retrieved_song = repo.find(db, song['id']) - expect(retrieved_song['title']).to eq "The Ally" - end - - it "updates songs" do - song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) - expect(song2['id']).to eq(song1['id']) - expect(song2['title']).to eq "Alicia" - - # Check for persistence - song3 = repo.find(db, song1['id']) - expect(song3['title']).to eq "Alicia" - end - -end diff --git a/spec/repos/shop_repo.rb b/spec/repos/shop_repo.rb deleted file mode 100644 index 5b77dfe2..00000000 --- a/spec/repos/shop_repo.rb +++ /dev/null @@ -1,93 +0,0 @@ -require 'spec_helper' - -describe Songify::SongRepo do - - def song_count - repo.all(db).count - end - - let(:repo) { Songify::SongRepo } - let(:db) { Songify.create_db_connection('songify_test') } - - before(:each) do - Songify.clear_db(db) - @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] - end - - it "gets all songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) - - songs = repo.all(db) - expect(songs).to be_a Array - expect(songs.count).to eq 2 - - titles = songs.map {|u| u['title'] } - expect(titles).to include "The Ally", "Barnway Blues" - end - - it "creates songs" do - expect(song_count).to eq 0 - - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - expect(song['id']).to_not be_nil - expect(song['album_id']).to_not be_nil - expect(song['title']).to eq "The Ally" - - # Check for persistence - expect(song_count).to eq 1 - - song_count_by_album = repo.songs_per_album(db) - song_count_by_album.each do |album_id| - expect(album_id['tracks'].to_i).to eq 1 - end - - song = repo.all(db).first - expect(song['title']).to eq "The Ally" - expect(song['id']).to_not be_nil - expect(song['album_id']).to_not be_nil - - end - - it "requires a title" do - expect { repo.save(db, {}) }.to raise_error {|e| - expect(e.message).to match /title/ - } - end - - it "requires an album id" do - expect { - repo.save(db, { 'title' => "The Ally" }) - } - .to raise_error {|e| - expect(e.message).to match /album_id/ - } - end - - it "requires an album id that exists" do - expect { - repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) - } - .to raise_error {|e| - expect(e.message).to match /album_id/ - } - end - - it "finds songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - retrieved_song = repo.find(db, song['id']) - expect(retrieved_song['title']).to eq "The Ally" - end - - it "updates songs" do - song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) - expect(song2['id']).to eq(song1['id']) - expect(song2['title']).to eq "Alicia" - - # Check for persistence - song3 = repo.find(db, song1['id']) - expect(song3['title']).to eq "Alicia" - end - -end diff --git a/spec/repos/shop_repo_spec.rb b/spec/repos/shop_repo_spec.rb new file mode 100644 index 00000000..f66a42bc --- /dev/null +++ b/spec/repos/shop_repo_spec.rb @@ -0,0 +1,73 @@ +require 'spec_helper' + +describe Petshop::ShopRepo do + + def owner_count + repo.all(db).count + end + + let(:repo) { Petshop::ShopRepo } + let(:db) { Petshop.create_db_connection('petshop_test') } + + before(:each) do + Petshop.clear_db(db) + @shop_id1 = Petshop::ShopRepo.save(db, { 'title' => "Pup By Pup Best" })['id'] + @shop_id2 = Petshop::ShopRepo.save(db, { 'title' => "U Buy Cat Now" })['id'] + end + + xit "gets all shops" do + + shops = repo.all(db) + expect(shops).to be_a Array + expect(shops.count).to eq 2 + + titles = shops.map {|u| u['title'] } + expect(titles).to include "Pup By Pup Best", "U Buy Cat Now" + end + + xit "creates shops" do + expect(shop_count).to eq 2 + + owner = repo.save(db, { 'title' => "Bitten by Kittens" }) + expect(shop['id']).to_not be_nil + expect(shop['title']).to eq "Bitten by Kittens" + + # Check for persistence + expect(shop_count).to eq 3 + + shop = repo.all(db).first + expect(owner['id']).to_not be_nil + expect(owner['title']).to eq "Pup By Pup Best" + + end + + xit "requires a title" do + expect { repo.save(db, {}) }.to raise_error {|e| + expect(e.message).to match /title/ + } + end + + xit "requires an shop id that exists" do + expect { + repo.save(db, { 'id' => 999, 'title' => "Mr Puppy Love" }) + } + .to raise_error {|e| + expect(e.message).to match /shop id/ + } + end + + xit "finds shops" do + retrieved_shop = repo.find(db, @shop_id1) + expect(retrieved_owner['name']).to eq "Pup By Pup Best" + end + + xit "updates shops" do + shop1 = repo.save(db, { 'id' => @shop_id1, 'title' => "Billy Boy" }) + expect(shop1['id']).to eq(@shop_id1) + + # Check for persistence + shop2 = repo.find(db, shop1['id']) + expect(owner3['title']).to eq "Billy Boy" + end + +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b15d3bbc..bd555383 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,4 @@ -require 'songify' +require 'petshop' RSpec.configure do |config| config.treat_symbols_as_metadata_keys_with_true_values = true From 2dda69210841f1a6e30bb6195411b5aa1878440e Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Thu, 11 Dec 2014 23:30:16 +0000 Subject: [PATCH 08/12] Pass Rspec Tests for all petshop repos --- Rakefile | 8 +++++++ lib/petshop.rb | 2 +- lib/petshop/dog_repo.rb | 9 ++++--- lib/petshop/owner_repo.rb | 2 +- lib/petshop/shop_repo.rb | 4 ++-- spec/repos/owner_repo_spec.rb | 12 +++++----- spec/repos/pet_repo_spec.rb | 44 +++++++++++++++++------------------ spec/repos/shop_repo_spec.rb | 24 +++++++++---------- 8 files changed, 55 insertions(+), 50 deletions(-) diff --git a/Rakefile b/Rakefile index 69b00f71..1ad1934b 100644 --- a/Rakefile +++ b/Rakefile @@ -25,13 +25,17 @@ namespace :db do 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 @@ -39,13 +43,17 @@ namespace :db do 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 diff --git a/lib/petshop.rb b/lib/petshop.rb index b735a9f0..8f3f4cca 100644 --- a/lib/petshop.rb +++ b/lib/petshop.rb @@ -94,5 +94,5 @@ def self.seed_db(db) require_relative 'petshop/shop_repo' require_relative 'petshop/owner_repo' -# require_relative 'petshop/dog_repo' +require_relative 'petshop/dog_repo' # require_relative 'petshop/cat_repo' diff --git a/lib/petshop/dog_repo.rb b/lib/petshop/dog_repo.rb index 06d6988a..5dac463d 100644 --- a/lib/petshop/dog_repo.rb +++ b/lib/petshop/dog_repo.rb @@ -7,7 +7,7 @@ def self.all(db) result = db.exec("SELECT * FROM dogs").to_a end - def self.find_by_id(db, dog_id) + def self.find(db, dog_id) dog = db.exec("SELECT * FROM dogs WHERE id=$1", [dog_id]).first end @@ -24,15 +24,14 @@ def self.save(db, dog_data) raise "A valid owner_id is required." if dog.nil? #Assign owner - result = db.exec("UPDATE dogs SET owner_id = $2, adopted_status = $3 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted_status']]) + result = db.exec("UPDATE dogs SET owner_id = $2, adopted_status = $3, name = $4 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted_status'], dog_data['name']]) self.find(db, dog_data['id']) else raise "name is required." if dog_data['name'].nil? || dog_data['name'] == '' raise "shop_id is required." if dog_data['shop_id'].nil? || dog_data['shop_id'] == '' raise "imageUrl is required." if dog_data['imageUrl'].nil? || dog_data['imageUrl'] == '' - result = db.exec("INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values ($1, $2, $3, false); RETURNING id", [dog_data['id'], dog_data['shop_id'], dog_data['imageUrl']]) - dog_data['id'] = result.entries.first['id'] - dog_data + result = db.exec("INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values ($1, $2, $3, false) RETURNING id", [dog_data['name'], dog_data['shop_id'], dog_data['imageUrl']]) + self.find(db, result.entries.first['id']) end end diff --git a/lib/petshop/owner_repo.rb b/lib/petshop/owner_repo.rb index ec640021..a0717800 100644 --- a/lib/petshop/owner_repo.rb +++ b/lib/petshop/owner_repo.rb @@ -18,7 +18,7 @@ def self.save(db, owner_data) # Ensure owner exists owner = find(db, owner_data['id']) - raise "A valid owner_id is required." if owner.nil? + raise "A valid owner id is required." if owner.nil? if owner_data['name'] # Update Owner Name result = db.exec("UPDATE owners SET name = $2 WHERE id = $1", [owner_data['id'], owner_data['name']]) diff --git a/lib/petshop/shop_repo.rb b/lib/petshop/shop_repo.rb index 1760538b..777a061c 100644 --- a/lib/petshop/shop_repo.rb +++ b/lib/petshop/shop_repo.rb @@ -21,12 +21,12 @@ def self.save(db, shop_data) # Ensure shop exists shop = find(db, shop_data['id']) - raise "A valid shop_id is required." if shop.nil? + raise "A valid shop id is required." if shop.nil? result = db.exec("UPDATE shops SET title = $2 WHERE id = $1", [shop_data['id'], shop_data['title']]) self.find(db, shop_data['id']) else - raise "title is required." if shop_data['title'].nil? || shop_data['title'] == '' + raise "shop title is required." if shop_data['title'].nil? || shop_data['title'] == '' result = db.exec("INSERT INTO shops (title) VALUES ($1) RETURNING *", [shop_data['title']]) self.find(db, result.entries.first['id']) end diff --git a/spec/repos/owner_repo_spec.rb b/spec/repos/owner_repo_spec.rb index e1a48bb3..97839b42 100644 --- a/spec/repos/owner_repo_spec.rb +++ b/spec/repos/owner_repo_spec.rb @@ -15,7 +15,7 @@ def owner_count @owner_id2 = Petshop::OwnerRepo.save(db, { 'name' => "Leonardo", 'password' => 'Swordfish' })['id'] end - xit "gets all owners" do + it "gets all owners" do owners = repo.all(db) expect(owners).to be_a Array @@ -25,7 +25,7 @@ def owner_count expect(names).to include "Leonardo", "Giovanni" end - xit "creates owners" do + it "creates owners" do expect(owner_count).to eq 2 owner = repo.save(db, { 'name' => "Brian", 'password' => 'puppyfan102' }) @@ -43,13 +43,13 @@ def owner_count end - xit "requires a name" do + it "requires a name" do expect { repo.save(db, {}) }.to raise_error {|e| expect(e.message).to match /name/ } end - xit "requires an owner id that exists" do + it "requires an owner id that exists" do expect { repo.save(db, { 'id' => 999, 'name' => "Mr FunGuy" }) } @@ -58,12 +58,12 @@ def owner_count } end - xit "finds owners" do + it "finds owners" do retrieved_owner = repo.find(db, @owner_id1) expect(retrieved_owner['name']).to eq "Giovanni" end - xit "updates owners" do + it "updates owners" do owner1 = repo.save(db, { 'id' => @owner_id1, 'name' => "Billy Boy" }) owner2 = repo.save(db, { 'id' => @owner_id1, 'password' => "bigDogsRCool14" }) expect(owner2['id']).to eq(owner1['id']) diff --git a/spec/repos/pet_repo_spec.rb b/spec/repos/pet_repo_spec.rb index fed52038..cd59824f 100644 --- a/spec/repos/pet_repo_spec.rb +++ b/spec/repos/pet_repo_spec.rb @@ -11,53 +11,51 @@ def dog_count before(:each) do Petshop.clear_db(db) - @shop_id = Petshop::ShopRepo.save(db, { 'name' => "SuperShop" })['id'] + @shop_id = Petshop::ShopRepo.save(db, { 'title' => "SuperShop" })['id'] end - xit "gets all dogs" do - dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) - dog = repo.save(db, {'name' => "Giles", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) - dog = repo.save(db, {'name' => "Goji", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) - dog = repo.save(db, {'name' => "Berry", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + it "gets all dogs" do + dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Giles", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Goji", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Berry", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) dogs = repo.all(db) - expect(songs).to be_a Array - expect(songs.count).to eq 4 + expect(dogs).to be_a Array + expect(dogs.count).to eq 4 names = dogs.map {|u| u['name'] } expect(names).to include "Barnaby", "Giles", "Goji", "Berry" end - xit "creates dogs" do + it "creates dogs" do expect(dog_count).to eq 0 - dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) expect(dog['id']).to_not be_nil expect(dog['shop_id']).to_not be_nil - expect(song['name']).to eq "Barnaby" + expect(dog['name']).to eq "Barnaby" # Check for persistence expect(dog_count).to eq 1 - dog_count_by_store = repo.find_all_by_shop(db) - dog_count_by_store.each do |dog| - expect(dog['id'].to_i).to eq 1 - end + dog_count_by_store = repo.find_all_by_shop(db, @shop_id).count + expect(dog_count_by_store).to eq 1 dog = repo.all(db).first expect(dog['id']).to_not be_nil expect(dog['shop_id']).to_not be_nil - expect(song['name']).to eq "Barnaby" + expect(dog['name']).to eq "Barnaby" end - xit "requires a name" do + it "requires a name" do expect { repo.save(db, {}) }.to raise_error {|e| expect(e.message).to match /name/ } end - xit "requires an shop id" do + it "requires an shop id" do expect { repo.save(db, { 'name' => "Barnaby" }) } @@ -66,7 +64,7 @@ def dog_count } end - xit "requires an imageUrl" do + it "requires an imageUrl" do expect { repo.save(db, { 'name' => "Barnaby", 'shop_id' => @shop_id }) } @@ -75,14 +73,14 @@ def dog_count } end - xit "finds dogs" do - dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + it "finds dogs" do + dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) retrieved_dog = repo.find(db, dog['id']) expect(retrieved_dog['name']).to eq "Barnaby" end - xit "updates dogs" do - dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'urlImage' => '', 'adopted_status' => false }) + it "updates dogs" do + dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) dog2 = repo.save(db, { 'id' => dog['id'], 'name' => "Funky" }) expect(dog2['id']).to eq(dog['id']) expect(dog2['name']).to eq "Funky" diff --git a/spec/repos/shop_repo_spec.rb b/spec/repos/shop_repo_spec.rb index f66a42bc..6a17ad1d 100644 --- a/spec/repos/shop_repo_spec.rb +++ b/spec/repos/shop_repo_spec.rb @@ -2,7 +2,7 @@ describe Petshop::ShopRepo do - def owner_count + def shop_count repo.all(db).count end @@ -15,7 +15,7 @@ def owner_count @shop_id2 = Petshop::ShopRepo.save(db, { 'title' => "U Buy Cat Now" })['id'] end - xit "gets all shops" do + it "gets all shops" do shops = repo.all(db) expect(shops).to be_a Array @@ -25,10 +25,10 @@ def owner_count expect(titles).to include "Pup By Pup Best", "U Buy Cat Now" end - xit "creates shops" do + it "creates shops" do expect(shop_count).to eq 2 - owner = repo.save(db, { 'title' => "Bitten by Kittens" }) + shop = repo.save(db, { 'title' => "Bitten by Kittens" }) expect(shop['id']).to_not be_nil expect(shop['title']).to eq "Bitten by Kittens" @@ -36,18 +36,18 @@ def owner_count expect(shop_count).to eq 3 shop = repo.all(db).first - expect(owner['id']).to_not be_nil - expect(owner['title']).to eq "Pup By Pup Best" + expect(shop['id']).to_not be_nil + expect(shop['title']).to eq "Pup By Pup Best" end - xit "requires a title" do + it "requires a title" do expect { repo.save(db, {}) }.to raise_error {|e| expect(e.message).to match /title/ } end - xit "requires an shop id that exists" do + it "requires an shop id that exists" do expect { repo.save(db, { 'id' => 999, 'title' => "Mr Puppy Love" }) } @@ -56,18 +56,18 @@ def owner_count } end - xit "finds shops" do + it "finds shops" do retrieved_shop = repo.find(db, @shop_id1) - expect(retrieved_owner['name']).to eq "Pup By Pup Best" + expect(retrieved_shop['title']).to eq "Pup By Pup Best" end - xit "updates shops" do + it "updates shops" do shop1 = repo.save(db, { 'id' => @shop_id1, 'title' => "Billy Boy" }) expect(shop1['id']).to eq(@shop_id1) # Check for persistence shop2 = repo.find(db, shop1['id']) - expect(owner3['title']).to eq "Billy Boy" + expect(shop2['title']).to eq "Billy Boy" end end From 84a696041e70cc10aced686f03541fd71be62934 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Fri, 12 Dec 2014 17:36:37 +0000 Subject: [PATCH 09/12] Built Endpoints (untested) --- lib/petshop.rb | 8 +- lib/petshop/cat_repo.rb | 33 ++++-- lib/petshop/dog_repo.rb | 14 ++- lib/petshop/owner_repo.rb | 18 +++- lib/petshop/shop_repo.rb | 2 +- server.rb | 195 +++++++++++++++++++--------------- spec/repos/owner_repo_spec.rb | 34 +++--- spec/repos/pet_repo_spec.rb | 10 +- spec/repos/shop_repo_spec.rb | 12 +-- 9 files changed, 191 insertions(+), 135 deletions(-) diff --git a/lib/petshop.rb b/lib/petshop.rb index 8f3f4cca..82f1ac72 100644 --- a/lib/petshop.rb +++ b/lib/petshop.rb @@ -1,6 +1,6 @@ require 'pg' -module Petshop +module PetShop def self.create_db_connection(dbname) PG.connect(host: 'localhost', dbname: dbname) end @@ -22,7 +22,7 @@ def self.create_tables(db) ); CREATE TABLE IF NOT EXISTS owners( id SERIAL PRIMARY KEY, - name VARCHAR, + username VARCHAR, password VARCHAR ); CREATE TABLE IF NOT EXISTS cats( @@ -64,8 +64,8 @@ def self.seed_db(db) db.exec <<-SQL -- Insert Owners - INSERT INTO owners (name, password) values ('Jason', 'pass'); - INSERT INTO owners (name, password) values ('Alex', 'pass'); + INSERT INTO owners (username, password) values ('Jason', 'pass'); + INSERT INTO owners (username, password) values ('Alex', 'pass'); SQL db.exec <<-SQL diff --git a/lib/petshop/cat_repo.rb b/lib/petshop/cat_repo.rb index 51a43e0c..63d2ccd5 100644 --- a/lib/petshop/cat_repo.rb +++ b/lib/petshop/cat_repo.rb @@ -1,38 +1,49 @@ require 'pry-byebug' -module Petshop - class CatsRepo +module PetShop + class CatRepo def self.all(db) result = db.exec("SELECT * FROM cats").to_a end def self.find(db, cat_id) - shop = db.exec("SELECT * FROM cats WHERE id=$1", [cat_id]).first + cat = db.exec("SELECT * FROM cats WHERE id=$1", [cat_id]).first end def self.find_all_by_shop(db, shop_id) cats = db.exec("SELECT * FROM cats WHERE shop_id=$1", [shop_id]).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 + 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? - raise "A valid owner_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 - result = db.exec("UPDATE cats SET owner_id = $2, adopted_status = $3 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['adopted_status']]) + if cat_data['adopted_status'] && cat_data['name'] + result = db.exec("UPDATE cats SET owner_id = $2, adopted_status = $3, name = $4 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['adopted_status'], 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_status'] + result = db.exec("UPDATE cats SET owner_id = $2, adopted_status = $3 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['adopted_status']]) + end self.find(db, cat_data['id']) - else + else # Create a New Cat raise "name is required." if cat_data['name'].nil? || cat_data['name'] == '' raise "shop_id is required." if cat_data['shop_id'].nil? || cat_data['shop_id'] == '' raise "imageUrl is required." if cat_data['imageUrl'].nil? || cat_data['imageUrl'] == '' - result = db.exec("INSERT INTO cats (name, shop_id, imageUrl, adopted_status) values ($1, $2, $3, false); RETURNING id", [dog_data['id'], dog_data['shop_id'], dog_data['imageUrl']]) - cat_data['id'] = result.entries.first['id'] - cat_data + result = db.exec("INSERT INTO cats (name, shop_id, imageUrl, adopted_status) values ($1, $2, $3, false) RETURNING id", [cat_data['name'], cat_data['shop_id'], cat_data['imageUrl']]) + self.find(db, result.entries.first['id']) end end @@ -42,4 +53,4 @@ def self.destroy(db, cat_id) end end -end \ No newline at end of file +end diff --git a/lib/petshop/dog_repo.rb b/lib/petshop/dog_repo.rb index 5dac463d..0683ad25 100644 --- a/lib/petshop/dog_repo.rb +++ b/lib/petshop/dog_repo.rb @@ -1,6 +1,6 @@ require 'pry-byebug' -module Petshop +module PetShop class DogRepo def self.all(db) @@ -15,6 +15,10 @@ def self.find_all_by_shop(db, shop_id) dogs = db.exec("SELECT * FROM dogs WHERE shop_id=$1", [shop_id]).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 @@ -24,7 +28,13 @@ def self.save(db, dog_data) raise "A valid owner_id is required." if dog.nil? #Assign owner - result = db.exec("UPDATE dogs SET owner_id = $2, adopted_status = $3, name = $4 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted_status'], dog_data['name']]) + if dog_data['adopted_status'] && dog_data['name'] + result = db.exec("UPDATE dogs SET owner_id = $2, adopted_status = $3, name = $4 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted_status'], 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_status'] + result = db.exec("UPDATE dogs SET owner_id = $2, adopted_status = $3 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted_status']]) + end self.find(db, dog_data['id']) else raise "name is required." if dog_data['name'].nil? || dog_data['name'] == '' diff --git a/lib/petshop/owner_repo.rb b/lib/petshop/owner_repo.rb index a0717800..dcf24340 100644 --- a/lib/petshop/owner_repo.rb +++ b/lib/petshop/owner_repo.rb @@ -1,6 +1,6 @@ require 'pry-byebug' -module Petshop +module PetShop class OwnerRepo def self.all(db) @@ -13,6 +13,14 @@ 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 @@ -20,8 +28,8 @@ def self.save(db, owner_data) owner = find(db, owner_data['id']) raise "A valid owner id is required." if owner.nil? - if owner_data['name'] # Update Owner Name - result = db.exec("UPDATE owners SET name = $2 WHERE id = $1", [owner_data['id'], owner_data['name']]) + 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 @@ -29,9 +37,9 @@ def self.save(db, owner_data) end self.find(db, owner_data['id']) else - raise "name is required." if owner_data['name'].nil? || owner_data['name'] == '' + 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 (name, password) VALUES ($1, $2) RETURNING *", [owner_data['name'], 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 diff --git a/lib/petshop/shop_repo.rb b/lib/petshop/shop_repo.rb index 777a061c..83e82cfc 100644 --- a/lib/petshop/shop_repo.rb +++ b/lib/petshop/shop_repo.rb @@ -1,6 +1,6 @@ require 'pry-byebug' -module Petshop +module PetShop class ShopRepo def self.all(db) diff --git a/server.rb b/server.rb index d0a3f070..191bc286 100644 --- a/server.rb +++ b/server.rb @@ -2,97 +2,124 @@ require 'sinatra/reloader' require 'rest-client' require 'json' +require 'pry-byebug' +# require 'rack-flash' -set :bind, '0.0.0.0' # This is needed for Vagrant +require_relative 'lib/petshop.rb' -# # -# This is our only html view... -# -get '/' do - if session[:user_id] - # TODO: Grab user from database - @current_user = $sample_user +class PetShop::Server < Sinatra::Application + + set :bind, '0.0.0.0' # This is needed for Vagrant + + configure do + enable :sessions + # use Rack::Flash + end + + before do + if session[:user_id] + owner_id = session['user_id'] + @db = PetShop.create_db_connection 'petshop_dev' + @current_user = PetShop::OwnerRepo.find @db, owner_id + else + @current_user = $sample_user + end + end + + # # + # This is our only html view... + # + get '/' do + erb :index + end + + # # + # ...the rest are JSON endpoints + # + get '/shops' do + headers['Content-Type'] = 'application/json' + shops = PetShop::ShopRepo.all(@db) end - erb :index -end -# # -# ...the rest are JSON endpoints -# -get '/shops' do - headers['Content-Type'] = 'application/json' - RestClient.get("http://pet-shop.api.mks.io/shops") -end + post '/signin' do + params = JSON.parse request.body.read + + username = params['username'] + password = params['password'] + + # TODO: Grab user by username from database and check password + owner = Petshop::OwnerRepo.find_by_name(@db, username) + # user = { 'username' => 'alice', 'password' => '123' } -post '/signin' do - params = JSON.parse request.body.read + # Validate credentials Valid + if password == owner['password'] + headers['Content-Type'] = 'application/json' - username = params['username'] - password = params['password'] + # TODO: Return all pets adopted by this user + @current_user['cats'] = Petshop::CatRepo.find_all_by_owner(@db, owner_id) + @current_user['dogs'] = Petshop::DogRepo.find_all_by_owner(@db, owner_id) - # TODO: Grab user by username from database and check password - user = { 'username' => 'alice', 'password' => '123' } + # TODO: Set session[:user_id] so the server will remember this user has logged in + session['user_id'] = owner['id'] - if password == user['password'] + @current_user.to_json + else + status 401 + end + end + + # # # # + # Cats # + # # # # + get '/shops/:id/cats' do headers['Content-Type'] = 'application/json' - # TODO: Return all pets adopted by this user - # TODO: Set session[:user_id] so the server will remember this user has logged in - $sample_user.to_json - else - status 401 + id = params[:id] + # TODO: Grab from database instead + # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") + + Petshop::CatRepo.find_all_by_shop(@db, id) end -end - - # # # # -# Cats # -# # # # -get '/shops/:id/cats' do - headers['Content-Type'] = 'application/json' - id = params[:id] - # TODO: Grab from database instead - RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") -end - -put '/shops/:shop_id/cats/:id/adopt' do - headers['Content-Type'] = 'application/json' - shop_id = params[:shop_id] - id = params[:id] - # TODO: Grab from database instead - RestClient.put("http://pet-shop.api.mks.io/shops/#{shop_id}/cats/#{id}", - { adopted: true }, :content_type => 'application/json') - # TODO (after you create users table): Attach new cat to logged in user -end - - - # # # # -# Dogs # -# # # # -get '/shops/:id/dogs' do - headers['Content-Type'] = 'application/json' - id = params[:id] - # TODO: Update database instead - 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') - # TODO (after you create users table): Attach new dog to logged in user -end - - -$sample_user = { - id: 999, - username: 'alice', - cats: [ - { shopId: 1, name: "NaN Cat", imageUrl: "http://i.imgur.com/TOEskNX.jpg", adopted: true, id: 44 }, - { shopId: 8, name: "Meowzer", imageUrl: "http://www.randomkittengenerator.com/images/cats/rotator.php", id: 8, adopted: "true" } - ], - dogs: [ - { shopId: 1, name: "Leaf Pup", imageUrl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted: "true" } - ] -} + + put '/shops/:shop_id/cats/:id/adopt' do + headers['Content-Type'] = 'application/json' + # shop_id = params[:shop_id] + id = params[:id] + # Grab Cat data from database + + owner = Petshop::OwnerRepo.find(@db, @current_user['id']) + cat = Petshop::CatRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted_status' => true}) + end + + + # # # # + # Dogs # + # # # # + get '/shops/:id/dogs' do + headers['Content-Type'] = 'application/json' + id = params[:id] + dogs = Petshop::DogRepo.find_all_by_shop(@db, id) + end + + put '/shops/:shop_id/dogs/:id/adopt' do + headers['Content-Type'] = 'application/json' + shop_id = params[:shop_id] + id = params[:id] + + owner = Petshop::OwnerRepo.find(@db, @current_user['id']) + cat = Petshop::DogRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted_status' => true}) + + end + + + $sample_user = { + id: 999, + username: 'anonymous', + cats: [ + { shopId: 1, name: "NaN Cat", imageUrl: "http://i.imgur.com/TOEskNX.jpg", adopted_status: true, id: 44 }, + { shopId: 8, name: "Meowzer", imageUrl: "http://www.randomkittengenerator.com/images/cats/rotator.php", id: 8, adopted_status: true } + ], + dogs: [ + { shopId: 1, name: "Leaf Pup", imageUrl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted_status: true } + ] + } +end \ No newline at end of file diff --git a/spec/repos/owner_repo_spec.rb b/spec/repos/owner_repo_spec.rb index 97839b42..b21f4a84 100644 --- a/spec/repos/owner_repo_spec.rb +++ b/spec/repos/owner_repo_spec.rb @@ -1,18 +1,18 @@ require 'spec_helper' -describe Petshop::OwnerRepo do +describe PetShop::OwnerRepo do def owner_count repo.all(db).count end - let(:repo) { Petshop::OwnerRepo } - let(:db) { Petshop.create_db_connection('petshop_test') } + let(:repo) { PetShop::OwnerRepo } + let(:db) { PetShop.create_db_connection('petshop_test') } before(:each) do - Petshop.clear_db(db) - @owner_id1 = Petshop::OwnerRepo.save(db, { 'name' => "Giovanni", 'password' => 'Swordfish' })['id'] - @owner_id2 = Petshop::OwnerRepo.save(db, { 'name' => "Leonardo", 'password' => 'Swordfish' })['id'] + PetShop.clear_db(db) + @owner_id1 = PetShop::OwnerRepo.save(db, { 'username' => "Giovanni", 'password' => 'Swordfish' })['id'] + @owner_id2 = PetShop::OwnerRepo.save(db, { 'username' => "Leonardo", 'password' => 'Swordfish' })['id'] end it "gets all owners" do @@ -21,16 +21,16 @@ def owner_count expect(owners).to be_a Array expect(owners.count).to eq 2 - names = owners.map {|u| u['name'] } - expect(names).to include "Leonardo", "Giovanni" + usernames = owners.map {|u| u['username'] } + expect(usernames).to include "Leonardo", "Giovanni" end it "creates owners" do expect(owner_count).to eq 2 - owner = repo.save(db, { 'name' => "Brian", 'password' => 'puppyfan102' }) + owner = repo.save(db, { 'username' => "Brian", 'password' => 'puppyfan102' }) expect(owner['id']).to_not be_nil - expect(owner['name']).to eq "Brian" + expect(owner['username']).to eq "Brian" expect(owner['password']).to eq "puppyfan102" # Check for persistence @@ -38,20 +38,20 @@ def owner_count owner = repo.all(db).first expect(owner['id']).to_not be_nil - expect(owner['name']).to eq "Giovanni" + expect(owner['username']).to eq "Giovanni" expect(owner['password']).to eq "Swordfish" end - it "requires a name" do + it "requires a username" do expect { repo.save(db, {}) }.to raise_error {|e| - expect(e.message).to match /name/ + expect(e.message).to match /username/ } end it "requires an owner id that exists" do expect { - repo.save(db, { 'id' => 999, 'name' => "Mr FunGuy" }) + repo.save(db, { 'id' => 999, 'username' => "Mr FunGuy" }) } .to raise_error {|e| expect(e.message).to match /owner id/ @@ -60,18 +60,18 @@ def owner_count it "finds owners" do retrieved_owner = repo.find(db, @owner_id1) - expect(retrieved_owner['name']).to eq "Giovanni" + expect(retrieved_owner['username']).to eq "Giovanni" end it "updates owners" do - owner1 = repo.save(db, { 'id' => @owner_id1, 'name' => "Billy Boy" }) + owner1 = repo.save(db, { 'id' => @owner_id1, 'username' => "Billy Boy" }) owner2 = repo.save(db, { 'id' => @owner_id1, 'password' => "bigDogsRCool14" }) expect(owner2['id']).to eq(owner1['id']) expect(owner2['password']).to eq "bigDogsRCool14" # Check for persistence owner3 = repo.find(db, owner1['id']) - expect(owner3['name']).to eq "Billy Boy" + expect(owner3['username']).to eq "Billy Boy" end end diff --git a/spec/repos/pet_repo_spec.rb b/spec/repos/pet_repo_spec.rb index cd59824f..4f8907e8 100644 --- a/spec/repos/pet_repo_spec.rb +++ b/spec/repos/pet_repo_spec.rb @@ -1,17 +1,17 @@ require 'spec_helper' -describe Petshop::DogRepo do +describe PetShop::DogRepo do def dog_count repo.all(db).count end - let(:repo) { Petshop::DogRepo } - let(:db) { Petshop.create_db_connection('petshop_test') } + let(:repo) { PetShop::DogRepo } + let(:db) { PetShop.create_db_connection('petshop_test') } before(:each) do - Petshop.clear_db(db) - @shop_id = Petshop::ShopRepo.save(db, { 'title' => "SuperShop" })['id'] + PetShop.clear_db(db) + @shop_id = PetShop::ShopRepo.save(db, { 'title' => "SuperShop" })['id'] end it "gets all dogs" do diff --git a/spec/repos/shop_repo_spec.rb b/spec/repos/shop_repo_spec.rb index 6a17ad1d..84ca3f80 100644 --- a/spec/repos/shop_repo_spec.rb +++ b/spec/repos/shop_repo_spec.rb @@ -1,18 +1,18 @@ require 'spec_helper' -describe Petshop::ShopRepo do +describe PetShop::ShopRepo do def shop_count repo.all(db).count end - let(:repo) { Petshop::ShopRepo } - let(:db) { Petshop.create_db_connection('petshop_test') } + let(:repo) { PetShop::ShopRepo } + let(:db) { PetShop.create_db_connection('petshop_test') } before(:each) do - Petshop.clear_db(db) - @shop_id1 = Petshop::ShopRepo.save(db, { 'title' => "Pup By Pup Best" })['id'] - @shop_id2 = Petshop::ShopRepo.save(db, { 'title' => "U Buy Cat Now" })['id'] + PetShop.clear_db(db) + @shop_id1 = PetShop::ShopRepo.save(db, { 'title' => "Pup By Pup Best" })['id'] + @shop_id2 = PetShop::ShopRepo.save(db, { 'title' => "U Buy Cat Now" })['id'] end it "gets all shops" do From ea30361c28176446142f8569ea8c7d06a2f5eca8 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Fri, 12 Dec 2014 19:20:41 +0000 Subject: [PATCH 10/12] All Repos pass converted server and reposto non-camel case --- config.ru | 8 ++++++ lib/petshop.rb | 50 ++++++++++++++++++------------------ lib/petshop/cat_repo.rb | 18 ++++++------- lib/petshop/dog_repo.rb | 18 ++++++------- lib/petshop/owner_repo.rb | 4 +-- lib/petshop/shop_repo.rb | 18 ++++++------- server.rb | 38 +++++++++++++-------------- spec/repos/pet_repo_spec.rb | 32 ++++++++++++----------- spec/repos/shop_repo_spec.rb | 30 +++++++++++----------- 9 files changed, 113 insertions(+), 103 deletions(-) create mode 100644 config.ru diff --git a/config.ru b/config.ru new file mode 100644 index 00000000..159ba053 --- /dev/null +++ b/config.ru @@ -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 diff --git a/lib/petshop.rb b/lib/petshop.rb index 82f1ac72..b7cd8ddd 100644 --- a/lib/petshop.rb +++ b/lib/petshop.rb @@ -18,7 +18,7 @@ def self.create_tables(db) db.exec <<-SQL CREATE TABLE IF NOT EXISTS shops( id SERIAL PRIMARY KEY, - title VARCHAR + name VARCHAR ); CREATE TABLE IF NOT EXISTS owners( id SERIAL PRIMARY KEY, @@ -28,18 +28,18 @@ def self.create_tables(db) CREATE TABLE IF NOT EXISTS cats( id SERIAL PRIMARY KEY, name VARCHAR, - shop_id INT references shops (id), + shopid INT references shops (id), owner_id INT references owners (id), - imageUrl TEXT, - adopted_status BOOLEAN + imageurl TEXT, + adopted VARCHAR ); CREATE TABLE IF NOT EXISTS dogs( id SERIAL PRIMARY KEY, name VARCHAR, - shop_id INT references shops (id), + shopid INT references shops (id), owner_id INT references owners (id), - imageUrl TEXT, - adopted_status BOOLEAN + imageurl TEXT, + adopted VARCHAR ); SQL @@ -57,9 +57,9 @@ def self.drop_tables(db) def self.seed_db(db) db.exec <<-SQL -- Insert Shops - INSERT INTO shops (title) values ('Store Prime'); - INSERT INTO shops (title) values ('Canineite'); - INSERT INTO shops (title) values ('Poochy Paradise'); + INSERT INTO shops (name) values ('Store Prime'); + INSERT INTO shops (name) values ('Canineite'); + INSERT INTO shops (name) values ('Poochy Paradise'); SQL db.exec <<-SQL @@ -70,24 +70,24 @@ def self.seed_db(db) db.exec <<-SQL -- Insert Dogs - INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values - ('Sam The Pup', 2, 'https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSMp3Zb-s_O38HlY4x9xPI0k0cJ8_DtEH4zJ4mKCt_4sGapVOOYrw', false); - INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values - ('Bark', 1, 'http://animal-backgrounds.com/download/1092/1920x1200/crop/cute-and-funny-small-dog-wallpaper-1920x1200.jpg', false); - INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values - ('Woof', 3, 'http://www.bobtail-chiens.com/medias/images/24-04-2011-gustave.jpg', false); - INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values - ('Growl', 2, 'http://www.smalldogbreedsdb.com/wp-content/uploads/EasyRotatorStorage/user-content/erc_66_1381848811/content/assets/Beagle-3.jpg-0.jpg', false); + 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, shop_id, imageUrl, adopted_status) values - ('Lammie', 1, 'https://c2.staticflickr.com/8/7418/9738381563_0ac7da6cdc_b.jpg', false); - INSERT INTO cats (name, shop_id, imageUrl, adopted_status) values - ('Joja', 2, 'http://wallpaper-download.net/wallpapers/animal-wallpapers-african-lion-king-wallpaper-31870.jpg', false); - INSERT INTO cats (name, shop_id, imageUrl, adopted_status) values - ('Biboo', 3, 'http://alabamapioneers.com/ap2/wp-content/uploads/2014/06/black_panther-t2.jpg', false); + 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 @@ -95,4 +95,4 @@ def self.seed_db(db) require_relative 'petshop/shop_repo' require_relative 'petshop/owner_repo' require_relative 'petshop/dog_repo' -# require_relative 'petshop/cat_repo' +require_relative 'petshop/cat_repo' diff --git a/lib/petshop/cat_repo.rb b/lib/petshop/cat_repo.rb index 63d2ccd5..b5af310b 100644 --- a/lib/petshop/cat_repo.rb +++ b/lib/petshop/cat_repo.rb @@ -11,8 +11,8 @@ 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, shop_id) - cats = db.exec("SELECT * FROM cats WHERE shop_id=$1", [shop_id]).to_a + 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) @@ -30,19 +30,19 @@ def self.save(db, cat_data) raise "A valid owner_id is required." if owner.nil? #Assign owner - if cat_data['adopted_status'] && cat_data['name'] - result = db.exec("UPDATE cats SET owner_id = $2, adopted_status = $3, name = $4 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['adopted_status'], cat_data['name']]) + 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_status'] - result = db.exec("UPDATE cats SET owner_id = $2, adopted_status = $3 WHERE id = $1", [cat_data['id'], cat_data['owner_id'], cat_data['adopted_status']]) + 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 "shop_id is required." if cat_data['shop_id'].nil? || cat_data['shop_id'] == '' - raise "imageUrl is required." if cat_data['imageUrl'].nil? || cat_data['imageUrl'] == '' - result = db.exec("INSERT INTO cats (name, shop_id, imageUrl, adopted_status) values ($1, $2, $3, false) RETURNING id", [cat_data['name'], cat_data['shop_id'], cat_data['imageUrl']]) + 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 diff --git a/lib/petshop/dog_repo.rb b/lib/petshop/dog_repo.rb index 0683ad25..94bf2e44 100644 --- a/lib/petshop/dog_repo.rb +++ b/lib/petshop/dog_repo.rb @@ -11,8 +11,8 @@ 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, shop_id) - dogs = db.exec("SELECT * FROM dogs WHERE shop_id=$1", [shop_id]).to_a + 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) @@ -28,19 +28,19 @@ def self.save(db, dog_data) raise "A valid owner_id is required." if dog.nil? #Assign owner - if dog_data['adopted_status'] && dog_data['name'] - result = db.exec("UPDATE dogs SET owner_id = $2, adopted_status = $3, name = $4 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted_status'], dog_data['name']]) + 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_status'] - result = db.exec("UPDATE dogs SET owner_id = $2, adopted_status = $3 WHERE id = $1", [dog_data['id'], dog_data['owner_id'], dog_data['adopted_status']]) + 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 "shop_id is required." if dog_data['shop_id'].nil? || dog_data['shop_id'] == '' - raise "imageUrl is required." if dog_data['imageUrl'].nil? || dog_data['imageUrl'] == '' - result = db.exec("INSERT INTO dogs (name, shop_id, imageUrl, adopted_status) values ($1, $2, $3, false) RETURNING id", [dog_data['name'], dog_data['shop_id'], dog_data['imageUrl']]) + 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 diff --git a/lib/petshop/owner_repo.rb b/lib/petshop/owner_repo.rb index dcf24340..61b5e583 100644 --- a/lib/petshop/owner_repo.rb +++ b/lib/petshop/owner_repo.rb @@ -46,8 +46,8 @@ def self.save(db, owner_data) def self.destroy(db, owner_id) # Delete SQL statement - db.exec("UPDATE cats SET owner_id = null, adopted_status = false WHERE owner_id = $1", [owner_id]) - db.exec("UPDATE dogs SET owner_id = null, adopted_status = false WHERE owner_id = $1", [owner_id]) + 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 diff --git a/lib/petshop/shop_repo.rb b/lib/petshop/shop_repo.rb index 83e82cfc..e21dd54a 100644 --- a/lib/petshop/shop_repo.rb +++ b/lib/petshop/shop_repo.rb @@ -10,10 +10,10 @@ def self.all(db) result = db.exec("SELECT * FROM shops").to_a end - def self.find(db, shop_id) + def self.find(db, shopid) # TODO: This has to be a JOIN table - shop = db.exec("SELECT * FROM shops WHERE id=$1", [shop_id]).first + shop = db.exec("SELECT * FROM shops WHERE id=$1", [shopid]).first end def self.save(db, shop_data) @@ -23,20 +23,20 @@ def self.save(db, shop_data) shop = find(db, shop_data['id']) raise "A valid shop id is required." if shop.nil? - result = db.exec("UPDATE shops SET title = $2 WHERE id = $1", [shop_data['id'], shop_data['title']]) + result = db.exec("UPDATE shops SET name = $2 WHERE id = $1", [shop_data['id'], shop_data['name']]) self.find(db, shop_data['id']) else - raise "shop title is required." if shop_data['title'].nil? || shop_data['title'] == '' - result = db.exec("INSERT INTO shops (title) VALUES ($1) RETURNING *", [shop_data['title']]) + raise "shop name is required." if shop_data['name'].nil? || shop_data['name'] == '' + result = db.exec("INSERT INTO shops (name) VALUES ($1) RETURNING *", [shop_data['name']]) self.find(db, result.entries.first['id']) end end - def self.destroy(db, shop_id) + def self.destroy(db, shopid) # Delete SQL statement - db.exec("DELETE FROM cats WHERE shop_id = $1", [shop_id]) - db.exec("DELETE FROM dogs WHERE shop_id = $1", [shop_id]) - db.exec("DELETE FROM shops WHERE id = $1", [shop_id]) + db.exec("DELETE FROM cats WHERE shopid = $1", [shopid]) + db.exec("DELETE FROM dogs WHERE shopid = $1", [shopid]) + db.exec("DELETE FROM shops WHERE id = $1", [shopid]) end end diff --git a/server.rb b/server.rb index 191bc286..a6b45fc7 100644 --- a/server.rb +++ b/server.rb @@ -38,7 +38,7 @@ class PetShop::Server < Sinatra::Application # get '/shops' do headers['Content-Type'] = 'application/json' - shops = PetShop::ShopRepo.all(@db) + shops = PetShop::ShopRepo.all(@db).to_json end post '/signin' do @@ -48,21 +48,21 @@ class PetShop::Server < Sinatra::Application password = params['password'] # TODO: Grab user by username from database and check password - owner = Petshop::OwnerRepo.find_by_name(@db, username) + owner = PetShop::OwnerRepo.find_by_name(@db, username) # user = { 'username' => 'alice', 'password' => '123' } # Validate credentials Valid - if password == owner['password'] + if !owner.nil? && password == owner['password'] headers['Content-Type'] = 'application/json' # TODO: Return all pets adopted by this user - @current_user['cats'] = Petshop::CatRepo.find_all_by_owner(@db, owner_id) - @current_user['dogs'] = Petshop::DogRepo.find_all_by_owner(@db, owner_id) + owner['cats'] = PetShop::CatRepo.find_all_by_owner(@db, owner['id']) + owner['dogs'] = PetShop::DogRepo.find_all_by_owner(@db, owner['id']) # TODO: Set session[:user_id] so the server will remember this user has logged in session['user_id'] = owner['id'] - @current_user.to_json + owner.to_json else status 401 end @@ -77,17 +77,17 @@ class PetShop::Server < Sinatra::Application # TODO: Grab from database instead # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") - Petshop::CatRepo.find_all_by_shop(@db, id) + PetShop::CatRepo.find_all_by_shop(@db, id).to_json end - put '/shops/:shop_id/cats/:id/adopt' do + put '/shops/:shopid/cats/:id/adopt' do headers['Content-Type'] = 'application/json' - # shop_id = params[:shop_id] + # shopid = params[:shopid] id = params[:id] # Grab Cat data from database - owner = Petshop::OwnerRepo.find(@db, @current_user['id']) - cat = Petshop::CatRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted_status' => true}) + owner = PetShop::OwnerRepo.find(@db, @current_user['id']) + cat = PetShop::CatRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted' => true}).to_json end @@ -97,16 +97,16 @@ class PetShop::Server < Sinatra::Application get '/shops/:id/dogs' do headers['Content-Type'] = 'application/json' id = params[:id] - dogs = Petshop::DogRepo.find_all_by_shop(@db, id) + dogs = PetShop::DogRepo.find_all_by_shop(@db, id).to_json end - put '/shops/:shop_id/dogs/:id/adopt' do + put '/shops/:shopid/dogs/:id/adopt' do headers['Content-Type'] = 'application/json' - shop_id = params[:shop_id] + shopid = params[:shopid] id = params[:id] - owner = Petshop::OwnerRepo.find(@db, @current_user['id']) - cat = Petshop::DogRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted_status' => true}) + owner = PetShop::OwnerRepo.find(@db, @current_user['id']) + cat = PetShop::DogRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted' => true}).to_json end @@ -115,11 +115,11 @@ class PetShop::Server < Sinatra::Application id: 999, username: 'anonymous', cats: [ - { shopId: 1, name: "NaN Cat", imageUrl: "http://i.imgur.com/TOEskNX.jpg", adopted_status: true, id: 44 }, - { shopId: 8, name: "Meowzer", imageUrl: "http://www.randomkittengenerator.com/images/cats/rotator.php", id: 8, adopted_status: true } + { shopid: 1, name: "NaN Cat", imageurl: "http://i.imgur.com/TOEskNX.jpg", adopted: true, id: 44 }, + { shopid: 8, name: "Meowzer", imageurl: "http://www.randomkittengenerator.com/images/cats/rotator.php", id: 8, adopted: true } ], dogs: [ - { shopId: 1, name: "Leaf Pup", imageUrl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted_status: true } + { shopid: 1, name: "Leaf Pup", imageurl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted: true } ] } end \ No newline at end of file diff --git a/spec/repos/pet_repo_spec.rb b/spec/repos/pet_repo_spec.rb index 4f8907e8..d2ff2b81 100644 --- a/spec/repos/pet_repo_spec.rb +++ b/spec/repos/pet_repo_spec.rb @@ -11,14 +11,16 @@ def dog_count before(:each) do PetShop.clear_db(db) - @shop_id = PetShop::ShopRepo.save(db, { 'title' => "SuperShop" })['id'] + @shopid = PetShop::ShopRepo.save(db, { 'name' => "SuperShop" })['id'] end it "gets all dogs" do - dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) - dog = repo.save(db, {'name' => "Giles", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) - dog = repo.save(db, {'name' => "Goji", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) - dog = repo.save(db, {'name' => "Berry", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Barnaby", 'shopid' => @shopid, 'imageurl' => 'dummy', 'adopted' => 'false' }) + dog = repo.save(db, {'name' => "Giles", 'shopid' => @shopid, 'imageurl' => 'dummy', 'adopted' => 'false' }) + dog = repo.save(db, {'name' => "Goji", 'shopid' => @shopid, 'imageurl' => 'dummy', 'adopted' => 'false' }) + dog = repo.save(db, {'name' => "Berry", 'shopid' => @shopid, 'imageurl' => 'dummy', 'adopted' => 'false' }) + + expect(dog['shopid']).to_not be_nil dogs = repo.all(db) expect(dogs).to be_a Array @@ -31,20 +33,20 @@ def dog_count it "creates dogs" do expect(dog_count).to eq 0 - dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Barnaby", 'shopid' => @shopid, 'imageurl' => 'dummy', 'adopted' => 'false' }) expect(dog['id']).to_not be_nil - expect(dog['shop_id']).to_not be_nil + expect(dog['shopid']).to_not be_nil expect(dog['name']).to eq "Barnaby" # Check for persistence expect(dog_count).to eq 1 - dog_count_by_store = repo.find_all_by_shop(db, @shop_id).count + dog_count_by_store = repo.find_all_by_shop(db, @shopid).count expect(dog_count_by_store).to eq 1 dog = repo.all(db).first expect(dog['id']).to_not be_nil - expect(dog['shop_id']).to_not be_nil + expect(dog['shopid']).to_not be_nil expect(dog['name']).to eq "Barnaby" end @@ -60,27 +62,27 @@ def dog_count repo.save(db, { 'name' => "Barnaby" }) } .to raise_error {|e| - expect(e.message).to match /shop_id/ + expect(e.message).to match /shopid/ } end - it "requires an imageUrl" do + it "requires an imageurl" do expect { - repo.save(db, { 'name' => "Barnaby", 'shop_id' => @shop_id }) + repo.save(db, { 'name' => "Barnaby", 'shopid' => @shopid }) } .to raise_error {|e| - expect(e.message).to match /imageUrl/ + expect(e.message).to match /imageurl/ } end it "finds dogs" do - dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Barnaby", 'shopid' => @shopid, 'imageurl' => 'dummy', 'adopted' => 'false' }) retrieved_dog = repo.find(db, dog['id']) expect(retrieved_dog['name']).to eq "Barnaby" end it "updates dogs" do - dog = repo.save(db, {'name' => "Barnaby", 'shop_id' => @shop_id, 'imageUrl' => 'dummy', 'adopted_status' => false }) + dog = repo.save(db, {'name' => "Barnaby", 'shopid' => @shopid, 'imageurl' => 'dummy', 'adopted' => 'false' }) dog2 = repo.save(db, { 'id' => dog['id'], 'name' => "Funky" }) expect(dog2['id']).to eq(dog['id']) expect(dog2['name']).to eq "Funky" diff --git a/spec/repos/shop_repo_spec.rb b/spec/repos/shop_repo_spec.rb index 84ca3f80..dd25ce0d 100644 --- a/spec/repos/shop_repo_spec.rb +++ b/spec/repos/shop_repo_spec.rb @@ -11,8 +11,8 @@ def shop_count before(:each) do PetShop.clear_db(db) - @shop_id1 = PetShop::ShopRepo.save(db, { 'title' => "Pup By Pup Best" })['id'] - @shop_id2 = PetShop::ShopRepo.save(db, { 'title' => "U Buy Cat Now" })['id'] + @shopid1 = PetShop::ShopRepo.save(db, { 'name' => "Pup By Pup Best" })['id'] + @shopid2 = PetShop::ShopRepo.save(db, { 'name' => "U Buy Cat Now" })['id'] end it "gets all shops" do @@ -21,35 +21,35 @@ def shop_count expect(shops).to be_a Array expect(shops.count).to eq 2 - titles = shops.map {|u| u['title'] } - expect(titles).to include "Pup By Pup Best", "U Buy Cat Now" + names = shops.map {|u| u['name'] } + expect(names).to include "Pup By Pup Best", "U Buy Cat Now" end it "creates shops" do expect(shop_count).to eq 2 - shop = repo.save(db, { 'title' => "Bitten by Kittens" }) + shop = repo.save(db, { 'name' => "Bitten by Kittens" }) expect(shop['id']).to_not be_nil - expect(shop['title']).to eq "Bitten by Kittens" + expect(shop['name']).to eq "Bitten by Kittens" # Check for persistence expect(shop_count).to eq 3 shop = repo.all(db).first expect(shop['id']).to_not be_nil - expect(shop['title']).to eq "Pup By Pup Best" + expect(shop['name']).to eq "Pup By Pup Best" end - it "requires a title" do + it "requires a name" do expect { repo.save(db, {}) }.to raise_error {|e| - expect(e.message).to match /title/ + expect(e.message).to match /name/ } end it "requires an shop id that exists" do expect { - repo.save(db, { 'id' => 999, 'title' => "Mr Puppy Love" }) + repo.save(db, { 'id' => 999, 'name' => "Mr Puppy Love" }) } .to raise_error {|e| expect(e.message).to match /shop id/ @@ -57,17 +57,17 @@ def shop_count end it "finds shops" do - retrieved_shop = repo.find(db, @shop_id1) - expect(retrieved_shop['title']).to eq "Pup By Pup Best" + retrieved_shop = repo.find(db, @shopid1) + expect(retrieved_shop['name']).to eq "Pup By Pup Best" end it "updates shops" do - shop1 = repo.save(db, { 'id' => @shop_id1, 'title' => "Billy Boy" }) - expect(shop1['id']).to eq(@shop_id1) + shop1 = repo.save(db, { 'id' => @shopid1, 'name' => "Billy Boy" }) + expect(shop1['id']).to eq(@shopid1) # Check for persistence shop2 = repo.find(db, shop1['id']) - expect(shop2['title']).to eq "Billy Boy" + expect(shop2['name']).to eq "Billy Boy" end end From e9383a3d5230bb012a17fb9e38f5bd91efeceadd Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Fri, 12 Dec 2014 20:30:36 +0000 Subject: [PATCH 11/12] Redirect after Adopt Link Click --- Rakefile | 34 +++++++++++++++++----------------- public/js/pet-container.js | 3 +-- public/js/pet-shop.js | 11 +++++------ server.rb | 19 +++++++++++-------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/Rakefile b/Rakefile index 1ad1934b..4d551e6d 100644 --- a/Rakefile +++ b/Rakefile @@ -25,36 +25,36 @@ namespace :db do 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) + 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) + 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) + 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) + db = PetShop.create_db_connection('petshop_dev') + db1 = PetShop.create_db_connection('petshop_test') + PetShop.seed_db(db) puts "Seeded." end diff --git a/public/js/pet-container.js b/public/js/pet-container.js index 68e095ca..ee33c56f 100644 --- a/public/js/pet-container.js +++ b/public/js/pet-container.js @@ -29,11 +29,10 @@ } var petsView = function (ctrl, type, pets) { - var petDivs = pets.map(function(pet) { return m('.pet', [ m('.photo', - m('img', { src: pet.imageUrl }) + m('img', { src: pet.imageurl }) ), m('.info', [ m('h4', pet.name) diff --git a/public/js/pet-shop.js b/public/js/pet-shop.js index 8aa6893b..160696f7 100644 --- a/public/js/pet-shop.js +++ b/public/js/pet-shop.js @@ -18,7 +18,7 @@ m.request({ method: 'put', url: adoptUrl }).then(function() { var pet = pets().find(function(p){ return p.id == petId }) - pet.adopted = true + pet.adopted = "true" appCtrl.user()[type].push(pet) }, genericError) } @@ -40,23 +40,22 @@ var petsView = function (ctrl, type, pets) { if (!pets) return null - + debugger var petDivs = pets.map(function(pet) { var adoptLink = m('a', { onclick: ctrl.adopt.coldCurry(type, pet.id), href: '#' }, 'Adopt this pet') - return m('.pet', [ m('.photo', - m('img', { src: pet.imageUrl }) + m('img', { src: pet.imageurl }) ), m('.info', [ m('h4', pet.name), m('b', "Adopted: "), - m('span', pet.adopted ? "Yes!" : "No..."), + m('span', pet.adopted == "true" ? "Yes!" : "No..."), m('br'), - pet.adopted ? null : adoptLink + pet.adopted == "true" ? null : adoptLink ]) ]) }) diff --git a/server.rb b/server.rb index a6b45fc7..17d62ad8 100644 --- a/server.rb +++ b/server.rb @@ -17,12 +17,15 @@ class PetShop::Server < Sinatra::Application end before do + @db = PetShop.create_db_connection 'petshop_dev' if session[:user_id] owner_id = session['user_id'] - @db = PetShop.create_db_connection 'petshop_dev' @current_user = PetShop::OwnerRepo.find @db, owner_id + @current_user['cats'] = PetShop::CatRepo.find_all_by_owner(@db, @current_user['id']) + @current_user['dogs'] = PetShop::DogRepo.find_all_by_owner(@db, @current_user['id']) + @current_user else - @current_user = $sample_user + #@current_user = $sample_user end end @@ -76,7 +79,7 @@ class PetShop::Server < Sinatra::Application id = params[:id] # TODO: Grab from database instead # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") - + PetShop::CatRepo.find_all_by_shop(@db, id).to_json end @@ -87,7 +90,7 @@ class PetShop::Server < Sinatra::Application # Grab Cat data from database owner = PetShop::OwnerRepo.find(@db, @current_user['id']) - cat = PetShop::CatRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted' => true}).to_json + cat = PetShop::CatRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted' => 'true'}).to_json end @@ -106,7 +109,7 @@ class PetShop::Server < Sinatra::Application id = params[:id] owner = PetShop::OwnerRepo.find(@db, @current_user['id']) - cat = PetShop::DogRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted' => true}).to_json + cat = PetShop::DogRepo.save(@db, {'id' => id, 'owner_id' => owner['id'], 'adopted' => 'true'}).to_json end @@ -115,11 +118,11 @@ class PetShop::Server < Sinatra::Application id: 999, username: 'anonymous', cats: [ - { shopid: 1, name: "NaN Cat", imageurl: "http://i.imgur.com/TOEskNX.jpg", adopted: true, id: 44 }, - { shopid: 8, name: "Meowzer", imageurl: "http://www.randomkittengenerator.com/images/cats/rotator.php", id: 8, adopted: true } + { shopid: 1, name: "NaN Cat", imageurl: "http://i.imgur.com/TOEskNX.jpg", adopted: 'true', id: 44 }, + { shopid: 8, name: "Meowzer", imageurl: "http://www.randomkittengenerator.com/images/cats/rotator.php", id: 8, adopted: 'true' } ], dogs: [ - { shopid: 1, name: "Leaf Pup", imageurl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted: true } + { shopid: 1, name: "Leaf Pup", imageurl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted: 'true' } ] } end \ No newline at end of file From 76b06f67e911433f3d29fc6488ed5134e33c3b45 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Fri, 12 Dec 2014 20:34:27 +0000 Subject: [PATCH 12/12] Add a README of instruction after pulling repo --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..36f201de --- /dev/null +++ b/README.md @@ -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