From 91f4958782dfd13aeb72edc0cba2083d03f9887f Mon Sep 17 00:00:00 2001 From: Jessica Meyer Date: Fri, 12 Dec 2014 10:34:03 -0600 Subject: [PATCH 01/22] first commit --- Gemfile | 2 + Gemfile.lock | 5 ++ lib/petshopserver.rb | 70 ++++++++++++++++++++ lib/petshopserver/repos/cats_repo.rb | 0 lib/petshopserver/repos/dogs_repo.rb | 0 lib/petshopserver/repos/shops_repo.rb | 0 lib/petshopserver/repos/users_repo.rb | 28 ++++++++ server.rb | 94 ++++++++++++++++++++++----- 8 files changed, 184 insertions(+), 15 deletions(-) create mode 100644 lib/petshopserver.rb create mode 100644 lib/petshopserver/repos/cats_repo.rb create mode 100644 lib/petshopserver/repos/dogs_repo.rb create mode 100644 lib/petshopserver/repos/shops_repo.rb create mode 100644 lib/petshopserver/repos/users_repo.rb diff --git a/Gemfile b/Gemfile index 66526d43..9695aef1 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ gem 'rspec', '~> 2.14.1' gem 'sinatra', '~> 1.4.5' gem 'sinatra-contrib', '~> 1.4.2' gem 'rest-client' +gem 'rack-flash3' +gem 'pg' # Testing gem 'pry-byebug' diff --git a/Gemfile.lock b/Gemfile.lock index bbeebb1e..bdcc68fb 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) @@ -22,6 +23,8 @@ GEM byebug (~> 3.4) pry (~> 0.10) rack (1.5.2) + rack-flash3 (1.0.5) + rack rack-protection (1.5.3) rack rack-test (0.6.2) @@ -55,7 +58,9 @@ PLATFORMS ruby DEPENDENCIES + pg pry-byebug + rack-flash3 rest-client rspec (~> 2.14.1) sinatra (~> 1.4.5) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb new file mode 100644 index 00000000..a2a30ba5 --- /dev/null +++ b/lib/petshopserver.rb @@ -0,0 +1,70 @@ +require 'pg' + +require_relative 'petshopserver/repos/shops_repo.rb' +require_relative 'petshopserver/repos/cats_repo.rb' +require_relative 'petshopserver/repos/dogs_repo.rb' +require_relative 'petshopserver/repos/users_repo.rb' + +# in irb - +# db = Petshopserver.create_db_connection('petshopserver') +# Petshopserver.create_tables(db) +# Petshopserver.seed_db(db) + +module Petshopserver + def self.create_db_connection(dbname) + PG.connect(host: 'localhost', dbname: dbname) + end + + def self.clear_db(db) + db.exec <<-SQL + DELETE FROM shops; + DELETE FROM dogs; + DELETE FROM cats; + DELETE FROM users; + SQL + end + + def self.create_tables(db) + db.exec <<-SQL + CREATE TABLE IF NOT EXISTS users( + id SERIAL PRIMARY KEY, + username VARCHAR, + password VARCHAR + ); + CREATE TABLE IF NOT EXISTS shops( + id SERIAL PRIMARY KEY, + name VARCHAR + ); + CREATE TABLE IF NOT EXISTS cats( + id SERIAL PRIMARY KEY, + shop_id INTEGER references shops(id), + name VARCHAR, + user_id INTEGER references users(id), + adopted BOOLEAN + ); + CREATE TABLE IF NOT EXISTS dogs( + id SERIAL PRIMARY KEY, + shop_id INTEGER references shops(id), + name VARCHAR, + user_id INTEGER references users(id), + adopted BOOLEAN + ); + SQL + end + + def self.seed_db(db) + db.exec <<-SQL + INSERT INTO users (username, password) values ('anonymous', 'anonymous') + INSERT INTO users (username, password) values ('Jessica', '123') + SQL + end + + def self.drop_tables(db) + db.exec <<-SQL + DROP TABLE shops; + DROP TABLE dogs; + DROP TABLE cats; + DROP TABLE users; + SQL + end +end \ No newline at end of file diff --git a/lib/petshopserver/repos/cats_repo.rb b/lib/petshopserver/repos/cats_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/petshopserver/repos/dogs_repo.rb b/lib/petshopserver/repos/dogs_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/petshopserver/repos/shops_repo.rb b/lib/petshopserver/repos/shops_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/petshopserver/repos/users_repo.rb b/lib/petshopserver/repos/users_repo.rb new file mode 100644 index 00000000..0fdfcc7f --- /dev/null +++ b/lib/petshopserver/repos/users_repo.rb @@ -0,0 +1,28 @@ +module Petshopserver + class UsersRepo + # find a user by user ID. Intended to be used when + # someone is already authenticated. We keep their + # user id in the cookie. + def self.find db, user_id + sql = %q[SELECT * FROM users WHERE id = $1] + result = db.exec(sql, [user_id]) + result.first + end + + # find user by username. Intended to be used when + # someone tries to sign in. + def self.find_by_username db, username + sql = %q[SELECT * FROM users WHERE username = $1] + result = db.exec(sql, [username]) + result.first + end + + # when someone signs up use this method to save their + # information in the db. we're not encrypting passwords. + def self.save db, user_data + sql = %q[INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *] + result = db.exec(sql, [user_data[:username], user_data[:password]]) + result.first + end + end +end diff --git a/server.rb b/server.rb index 07df95f4..7672f0ab 100644 --- a/server.rb +++ b/server.rb @@ -2,26 +2,59 @@ require 'sinatra/reloader' require 'rest-client' require 'json' +require 'rack-flash' +require 'pry-byebug' +require_relative 'lib/petshopserver.rb' + +# class Petshopserver::Server < Sinatra::Application + + configure do + set :bind, '0.0.0.0' + enable :sessions + use Rack::Flash + end + +# before do +# if session['user_id'] # if it exists, the user is logged in +# user_id = session['user_id'] +# db = Petshopserver.create_db_connection 'petshopserver' +# @current_user = Petshopserver::UsersRepo.find db, user_id +# else +# @current_user = {'username' => 'anonymous', 'id' => 1} +# end +# end # # +# for session you use a symbol + # This is our only html view... # get '/' do - if session[:user_id] + if session['user_id'] # TODO: Grab user from database - @current_user = $sample_user + # @current_user = $sample_user + user_id = session['user_id'] + db = Petshopserver.create_db_connection 'petshopserver' + @current_user = Petshopserver::UsersRepo.find db, user_id end erb :index end +get '/logout' do + session.delete 'user_id' + redirect to '/' +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 @@ -29,18 +62,31 @@ password = params['password'] # TODO: Grab user by username from database and check password - user = { 'username' => 'alice', 'password' => '123' } + # user = { 'username' => 'alice', 'password' => '123' } + + db = Petshopserver.create_db_connection 'petshopserver' + user = Petshopserver::UsersRepo.find_by_username(db, username) if password == user['password'] headers['Content-Type'] = 'application/json' + session[:user_id] = user['id'] + + user['cats'] = [] + user['dogs'] = [] + + user.to_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 + # $sample_user.to_json else status 401 end end +# user_id = session['user_id'] +# db = Petshopserver.create_db_connection 'petshopserver' +# @current_user = Petshopserver::UsersRepo.find db, user_id + # # # # # Cats # # # # # @@ -83,14 +129,32 @@ 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" } - ] -} + + + + +# $sample_user = { +# id: 999, +# username: 'alice', +# cats: [ +# { shop_id: 1, name: "NaN Cat", adopted: true, id: 44 }, +# { shop_id: 8, name: "Meowzer", id: 8, adopted: "true" } +# ], +# dogs: [ +# { shop_id: 1, name: "Leaf Pup", id: 2, adopted: "true" } +# ] +# } + +# $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" } +# ] +# } + +# end From 8d43b78c84650b323063eb1309d3d2407d93412a Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 10:39:50 -0600 Subject: [PATCH 02/22] add server.rb and lib etc --- Gemfile | 4 +- Gemfile.lock | 15 ++++- lib/petshopserver.rb | 68 ++++++++++++++++++++++ lib/petshopserver/repos/cats_repo.rb | 0 lib/petshopserver/repos/dogs_repo.rb | 0 lib/petshopserver/repos/shops_repo.rb | 0 lib/petshopserver/repos/users_repo.rb | 29 ++++++++++ server.rb | 81 ++++++++++++++++++++------- 8 files changed, 174 insertions(+), 23 deletions(-) create mode 100644 lib/petshopserver.rb create mode 100644 lib/petshopserver/repos/cats_repo.rb create mode 100644 lib/petshopserver/repos/dogs_repo.rb create mode 100644 lib/petshopserver/repos/shops_repo.rb create mode 100644 lib/petshopserver/repos/users_repo.rb diff --git a/Gemfile b/Gemfile index 20e422c9..3d314548 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,8 @@ ruby '2.0.0' gem 'rspec', '~> 2.14.1' gem 'sinatra', '~> 1.4.5' gem 'sinatra-contrib', '~> 1.4.2' - +gem 'rest-client' +gem 'rack-flash3' # Testing gem 'pry-byebug' +gem 'pg' diff --git a/Gemfile.lock b/Gemfile.lock index 92b9a02a..bdf4e0da 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,7 +11,10 @@ GEM debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) + mime-types (2.4.3) multi_json (1.10.1) + netrc (0.10.0) + pg (0.17.1) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -20,10 +23,15 @@ GEM byebug (~> 3.4) pry (~> 0.10) rack (1.5.2) + rack-flash3 (1.0.5) + rack rack-protection (1.5.3) rack rack-test (0.6.2) rack (>= 1.0) + rest-client (1.7.2) + mime-types (>= 1.16, < 3.0) + netrc (~> 0.7) rspec (2.14.1) rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) @@ -50,7 +58,10 @@ PLATFORMS ruby DEPENDENCIES + pg pry-byebug + rack-flash3 + rest-client rspec (~> 2.14.1) - sinatra - sinatra-contrib + sinatra (~> 1.4.5) + sinatra-contrib (~> 1.4.2) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb new file mode 100644 index 00000000..d6c4f05e --- /dev/null +++ b/lib/petshopserver.rb @@ -0,0 +1,68 @@ +require 'pg' + +require_relative 'petshopserver/repos/shops_repo.rb' +require_relative 'petshopserver/repos/cats_repo.rb' +require_relative 'petshopserver/repos/dogs_repo.rb' +require_relative 'petshopserver/repos/users_repo.rb' + + +module Petshopserver + def self.create_db_connection(dbname) + PG.connect(host: 'localhost', dbname: dbname) + end + + def self.clear_db(db) + db.exec <<-SQL + DELETE FROM shops; + DELETE FROM dogs; + DELETE FROM cats; + DELETE FROM users; + SQL + end + + def self.create_tables(db) + db.exec <<-SQL + CREATE TABLE IF NOT EXISTS users( + id SERIAL PRIMARY KEY, + username VARCHAR, + password VARCHAR + ); + CREATE TABLE IF NOT EXISTS shops( + id SERIAL PRIMARY KEY, + name VARCHAR + ); + CREATE TABLE IF NOT EXISTS cats( + id SERIAL PRIMARY KEY, + shop_id INT references shops(id), + name VARCHAR, + user_id INT references users(id), + adopted BOOLEAN + ); + CREATE TABLE IF NOT EXISTS dogs( + id SERIAL PRIMARY KEY, + shop_id INT references shops(id), + name VARCHAR, + user_id INT references users(id), + adopted BOOLEAN + ); + SQL + end + + def self.seed_db(db) + db.exec <<-SQL + INSERT INTO users (username, password) values ('anonymous', 'anonymous') + INSERT INTO users (username, password) values ('Cristina', '1234') + + SQL + end + + def self.drop_tables(db) + db.exec <<-SQL + DROP TABLE shops; + DROP TABLE dogs; + DROP TABLE cats; + DROP TABLE users; + SQL + end +end + diff --git a/lib/petshopserver/repos/cats_repo.rb b/lib/petshopserver/repos/cats_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/petshopserver/repos/dogs_repo.rb b/lib/petshopserver/repos/dogs_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/petshopserver/repos/shops_repo.rb b/lib/petshopserver/repos/shops_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/petshopserver/repos/users_repo.rb b/lib/petshopserver/repos/users_repo.rb new file mode 100644 index 00000000..d44f4508 --- /dev/null +++ b/lib/petshopserver/repos/users_repo.rb @@ -0,0 +1,29 @@ +module Petshopserver + + class UsersRepo + # find a user by user ID. Intended to be used when + # someone is already authenticated. We keep their + # user id in the cookie. + def self.find db, user_id + sql = %q[SELECT * FROM users WHERE id = $1] + result = db.exec(sql, [user_id]) + result.first + end + + # find user by username. Intended to be used when + # someone tries to sign in. + def self.find_by_username db, username + sql = %q[SELECT * FROM users WHERE username = $1] + result = db.exec(sql, [username]) + result.first + end + + # when someone signs up use this method to save their + # information in the db. we're not encrypting passwords. + def self.save db, user_data + sql = %q[INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *] + result = db.exec(sql, [user_data[:username], user_data[:password]]) + result.first + end + end +end diff --git a/server.rb b/server.rb index 9ae90b34..f86a6947 100644 --- a/server.rb +++ b/server.rb @@ -1,15 +1,46 @@ require 'sinatra' require 'sinatra/reloader' require 'rest-client' +require 'rack-flash' +require 'pry-byebug' +require 'json' #why? -# # -# This is our only html view... -# -get '/' do - if session[:user_id] - # TODO: Grab user from database - @current_user = $sample_user +#bundle exec ruby server.rb + + +require_relative 'lib/petshopserver.rb' + +# class Petshopserver::Server < Sinatra::Application + configure do + set :bind, '0.0.0.0' + enable :sessions + use Rack::Flash + end + + helpers do + def mydb + # establish connection to db here + end end + + # before do + # if session['user_id'] + # user_id = session['user_id'] + # db = Petshopserver.create_db_connection 'petshopserver' + # @current_user = Petshopserver::UsersRepo.find db, user_id + # else + # @current_user = {'username' => 'anonymous', 'id' => 1} + # end + # end + +get '/' do + if session['user_id'] + user_id = session['user_id'] + db = Petshopserver.create_db_connection 'petshopserver' + @current_user = Petshopserver::UsersRepo.find db, user_id + else + @current_user = {'username' => 'anonymous', 'id' => 1} + end erb :index end @@ -28,13 +59,21 @@ password = params['password'] # TODO: Grab user by username from database and check password - user = { 'username' => 'alice', 'password' => '123' } + #user = { 'username' => 'alice', 'password' => '123' } + db = Petshopserver.create_db_connection 'petshopserver' + user = Petshopserver::UsersRepo.find_by_username(db, username) if password == user['password'] headers['Content-Type'] = 'application/json' + session['user_id'] = user['id'] + # + user[:cats] =[] + user[:dogs] =[] + + user.to_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 + #$sample_user.to_json else status 401 end @@ -82,14 +121,16 @@ 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" } - ] -} +# $sample_user = { +# id: 999, +# username: 'alice', +# cats: [ +# { shop_id: 1, name: "NaN Cat", adopted: true, id: 44 }, +# { shop_id: 8, name: "Meowzer", id: 8, adopted: "true" } +# ], +# dogs: [ +# { shop_id: 1, name: "Leaf Pup", id: 2, adopted: "true" } +# ] +# } + +# end \ No newline at end of file From 8620e53eaac14838542bff3e9e5655ebd8f188b0 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 11:04:37 -0600 Subject: [PATCH 03/22] change name into seed_db cristia->jessica and password 1234->123 --- lib/petshopserver.rb | 2 +- server.rb | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index d6c4f05e..b4d7f562 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -51,7 +51,7 @@ def self.create_tables(db) def self.seed_db(db) db.exec <<-SQL INSERT INTO users (username, password) values ('anonymous', 'anonymous') - INSERT INTO users (username, password) values ('Cristina', '1234') + INSERT INTO users (username, password) values ('Jessica', '123') SQL end diff --git a/server.rb b/server.rb index f86a6947..b0b9af58 100644 --- a/server.rb +++ b/server.rb @@ -19,7 +19,7 @@ helpers do def mydb - # establish connection to db here + db = Petshopserver.create_db_connection 'petshopserver' end end @@ -36,11 +36,10 @@ def mydb get '/' do if session['user_id'] user_id = session['user_id'] - db = Petshopserver.create_db_connection 'petshopserver' - @current_user = Petshopserver::UsersRepo.find db, user_id + @current_user = Petshopserver::UsersRepo.find mydb, user_id else @current_user = {'username' => 'anonymous', 'id' => 1} - end + end erb :index end @@ -60,8 +59,7 @@ def mydb # TODO: Grab user by username from database and check password #user = { 'username' => 'alice', 'password' => '123' } - db = Petshopserver.create_db_connection 'petshopserver' - user = Petshopserver::UsersRepo.find_by_username(db, username) + user = Petshopserver::UsersRepo.find_by_username(mydb, username) if password == user['password'] headers['Content-Type'] = 'application/json' From b25949d835077f1267e2fae254b19fdfc2f3a543 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 11:31:35 -0600 Subject: [PATCH 04/22] fix merging issues --- Gemfile.lock | 2 +- server.rb | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a9500578..bdcc68fb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -64,4 +64,4 @@ DEPENDENCIES rest-client rspec (~> 2.14.1) sinatra (~> 1.4.5) - sinatra-contrib (~> 1.4.2) \ No newline at end of file + sinatra-contrib (~> 1.4.2) diff --git a/server.rb b/server.rb index 7bb134e3..f7e273fb 100644 --- a/server.rb +++ b/server.rb @@ -33,12 +33,6 @@ def mydb # end # end -get '/' do - if session['user_id'] - user_id = session['user_id'] - @current_user = Petshopserver::UsersRepo.find mydb, user_id - else - @current_user = {'username' => 'anonymous', 'id' => 1} require_relative 'lib/petshopserver.rb' @@ -71,7 +65,7 @@ def mydb # @current_user = $sample_user user_id = session['user_id'] db = Petshopserver.create_db_connection 'petshopserver' - @current_user = Petshopserver::UsersRepo.find db, user_id + @current_user = Petshopserver::UsersRepo.find mydb, user_id end erb :index end @@ -176,5 +170,5 @@ def mydb # ] # } -# end + From a09dc5b9e41cb282b15d8e39217e472b565e89b3 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 11:35:17 -0600 Subject: [PATCH 05/22] fix merging issues --- server.rb | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/server.rb b/server.rb index f7e273fb..11ee4e65 100644 --- a/server.rb +++ b/server.rb @@ -23,27 +23,6 @@ def mydb end end - # before do - # if session['user_id'] - # user_id = session['user_id'] - # db = Petshopserver.create_db_connection 'petshopserver' - # @current_user = Petshopserver::UsersRepo.find db, user_id - # else - # @current_user = {'username' => 'anonymous', 'id' => 1} - # end - # end - - - -require_relative 'lib/petshopserver.rb' - -# class Petshopserver::Server < Sinatra::Application - - configure do - set :bind, '0.0.0.0' - enable :sessions - use Rack::Flash - end # before do # if session['user_id'] # if it exists, the user is logged in From d714426277a9cadb1f91ddcaec204e0cc7adf39f Mon Sep 17 00:00:00 2001 From: Nora Neyland Date: Fri, 12 Dec 2014 17:39:17 +0000 Subject: [PATCH 06/22] fix merge issues --- Gemfile | 4 +- Gemfile.lock | 5 ++ lib/petshopserver.rb | 70 ++++++++++++++++++++++++++ lib/repos/cats_repo.rb | 0 lib/repos/dos_repo.rb | 0 lib/repos/shops_repo.rb | 0 lib/repos/users_repo.rb | 28 +++++++++++ server.rb | 106 ++++++++++++++++++++++++++++++++++------ 8 files changed, 196 insertions(+), 17 deletions(-) create mode 100644 lib/petshopserver.rb create mode 100644 lib/repos/cats_repo.rb create mode 100644 lib/repos/dos_repo.rb create mode 100644 lib/repos/shops_repo.rb create mode 100644 lib/repos/users_repo.rb diff --git a/Gemfile b/Gemfile index 66526d43..ae734a19 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ gem 'rspec', '~> 2.14.1' gem 'sinatra', '~> 1.4.5' gem 'sinatra-contrib', '~> 1.4.2' gem 'rest-client' +gem 'rack-flash3' +gem 'pg' # Testing -gem 'pry-byebug' +gem 'pry-byebug' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index bbeebb1e..bdcc68fb 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) @@ -22,6 +23,8 @@ GEM byebug (~> 3.4) pry (~> 0.10) rack (1.5.2) + rack-flash3 (1.0.5) + rack rack-protection (1.5.3) rack rack-test (0.6.2) @@ -55,7 +58,9 @@ PLATFORMS ruby DEPENDENCIES + pg pry-byebug + rack-flash3 rest-client rspec (~> 2.14.1) sinatra (~> 1.4.5) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb new file mode 100644 index 00000000..a2a30ba5 --- /dev/null +++ b/lib/petshopserver.rb @@ -0,0 +1,70 @@ +require 'pg' + +require_relative 'petshopserver/repos/shops_repo.rb' +require_relative 'petshopserver/repos/cats_repo.rb' +require_relative 'petshopserver/repos/dogs_repo.rb' +require_relative 'petshopserver/repos/users_repo.rb' + +# in irb - +# db = Petshopserver.create_db_connection('petshopserver') +# Petshopserver.create_tables(db) +# Petshopserver.seed_db(db) + +module Petshopserver + def self.create_db_connection(dbname) + PG.connect(host: 'localhost', dbname: dbname) + end + + def self.clear_db(db) + db.exec <<-SQL + DELETE FROM shops; + DELETE FROM dogs; + DELETE FROM cats; + DELETE FROM users; + SQL + end + + def self.create_tables(db) + db.exec <<-SQL + CREATE TABLE IF NOT EXISTS users( + id SERIAL PRIMARY KEY, + username VARCHAR, + password VARCHAR + ); + CREATE TABLE IF NOT EXISTS shops( + id SERIAL PRIMARY KEY, + name VARCHAR + ); + CREATE TABLE IF NOT EXISTS cats( + id SERIAL PRIMARY KEY, + shop_id INTEGER references shops(id), + name VARCHAR, + user_id INTEGER references users(id), + adopted BOOLEAN + ); + CREATE TABLE IF NOT EXISTS dogs( + id SERIAL PRIMARY KEY, + shop_id INTEGER references shops(id), + name VARCHAR, + user_id INTEGER references users(id), + adopted BOOLEAN + ); + SQL + end + + def self.seed_db(db) + db.exec <<-SQL + INSERT INTO users (username, password) values ('anonymous', 'anonymous') + INSERT INTO users (username, password) values ('Jessica', '123') + SQL + end + + def self.drop_tables(db) + db.exec <<-SQL + DROP TABLE shops; + DROP TABLE dogs; + DROP TABLE cats; + DROP TABLE users; + SQL + end +end \ No newline at end of file diff --git a/lib/repos/cats_repo.rb b/lib/repos/cats_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/repos/dos_repo.rb b/lib/repos/dos_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/repos/shops_repo.rb b/lib/repos/shops_repo.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/repos/users_repo.rb b/lib/repos/users_repo.rb new file mode 100644 index 00000000..b1139376 --- /dev/null +++ b/lib/repos/users_repo.rb @@ -0,0 +1,28 @@ +module Petshopserver + class UsersRepo + # find a user by user ID. Intended to be used when + # someone is already authenticated. We keep their + # user id in the cookie. + def self.find db, user_id + sql = %q[SELECT * FROM users WHERE id = $1] + result = db.exec(sql, [user_id]) + result.first + end + + # find user by username. Intended to be used when + # someone tries to sign in. + def self.find_by_username db, username + sql = %q[SELECT * FROM users WHERE username = $1] + result = db.exec(sql, [username]) + result.first + end + + # when someone signs up use this method to save their + # information in the db. we're not encrypting passwords. + def self.save db, user_data + sql = %q[INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *] + result = db.exec(sql, [user_data[:username], user_data[:password]]) + result.first + end + end +end \ No newline at end of file diff --git a/server.rb b/server.rb index 07df95f4..95d7ae25 100644 --- a/server.rb +++ b/server.rb @@ -1,27 +1,87 @@ require 'sinatra' require 'sinatra/reloader' require 'rest-client' -require 'json' +require 'rack-flash' +require 'pry-byebug' +require 'json' #why? +#bundle exec ruby server.rb + + +require_relative 'lib/petshopserver.rb' + +# class Petshopserver::Server < Sinatra::Application + configure do + set :bind, '0.0.0.0' + enable :sessions + use Rack::Flash + end + + helpers do + def mydb + db = Petshopserver.create_db_connection 'petshopserver' + end + end + + # before do + # if session['user_id'] + # user_id = session['user_id'] + # db = Petshopserver.create_db_connection 'petshopserver' + # @current_user = Petshopserver::UsersRepo.find db, user_id + # else + # @current_user = {'username' => 'anonymous', 'id' => 1} + # end + # end + + +# class Petshopserver::Server < Sinatra::Application + + configure do + set :bind, '0.0.0.0' + enable :sessions + use Rack::Flash + end + +# before do +# if session['user_id'] # if it exists, the user is logged in +# user_id = session['user_id'] +# db = Petshopserver.create_db_connection 'petshopserver' +# @current_user = Petshopserver::UsersRepo.find db, user_id +# else +# @current_user = {'username' => 'anonymous', 'id' => 1} +# end +# end # # +# for session you use a symbol + # This is our only html view... # get '/' do - if session[:user_id] + if session['user_id'] # TODO: Grab user from database - @current_user = $sample_user + # @current_user = $sample_user + user_id = session['user_id'] + db = Petshopserver.create_db_connection 'petshopserver' + @current_user = Petshopserver::UsersRepo.find db, user_id end erb :index end +get '/logout' do + session.delete 'user_id' + redirect to '/' +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 @@ -29,18 +89,30 @@ password = params['password'] # TODO: Grab user by username from database and check password - user = { 'username' => 'alice', 'password' => '123' } + #user = { 'username' => 'alice', 'password' => '123' } + user = Petshopserver::UsersRepo.find_by_username(mydb, username) if password == user['password'] headers['Content-Type'] = 'application/json' + session['user_id'] = user['id'] + # + user[:cats] =[] + user[:dogs] =[] + + + user.to_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 + #$sample_user.to_json else status 401 end end +# user_id = session['user_id'] +# db = Petshopserver.create_db_connection 'petshopserver' +# @current_user = Petshopserver::UsersRepo.find db, user_id + # # # # # Cats # # # # # @@ -83,14 +155,16 @@ 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" } - ] -} +# $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" } +# ] +# } + +# end From 97d54854c4eae2834a2946bc5d54440bfa9c479e Mon Sep 17 00:00:00 2001 From: Jessica Meyer Date: Fri, 12 Dec 2014 11:55:14 -0600 Subject: [PATCH 07/22] add updated cat/dog tables --- lib/petshopserver.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index 59dc2e81..5a8ac9aa 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -34,7 +34,6 @@ def self.create_tables(db) password VARCHAR ); CREATE TABLE IF NOT EXISTS shops( - id SERIAL PRIMARY KEY, name VARCHAR ); @@ -43,20 +42,27 @@ def self.create_tables(db) shop_id INTEGER references shops(id), name VARCHAR, user_id INTEGER references users(id), - adopted BOOLEAN + adopted BOOLEAN, + imageUrl VARCHAR ); CREATE TABLE IF NOT EXISTS dogs( id SERIAL PRIMARY KEY, shop_id INTEGER references shops(id), name VARCHAR, user_id INTEGER references users(id), - adopted BOOLEAN + adopted BOOLEAN, + imageUrl VARCHAR + # happiness VARCHAR ); SQL end + def self.seed_db(db) db.exec <<-SQL + INSERT INTO shops (name, id) VALUES ($1, $2) + + INSERT INTO users (username, password) values ('anonymous', 'anonymous') INSERT INTO users (username, password) values ('Jessica', '123') SQL From ce6b6ad2ce4524b9c80a6b57551557a7d743c944 Mon Sep 17 00:00:00 2001 From: Nora Neyland Date: Fri, 12 Dec 2014 18:30:22 +0000 Subject: [PATCH 08/22] update dog/cat/shop repos --- lib/petshopserver.rb | 6 +----- lib/repos/cats_repo.rb | 32 ++++++++++++++++++++++++++++++++ lib/repos/dogs_repo.rb | 32 ++++++++++++++++++++++++++++++++ lib/repos/shops_repo.rb | 32 ++++++++++++++++++++++++++++++++ server.rb | 30 ------------------------------ 5 files changed, 97 insertions(+), 35 deletions(-) create mode 100644 lib/repos/dogs_repo.rb diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index 5a8ac9aa..cddf1e4b 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -52,17 +52,13 @@ def self.create_tables(db) user_id INTEGER references users(id), adopted BOOLEAN, imageUrl VARCHAR - # happiness VARCHAR ); SQL end def self.seed_db(db) - db.exec <<-SQL - INSERT INTO shops (name, id) VALUES ($1, $2) - - + db.exec <<-SQL INSERT INTO users (username, password) values ('anonymous', 'anonymous') INSERT INTO users (username, password) values ('Jessica', '123') SQL diff --git a/lib/repos/cats_repo.rb b/lib/repos/cats_repo.rb index e69de29b..75bd47fb 100644 --- a/lib/repos/cats_repo.rb +++ b/lib/repos/cats_repo.rb @@ -0,0 +1,32 @@ +module Petshop + class CatsRepo + def self.all db + sql = %q[SELECT * FROM cats] + result = db.exec(sql) + result.entries + end + + def self.find db, id + sql = %q[SELECT * FROM cats WHERE id = $1] + result = db.exec(sql, [id]) + result.first + end + + def self.save db, cat_name + if cat_name[:id] + sql = %q[UPDATE cats SET content = $1 WHERE id = $2 RETURNING *] + result = db.exec(sql, [cat_name[:name], cat_name[:id]]) + else + sql = %q[INSERT INTO cats (cat_name) VALUES ($1) RETURNING *] + result = db.exec(sql, [cat_name[:name], cat_name[:id]]) + end + result.first + end + + def self.destroy db, id + sql = %q[DELETE FROM cats WHERE id = $1] + db.exec(sql, [id]) + end + + end +end \ No newline at end of file diff --git a/lib/repos/dogs_repo.rb b/lib/repos/dogs_repo.rb new file mode 100644 index 00000000..305957af --- /dev/null +++ b/lib/repos/dogs_repo.rb @@ -0,0 +1,32 @@ +module Petshop + class DogsRepo + def self.all db + sql = %q[SELECT * FROM dogs] + result = db.exec(sql) + result.entries + end + + def self.find db, id + sql = %q[SELECT * FROM dogs WHERE id = $1] + result = db.exec(sql, [id]) + result.first + end + + def self.save db, dog_name + if dog_name[:id] + sql = %q[UPDATE dogs SET content = $1 WHERE id = $2 RETURNING *] + result = db.exec(sql, [dog_name[:name], dog_name[:id]]) + else + sql = %q[INSERT INTO dogs (dog_name) VALUES ($1) RETURNING *] + result = db.exec(sql, [dog_name[:name], dog_name[:id]]) + end + result.first + end + + def self.destroy db, id + sql = %q[DELETE FROM dogs WHERE id = $1] + db.exec(sql, [id]) + end + + end +end \ No newline at end of file diff --git a/lib/repos/shops_repo.rb b/lib/repos/shops_repo.rb index e69de29b..efb56c48 100644 --- a/lib/repos/shops_repo.rb +++ b/lib/repos/shops_repo.rb @@ -0,0 +1,32 @@ +module Petshop + class ShopsRepo + def self.all db + sql = %q[SELECT * FROM shops] + result = db.exec(sql) + result.entries + end + + def self.find db, id + sql = %q[SELECT * FROM shops WHERE id = $1] + result = db.exec(sql, [id]) + result.first + end + + def self.save db, shop_name + if shop_name[:id] + sql = %q[UPDATE shops SET content = $1 WHERE id = $2 RETURNING *] + result = db.exec(sql, [shop_name[:name], shop_name[:id]]) + else + sql = %q[INSERT INTO shops (shop_name) VALUES ($1) RETURNING *] + result = db.exec(sql, [shop_name[:name], shop_name[:id]]) + end + result.first + end + + def self.destroy db, id + sql = %q[DELETE FROM shops WHERE id = $1] + db.exec(sql, [id]) + end + + end +end \ No newline at end of file diff --git a/server.rb b/server.rb index 91e68c4b..8ede5bce 100644 --- a/server.rb +++ b/server.rb @@ -23,27 +23,6 @@ def mydb end end -<<<<<<< HEAD - # before do - # if session['user_id'] - # user_id = session['user_id'] - # db = Petshopserver.create_db_connection 'petshopserver' - # @current_user = Petshopserver::UsersRepo.find db, user_id - # else - # @current_user = {'username' => 'anonymous', 'id' => 1} - # end - # end - - -# class Petshopserver::Server < Sinatra::Application - - configure do - set :bind, '0.0.0.0' - enable :sessions - use Rack::Flash - end -======= ->>>>>>> a09dc5b9e41cb282b15d8e39217e472b565e89b3 # before do # if session['user_id'] # if it exists, the user is logged in @@ -65,11 +44,7 @@ def mydb # @current_user = $sample_user user_id = session['user_id'] db = Petshopserver.create_db_connection 'petshopserver' -<<<<<<< HEAD - @current_user = Petshopserver::UsersRepo.find db, user_id -======= @current_user = Petshopserver::UsersRepo.find mydb, user_id ->>>>>>> a09dc5b9e41cb282b15d8e39217e472b565e89b3 end erb :index end @@ -174,9 +149,4 @@ def mydb # ] # } -<<<<<<< HEAD -# end -======= - ->>>>>>> a09dc5b9e41cb282b15d8e39217e472b565e89b3 From a52496ba821e48d8f2199ae3dd1e253a86ee44c6 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 12:30:36 -0600 Subject: [PATCH 09/22] remove restclient and add seed_shops --- lib/petshopserver.rb | 50 +++++++++++++++++++++++++++++++++++++++----- server.rb | 8 ++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index 5a8ac9aa..8a8609b8 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -52,7 +52,6 @@ def self.create_tables(db) user_id INTEGER references users(id), adopted BOOLEAN, imageUrl VARCHAR - # happiness VARCHAR ); SQL end @@ -60,9 +59,6 @@ def self.create_tables(db) def self.seed_db(db) db.exec <<-SQL - INSERT INTO shops (name, id) VALUES ($1, $2) - - INSERT INTO users (username, password) values ('anonymous', 'anonymous') INSERT INTO users (username, password) values ('Jessica', '123') SQL @@ -76,4 +72,48 @@ def self.drop_tables(db) DROP TABLE users; SQL end -end + +def self.seed_shops_db(db) + shops = JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops") + shops.shift + # cats = shops.map do |shop| + # shop_id = shop['id'] + # JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/cats") + # end + + # dogs = shops.map do |shop| + # shop_id = shop['id'] + # JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs") + # end + + save_shops_to_db(shops,db) + # save_cats_to_db(cats.flatten) + # save_dogs_to_db(dogs.flatten) + end + + def self.save_shops_to_db(shops,db) + sq = [] + # "(1, 'Jo's Palace'), (2, Nicks Shop)" + shops.each do |shop| + # if shop["name"].include? ("'") + # sq << shop + # shops.delete(shop) + # # k = shops.count + # end + # sq + shop["name"].gsub(/'/, "///'") + end + values = shops.map do |shop| + id = shop['id'].to_i + name = shop['name'] + db.escape_string(name.to_s) + ["(#{id}, '#{name}')"] + end + values = values.flatten.join(',') + sql = %Q[ + insert into shops (id, name) values #{values} + ] + + db.exec(sql) + end +end \ No newline at end of file diff --git a/server.rb b/server.rb index 11ee4e65..8ff80052 100644 --- a/server.rb +++ b/server.rb @@ -43,7 +43,6 @@ def mydb # TODO: Grab user from database # @current_user = $sample_user user_id = session['user_id'] - db = Petshopserver.create_db_connection 'petshopserver' @current_user = Petshopserver::UsersRepo.find mydb, user_id end erb :index @@ -59,11 +58,14 @@ def mydb # get '/shops' do - headers['Content-Type'] = 'application/json' - RestClient.get("http://pet-shop.api.mks.io/shops") + # shops = Petshopserver.seed_shops_db(mydb) + + # headers['Content-Type'] = 'application/json' + # RestClient.get("http://pet-shop.api.mks.io/shops") end + post '/signin' do params = JSON.parse request.body.read From acc1d45afec262005577e414cd8a3cdb8d19e8b5 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 13:20:51 -0600 Subject: [PATCH 10/22] add shops_repo --- lib/petshopserver/repos/shops_repo.rb | 32 +++++++++++++++++++++++++++ server.rb | 2 ++ 2 files changed, 34 insertions(+) diff --git a/lib/petshopserver/repos/shops_repo.rb b/lib/petshopserver/repos/shops_repo.rb index e69de29b..7d9aa9bc 100644 --- a/lib/petshopserver/repos/shops_repo.rb +++ b/lib/petshopserver/repos/shops_repo.rb @@ -0,0 +1,32 @@ +module Petshopserver + + class ShopsRepo + # find a user by user ID. Intended to be used when + # someone is already authenticated. We keep their + # user id in the cookie. + def self.find db, shop_id + sql = %q[SELECT * FROM shops WHERE id = $1] + result = db.exec(sql, [user_id]) + result.first + end + + # find shops by name. Intended to be used when + # someone tries to sign in. + def self.find_by_name db, name + sql = %q[SELECT * FROM shops WHERE name = $1] + result = db.exec(sql, [name]) + result.first + end + + def self.all(db) + db.exec("SELECT * FROM songs").to_a + end + + # save shop info + def self.save db, shop_data + sql = %q[INSERT INTO shops (name) VALUES ($1) RETURNING *] + result = db.exec(sql, [user_data[:name]]) + result.first + end + end +end diff --git a/server.rb b/server.rb index 972e777f..8a8c37a8 100644 --- a/server.rb +++ b/server.rb @@ -62,6 +62,8 @@ def mydb # headers['Content-Type'] = 'application/json' # RestClient.get("http://pet-shop.api.mks.io/shops") + + shops = Petshopserver::ShopsRepo.all mydb end From c25fdadaa45ba411612e41527d26bc0d7e5338f8 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 13:27:29 -0600 Subject: [PATCH 11/22] test petshop --- lib/petshopserver/repos/shops_repo.rb | 2 +- server.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/petshopserver/repos/shops_repo.rb b/lib/petshopserver/repos/shops_repo.rb index 7d9aa9bc..edbaca2e 100644 --- a/lib/petshopserver/repos/shops_repo.rb +++ b/lib/petshopserver/repos/shops_repo.rb @@ -19,7 +19,7 @@ def self.find_by_name db, name end def self.all(db) - db.exec("SELECT * FROM songs").to_a + db.exec("SELECT * FROM shops").to_a end # save shop info diff --git a/server.rb b/server.rb index 8a8c37a8..acd5cb53 100644 --- a/server.rb +++ b/server.rb @@ -64,6 +64,7 @@ def mydb # RestClient.get("http://pet-shop.api.mks.io/shops") shops = Petshopserver::ShopsRepo.all mydb + shops.to_json end From 6e02a8d9161fbaf681a649f9424e62b5c33dea6f Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 13:42:29 -0600 Subject: [PATCH 12/22] change update tables dogs and cats --- lib/petshopserver.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index 9f4e1454..f3c88ade 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -37,21 +37,22 @@ def self.create_tables(db) id SERIAL PRIMARY KEY, name VARCHAR ); - CREATE TABLE IF NOT EXISTS cats( - id SERIAL PRIMARY KEY, - shop_id INTEGER references shops(id), - name VARCHAR, - user_id INTEGER references users(id), - adopted BOOLEAN, - imageUrl VARCHAR - ); CREATE TABLE IF NOT EXISTS dogs( - id SERIAL PRIMARY KEY, - shop_id INTEGER references shops(id), - name VARCHAR, - user_id INTEGER references users(id), - adopted BOOLEAN, - imageUrl VARCHAR + id int, + shopId int references shops(id), + name varchar, + imageUrl varchar, + happiness int, + adopted boolean, + user_id INTEGER references users(id) + ); + CREATE TABLE IF NOT EXISTS cats( + id int, + shopId int references shops(id), + name varchar, + imageUrl varchar, + adopted boolean, + user_id INTEGER references users(id) ); SQL end From 6deabf5f826394785f963a0f1c66ac2578d42324 Mon Sep 17 00:00:00 2001 From: Jessica Meyer Date: Fri, 12 Dec 2014 13:55:41 -0600 Subject: [PATCH 13/22] setup rspec and add spec files --- Gemfile | 5 +++++ Gemfile.lock | 1 + lib/petshopserver.rb | 4 +--- server.rb | 11 +++++------ spec/cats_repo_spec.rb | 2 ++ spec/dogs_repo_spec.rb | 1 + spec/petshopserver_spec.rb | 1 + spec/server_spec.rb | 1 + spec/shops_repo_spec.rb | 1 + spec/spec_helper.rb | 14 ++++++++++++++ spec/users_repo_spec.rb | 1 + 11 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 spec/cats_repo_spec.rb create mode 100644 spec/dogs_repo_spec.rb create mode 100644 spec/petshopserver_spec.rb create mode 100644 spec/server_spec.rb create mode 100644 spec/shops_repo_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/users_repo_spec.rb diff --git a/Gemfile b/Gemfile index ae734a19..cfb1cb47 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,11 @@ gem 'sinatra-contrib', '~> 1.4.2' gem 'rest-client' gem 'rack-flash3' gem 'pg' +# added for rspec portion group block + +group :test do + gem 'rack-test' +end # Testing gem 'pry-byebug' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index bdcc68fb..12bd3520 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -61,6 +61,7 @@ DEPENDENCIES pg pry-byebug rack-flash3 + rack-test rest-client rspec (~> 2.14.1) sinatra (~> 1.4.5) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index 5a8ac9aa..fe8be76b 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -7,6 +7,7 @@ # in irb - +# load 'file_name.rb' # db = Petshopserver.create_db_connection('petshopserver') # Petshopserver.create_tables(db) # Petshopserver.seed_db(db) @@ -60,9 +61,6 @@ def self.create_tables(db) def self.seed_db(db) db.exec <<-SQL - INSERT INTO shops (name, id) VALUES ($1, $2) - - INSERT INTO users (username, password) values ('anonymous', 'anonymous') INSERT INTO users (username, password) values ('Jessica', '123') SQL diff --git a/server.rb b/server.rb index 11ee4e65..285703c9 100644 --- a/server.rb +++ b/server.rb @@ -3,14 +3,14 @@ require 'rest-client' require 'rack-flash' require 'pry-byebug' -require 'json' #why? +require 'json' # why? +require 'pg' # new #bundle exec ruby server.rb - +#bundle exec rspec require_relative 'lib/petshopserver.rb' - -# class Petshopserver::Server < Sinatra::Application + configure do set :bind, '0.0.0.0' enable :sessions @@ -148,6 +148,5 @@ def mydb # { shopId: 1, name: "Leaf Pup", imageUrl: "http://i.imgur.com/kuSHji2.jpg", happiness: 2, id: 2, adopted: "true" } # ] # } - - + diff --git a/spec/cats_repo_spec.rb b/spec/cats_repo_spec.rb new file mode 100644 index 00000000..51f5efc4 --- /dev/null +++ b/spec/cats_repo_spec.rb @@ -0,0 +1,2 @@ +require 'spec_helper' + diff --git a/spec/dogs_repo_spec.rb b/spec/dogs_repo_spec.rb new file mode 100644 index 00000000..335cafc3 --- /dev/null +++ b/spec/dogs_repo_spec.rb @@ -0,0 +1 @@ +require 'spec_helper' \ No newline at end of file diff --git a/spec/petshopserver_spec.rb b/spec/petshopserver_spec.rb new file mode 100644 index 00000000..335cafc3 --- /dev/null +++ b/spec/petshopserver_spec.rb @@ -0,0 +1 @@ +require 'spec_helper' \ No newline at end of file diff --git a/spec/server_spec.rb b/spec/server_spec.rb new file mode 100644 index 00000000..335cafc3 --- /dev/null +++ b/spec/server_spec.rb @@ -0,0 +1 @@ +require 'spec_helper' \ No newline at end of file diff --git a/spec/shops_repo_spec.rb b/spec/shops_repo_spec.rb new file mode 100644 index 00000000..335cafc3 --- /dev/null +++ b/spec/shops_repo_spec.rb @@ -0,0 +1 @@ +require 'spec_helper' \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..96f3ee41 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,14 @@ +# require 'server' +require './lib/petshopserver' # ? + +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 \ No newline at end of file diff --git a/spec/users_repo_spec.rb b/spec/users_repo_spec.rb new file mode 100644 index 00000000..f8ec3695 --- /dev/null +++ b/spec/users_repo_spec.rb @@ -0,0 +1 @@ +require 'spec_helper' From 7c6684a1a553bbd4efba60b82de6999acbfd225e Mon Sep 17 00:00:00 2001 From: Nora Neyland Date: Fri, 12 Dec 2014 20:09:54 +0000 Subject: [PATCH 14/22] correct repo location --- lib/petshopserver/repos/cats_repo.rb | 32 +++++++++++++++++++++++++++ lib/petshopserver/repos/dogs_repo.rb | 32 +++++++++++++++++++++++++++ lib/petshopserver/repos/shops_repo.rb | 6 +++++ lib/repos/cats_repo.rb | 32 --------------------------- lib/repos/dogs_repo.rb | 32 --------------------------- lib/repos/dos_repo.rb | 0 lib/repos/shops_repo.rb | 32 --------------------------- lib/repos/users_repo.rb | 28 ----------------------- 8 files changed, 70 insertions(+), 124 deletions(-) delete mode 100644 lib/repos/cats_repo.rb delete mode 100644 lib/repos/dogs_repo.rb delete mode 100644 lib/repos/dos_repo.rb delete mode 100644 lib/repos/shops_repo.rb delete mode 100644 lib/repos/users_repo.rb diff --git a/lib/petshopserver/repos/cats_repo.rb b/lib/petshopserver/repos/cats_repo.rb index e69de29b..75bd47fb 100644 --- a/lib/petshopserver/repos/cats_repo.rb +++ b/lib/petshopserver/repos/cats_repo.rb @@ -0,0 +1,32 @@ +module Petshop + class CatsRepo + def self.all db + sql = %q[SELECT * FROM cats] + result = db.exec(sql) + result.entries + end + + def self.find db, id + sql = %q[SELECT * FROM cats WHERE id = $1] + result = db.exec(sql, [id]) + result.first + end + + def self.save db, cat_name + if cat_name[:id] + sql = %q[UPDATE cats SET content = $1 WHERE id = $2 RETURNING *] + result = db.exec(sql, [cat_name[:name], cat_name[:id]]) + else + sql = %q[INSERT INTO cats (cat_name) VALUES ($1) RETURNING *] + result = db.exec(sql, [cat_name[:name], cat_name[:id]]) + end + result.first + end + + def self.destroy db, id + sql = %q[DELETE FROM cats WHERE id = $1] + db.exec(sql, [id]) + end + + end +end \ No newline at end of file diff --git a/lib/petshopserver/repos/dogs_repo.rb b/lib/petshopserver/repos/dogs_repo.rb index e69de29b..305957af 100644 --- a/lib/petshopserver/repos/dogs_repo.rb +++ b/lib/petshopserver/repos/dogs_repo.rb @@ -0,0 +1,32 @@ +module Petshop + class DogsRepo + def self.all db + sql = %q[SELECT * FROM dogs] + result = db.exec(sql) + result.entries + end + + def self.find db, id + sql = %q[SELECT * FROM dogs WHERE id = $1] + result = db.exec(sql, [id]) + result.first + end + + def self.save db, dog_name + if dog_name[:id] + sql = %q[UPDATE dogs SET content = $1 WHERE id = $2 RETURNING *] + result = db.exec(sql, [dog_name[:name], dog_name[:id]]) + else + sql = %q[INSERT INTO dogs (dog_name) VALUES ($1) RETURNING *] + result = db.exec(sql, [dog_name[:name], dog_name[:id]]) + end + result.first + end + + def self.destroy db, id + sql = %q[DELETE FROM dogs WHERE id = $1] + db.exec(sql, [id]) + end + + end +end \ No newline at end of file diff --git a/lib/petshopserver/repos/shops_repo.rb b/lib/petshopserver/repos/shops_repo.rb index edbaca2e..1c941421 100644 --- a/lib/petshopserver/repos/shops_repo.rb +++ b/lib/petshopserver/repos/shops_repo.rb @@ -28,5 +28,11 @@ def self.save db, shop_data result = db.exec(sql, [user_data[:name]]) result.first end + + def self.destroy db, id + sql = %q[DELETE FROM shops WHERE id = $1] + db.exec(sql, [id]) + end + end end diff --git a/lib/repos/cats_repo.rb b/lib/repos/cats_repo.rb deleted file mode 100644 index 75bd47fb..00000000 --- a/lib/repos/cats_repo.rb +++ /dev/null @@ -1,32 +0,0 @@ -module Petshop - class CatsRepo - def self.all db - sql = %q[SELECT * FROM cats] - result = db.exec(sql) - result.entries - end - - def self.find db, id - sql = %q[SELECT * FROM cats WHERE id = $1] - result = db.exec(sql, [id]) - result.first - end - - def self.save db, cat_name - if cat_name[:id] - sql = %q[UPDATE cats SET content = $1 WHERE id = $2 RETURNING *] - result = db.exec(sql, [cat_name[:name], cat_name[:id]]) - else - sql = %q[INSERT INTO cats (cat_name) VALUES ($1) RETURNING *] - result = db.exec(sql, [cat_name[:name], cat_name[:id]]) - end - result.first - end - - def self.destroy db, id - sql = %q[DELETE FROM cats WHERE id = $1] - db.exec(sql, [id]) - end - - end -end \ No newline at end of file diff --git a/lib/repos/dogs_repo.rb b/lib/repos/dogs_repo.rb deleted file mode 100644 index 305957af..00000000 --- a/lib/repos/dogs_repo.rb +++ /dev/null @@ -1,32 +0,0 @@ -module Petshop - class DogsRepo - def self.all db - sql = %q[SELECT * FROM dogs] - result = db.exec(sql) - result.entries - end - - def self.find db, id - sql = %q[SELECT * FROM dogs WHERE id = $1] - result = db.exec(sql, [id]) - result.first - end - - def self.save db, dog_name - if dog_name[:id] - sql = %q[UPDATE dogs SET content = $1 WHERE id = $2 RETURNING *] - result = db.exec(sql, [dog_name[:name], dog_name[:id]]) - else - sql = %q[INSERT INTO dogs (dog_name) VALUES ($1) RETURNING *] - result = db.exec(sql, [dog_name[:name], dog_name[:id]]) - end - result.first - end - - def self.destroy db, id - sql = %q[DELETE FROM dogs WHERE id = $1] - db.exec(sql, [id]) - end - - end -end \ No newline at end of file diff --git a/lib/repos/dos_repo.rb b/lib/repos/dos_repo.rb deleted file mode 100644 index e69de29b..00000000 diff --git a/lib/repos/shops_repo.rb b/lib/repos/shops_repo.rb deleted file mode 100644 index efb56c48..00000000 --- a/lib/repos/shops_repo.rb +++ /dev/null @@ -1,32 +0,0 @@ -module Petshop - class ShopsRepo - def self.all db - sql = %q[SELECT * FROM shops] - result = db.exec(sql) - result.entries - end - - def self.find db, id - sql = %q[SELECT * FROM shops WHERE id = $1] - result = db.exec(sql, [id]) - result.first - end - - def self.save db, shop_name - if shop_name[:id] - sql = %q[UPDATE shops SET content = $1 WHERE id = $2 RETURNING *] - result = db.exec(sql, [shop_name[:name], shop_name[:id]]) - else - sql = %q[INSERT INTO shops (shop_name) VALUES ($1) RETURNING *] - result = db.exec(sql, [shop_name[:name], shop_name[:id]]) - end - result.first - end - - def self.destroy db, id - sql = %q[DELETE FROM shops WHERE id = $1] - db.exec(sql, [id]) - end - - end -end \ No newline at end of file diff --git a/lib/repos/users_repo.rb b/lib/repos/users_repo.rb deleted file mode 100644 index b1139376..00000000 --- a/lib/repos/users_repo.rb +++ /dev/null @@ -1,28 +0,0 @@ -module Petshopserver - class UsersRepo - # find a user by user ID. Intended to be used when - # someone is already authenticated. We keep their - # user id in the cookie. - def self.find db, user_id - sql = %q[SELECT * FROM users WHERE id = $1] - result = db.exec(sql, [user_id]) - result.first - end - - # find user by username. Intended to be used when - # someone tries to sign in. - def self.find_by_username db, username - sql = %q[SELECT * FROM users WHERE username = $1] - result = db.exec(sql, [username]) - result.first - end - - # when someone signs up use this method to save their - # information in the db. we're not encrypting passwords. - def self.save db, user_data - sql = %q[INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *] - result = db.exec(sql, [user_data[:username], user_data[:password]]) - result.first - end - end -end \ No newline at end of file From 448712df0964b5827f5a4be8761ec0188769255e Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 14:13:14 -0600 Subject: [PATCH 15/22] change update tables dogs and cats --- lib/petshopserver.rb | 17 +++++++++------ lib/petshopserver/repos/dogs_repo.rb | 32 ++++++++++++++++++++++++++++ server.rb | 8 +++---- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index f3c88ade..c29096e5 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -38,26 +38,29 @@ def self.create_tables(db) name VARCHAR ); CREATE TABLE IF NOT EXISTS dogs( - id int, + id SERIAL PRIMARY KEY, shopId int references shops(id), name varchar, imageUrl varchar, happiness int, - adopted boolean, - user_id INTEGER references users(id) + adopted boolean ); CREATE TABLE IF NOT EXISTS cats( - id int, + id SERIAL PRIMARY KEY, shopId int references shops(id), name varchar, imageUrl varchar, - adopted boolean, - user_id INTEGER references users(id) + adopted boolean + ); + CREATE TABLE IF NOT EXISTS userPets( + id SERIAL PRIMARY KEY, + user_id INTEGER references users(id), + cat_id INTEGER references cats(id), + dog_id INTEGER references dogs(id) ); SQL end - def self.seed_db(db) db.exec <<-SQL INSERT INTO users (username, password) values ('anonymous', 'anonymous') diff --git a/lib/petshopserver/repos/dogs_repo.rb b/lib/petshopserver/repos/dogs_repo.rb index e69de29b..305957af 100644 --- a/lib/petshopserver/repos/dogs_repo.rb +++ b/lib/petshopserver/repos/dogs_repo.rb @@ -0,0 +1,32 @@ +module Petshop + class DogsRepo + def self.all db + sql = %q[SELECT * FROM dogs] + result = db.exec(sql) + result.entries + end + + def self.find db, id + sql = %q[SELECT * FROM dogs WHERE id = $1] + result = db.exec(sql, [id]) + result.first + end + + def self.save db, dog_name + if dog_name[:id] + sql = %q[UPDATE dogs SET content = $1 WHERE id = $2 RETURNING *] + result = db.exec(sql, [dog_name[:name], dog_name[:id]]) + else + sql = %q[INSERT INTO dogs (dog_name) VALUES ($1) RETURNING *] + result = db.exec(sql, [dog_name[:name], dog_name[:id]]) + end + result.first + end + + def self.destroy db, id + sql = %q[DELETE FROM dogs WHERE id = $1] + db.exec(sql, [id]) + end + + end +end \ No newline at end of file diff --git a/server.rb b/server.rb index acd5cb53..68de5b5b 100644 --- a/server.rb +++ b/server.rb @@ -80,12 +80,12 @@ def mydb user = Petshopserver::UsersRepo.find_by_username(mydb, username) if password == user['password'] - headers['Content-Type'] = 'application/json' + # headers['Content-Type'] = 'application/json' session['user_id'] = user['id'] # - user[:cats] =[] - user[:dogs] =[] - + # user[:cats] =[] + # user[:dogs] =[] + user.to_json # TODO: Return all pets adopted by this user From b30dfae8385e8442d8639027646ff2f4156f74f7 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 15:12:24 -0600 Subject: [PATCH 16/22] load cats and dogs --- lib/petshopserver.rb | 65 +++++++++++++++++++-------- lib/petshopserver/repos/shops_repo.rb | 4 +- server.rb | 2 +- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index 6d6783fc..8795d691 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -78,34 +78,28 @@ def self.drop_tables(db) SQL end -def self.seed_shops_db(db) + def self.seed_db(db) shops = JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops") shops.shift - # cats = shops.map do |shop| - # shop_id = shop['id'] - # JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/cats") - # end + cats = shops.map do |shop| + shop_id = shop['id'] + JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/cats") + end - # dogs = shops.map do |shop| - # shop_id = shop['id'] - # JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs") - # end + dogs = shops.map do |shop| + shop_id = shop['id'] + JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs") + end - save_shops_to_db(shops,db) - # save_cats_to_db(cats.flatten) - # save_dogs_to_db(dogs.flatten) + # save_shops_to_db(shops,db) + save_cats_to_db(cats.flatten,db) + save_dogs_to_db(dogs.flatten,db) end def self.save_shops_to_db(shops,db) sq = [] # "(1, 'Jo's Palace'), (2, Nicks Shop)" shops.each do |shop| - # if shop["name"].include? ("'") - # sq << shop - # shops.delete(shop) - # # k = shops.count - # end - # sq shop["name"].gsub(/'/, "///'") end values = shops.map do |shop| @@ -121,4 +115,39 @@ def self.save_shops_to_db(shops,db) db.exec(sql) end + + + + def self.save_cats_to_db(cats,db) + sql = %q[ + INSERT INTO cats (shopId, name, imageUrl, adopted) + VALUES ($1, $2, $3, $4) + ] + cats.each do |cat| + name = cat['name'] + imageurl = cat['imageUrl'] + adopted = cat['adopted'] ? true : false + shopid = cat['shopId'] + + db.exec(sql, [shopid, name, imageurl, adopted]) + end + end + + + def self.save_dogs_to_db(dogs,db) + sql = %q[ + INSERT INTO dogs (shopId, name, imageUrl, happiness, adopted) + VALUES ($1, $2, $3, $4, $5) + ] + dogs.each do |dog| + name = dog['name'] + imageurl = dog['imageUrl'] + happiness = dog['happiness'].to_i + x ||= dog['adopted'] ||= dog['adoptedstatus'] + adopted = x ? true : false + shopid = dog['shopId'] + + db.exec(sql, [shopid, name, imageurl, happiness, adopted]) + end + end end diff --git a/lib/petshopserver/repos/shops_repo.rb b/lib/petshopserver/repos/shops_repo.rb index 1c941421..994b1aa2 100644 --- a/lib/petshopserver/repos/shops_repo.rb +++ b/lib/petshopserver/repos/shops_repo.rb @@ -6,7 +6,7 @@ class ShopsRepo # user id in the cookie. def self.find db, shop_id sql = %q[SELECT * FROM shops WHERE id = $1] - result = db.exec(sql, [user_id]) + result = db.exec(sql, [shop_id]) result.first end @@ -25,7 +25,7 @@ def self.all(db) # save shop info def self.save db, shop_data sql = %q[INSERT INTO shops (name) VALUES ($1) RETURNING *] - result = db.exec(sql, [user_data[:name]]) + result = db.exec(sql, [shop_data[:name]]) result.first end diff --git a/server.rb b/server.rb index 4c8f9769..f505d237 100644 --- a/server.rb +++ b/server.rb @@ -58,7 +58,7 @@ def mydb # get '/shops' do - # shops = Petshopserver.seed_shops_db(mydb) + shops = Petshopserver.seed_db(mydb) # headers['Content-Type'] = 'application/json' # RestClient.get("http://pet-shop.api.mks.io/shops") From 6ac36639df85664f304861421746b3ec049743c2 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 15:15:59 -0600 Subject: [PATCH 17/22] make shops cats dogs viewable to all --- lib/petshopserver.rb | 5 +---- server.rb | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index 8795d691..f6916616 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -91,7 +91,7 @@ def self.seed_db(db) JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops/#{shop_id}/dogs") end - # save_shops_to_db(shops,db) + save_shops_to_db(shops,db) save_cats_to_db(cats.flatten,db) save_dogs_to_db(dogs.flatten,db) end @@ -116,8 +116,6 @@ def self.save_shops_to_db(shops,db) db.exec(sql) end - - def self.save_cats_to_db(cats,db) sql = %q[ INSERT INTO cats (shopId, name, imageUrl, adopted) @@ -133,7 +131,6 @@ def self.save_cats_to_db(cats,db) end end - def self.save_dogs_to_db(dogs,db) sql = %q[ INSERT INTO dogs (shopId, name, imageUrl, happiness, adopted) diff --git a/server.rb b/server.rb index f505d237..f98b58d0 100644 --- a/server.rb +++ b/server.rb @@ -58,7 +58,7 @@ def mydb # get '/shops' do - shops = Petshopserver.seed_db(mydb) + # shopscatsdogs = Petshopserver.seed_db(mydb) # headers['Content-Type'] = 'application/json' # RestClient.get("http://pet-shop.api.mks.io/shops") From fd87abf2f892de697377c43435c0e1e4182b5bde Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 16:07:30 -0600 Subject: [PATCH 18/22] add cats and dogs db requests --- lib/petshopserver.rb | 10 +++++----- lib/petshopserver/repos/cats_repo.rb | 9 ++++++++- lib/petshopserver/repos/dogs_repo.rb | 9 ++++++++- server.rb | 15 ++++++++++----- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index f6916616..954483c9 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -40,17 +40,17 @@ def self.create_tables(db) ); CREATE TABLE IF NOT EXISTS dogs( id SERIAL PRIMARY KEY, - shopId int references shops(id), + "shopId" int references shops(id), name varchar, - imageUrl varchar, + "imageUrl" varchar, happiness int, adopted boolean ); CREATE TABLE IF NOT EXISTS cats( id SERIAL PRIMARY KEY, - shopId int references shops(id), + "shopId" int references shops(id), name varchar, - imageUrl varchar, + "imageUrl" varchar, adopted boolean ); CREATE TABLE IF NOT EXISTS userPets( @@ -78,7 +78,7 @@ def self.drop_tables(db) SQL end - def self.seed_db(db) + def self.seed_all_db(db) shops = JSON.parse RestClient.get("http://pet-shop.api.mks.io/shops") shops.shift cats = shops.map do |shop| diff --git a/lib/petshopserver/repos/cats_repo.rb b/lib/petshopserver/repos/cats_repo.rb index 75bd47fb..837c473a 100644 --- a/lib/petshopserver/repos/cats_repo.rb +++ b/lib/petshopserver/repos/cats_repo.rb @@ -1,11 +1,18 @@ -module Petshop +module Petshopserver class CatsRepo + def self.all db sql = %q[SELECT * FROM cats] result = db.exec(sql) result.entries end + def self.all_by_shop db, id + sql = %q[SELECT * FROM cats WHERE "shopId" = $1] + result = db.exec(sql, [id]) + result.entries + end + def self.find db, id sql = %q[SELECT * FROM cats WHERE id = $1] result = db.exec(sql, [id]) diff --git a/lib/petshopserver/repos/dogs_repo.rb b/lib/petshopserver/repos/dogs_repo.rb index 305957af..49822f85 100644 --- a/lib/petshopserver/repos/dogs_repo.rb +++ b/lib/petshopserver/repos/dogs_repo.rb @@ -1,11 +1,18 @@ -module Petshop +module Petshopserver class DogsRepo + def self.all db sql = %q[SELECT * FROM dogs] result = db.exec(sql) result.entries end + def self.all_by_shop db, id + sql = %q[SELECT * FROM dogs WHERE "shopId" = $1] + result = db.exec(sql, [id]) + result.first + end + def self.find db, id sql = %q[SELECT * FROM dogs WHERE id = $1] result = db.exec(sql, [id]) diff --git a/server.rb b/server.rb index f98b58d0..f105fd72 100644 --- a/server.rb +++ b/server.rb @@ -58,7 +58,7 @@ def mydb # get '/shops' do - # shopscatsdogs = Petshopserver.seed_db(mydb) + # shopscatsdogs = Petshopserver.seed_all_db(mydb) # headers['Content-Type'] = 'application/json' # RestClient.get("http://pet-shop.api.mks.io/shops") @@ -104,10 +104,13 @@ def mydb # Cats # # # # # get '/shops/:id/cats' do - headers['Content-Type'] = 'application/json' + # headers['Content-Type'] = 'application/json' id = params[:id] + cats = Petshopserver::CatsRepo.all_by_shop mydb, id + cats.to_json # TODO: Grab from database instead - RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") + # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/cats") + end put '/shops/:shop_id/cats/:id/adopt' do @@ -125,10 +128,12 @@ def mydb # Dogs # # # # # get '/shops/:id/dogs' do - headers['Content-Type'] = 'application/json' + # headers['Content-Type'] = 'application/json' id = params[:id] + dogs = Petshopserver::CatsRepo.all_by_shop mydb, id + dogs.to_json # TODO: Update database instead - RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") + # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") end put '/shops/:shop_id/dogs/:id/adopt' do From ad1eea681b80a486fff642946a39d29720a4858a Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Fri, 12 Dec 2014 17:09:53 -0600 Subject: [PATCH 19/22] fixed adopted link and added --- lib/petshopserver.rb | 17 ++++++++++++---- lib/petshopserver/repos/cats_repo.rb | 21 ++++++++++--------- lib/petshopserver/repos/dogs_repo.rb | 29 ++++++++++++++++++--------- lib/petshopserver/repos/shops_repo.rb | 2 ++ server.rb | 13 ++++++------ 5 files changed, 54 insertions(+), 28 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index 954483c9..c787086f 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -124,7 +124,7 @@ def self.save_cats_to_db(cats,db) cats.each do |cat| name = cat['name'] imageurl = cat['imageUrl'] - adopted = cat['adopted'] ? true : false + adopted = cat['adopted'] shopid = cat['shopId'] db.exec(sql, [shopid, name, imageurl, adopted]) @@ -140,11 +140,20 @@ def self.save_dogs_to_db(dogs,db) name = dog['name'] imageurl = dog['imageUrl'] happiness = dog['happiness'].to_i - x ||= dog['adopted'] ||= dog['adoptedstatus'] - adopted = x ? true : false + x ||= dog['adopted'] shopid = dog['shopId'] db.exec(sql, [shopid, name, imageurl, happiness, adopted]) end - end + end + + def self.boolean_type_cast object, key + if object[key] == 't' + object[key] = true + else + object[key] = false + end + object + end + end diff --git a/lib/petshopserver/repos/cats_repo.rb b/lib/petshopserver/repos/cats_repo.rb index 837c473a..e77b88ad 100644 --- a/lib/petshopserver/repos/cats_repo.rb +++ b/lib/petshopserver/repos/cats_repo.rb @@ -4,28 +4,31 @@ class CatsRepo def self.all db sql = %q[SELECT * FROM cats] result = db.exec(sql) - result.entries + result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') } end def self.all_by_shop db, id sql = %q[SELECT * FROM cats WHERE "shopId" = $1] result = db.exec(sql, [id]) - result.entries + result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') } + end def self.find db, id sql = %q[SELECT * FROM cats WHERE id = $1] result = db.exec(sql, [id]) - result.first + Petshopserver.boolean_type_cast(result.first, 'adopted') + #added class end - def self.save db, cat_name - if cat_name[:id] - sql = %q[UPDATE cats SET content = $1 WHERE id = $2 RETURNING *] - result = db.exec(sql, [cat_name[:name], cat_name[:id]]) + def self.save db, cat_data + if cat_data["id"] + id = cat_data["id"].to_i + sql = %q[UPDATE cats SET adopted = $1 WHERE id = $2 RETURNING *] + result = db.exec(sql, [cat_data["adopted"], cat_data["id"]]) else - sql = %q[INSERT INTO cats (cat_name) VALUES ($1) RETURNING *] - result = db.exec(sql, [cat_name[:name], cat_name[:id]]) + sql = %q[INSERT INTO cats ("shopId", name, "imageUrl", adopted) VALUES ($1,$2,$3,$4) RETURNING *] + result = db.exec(sql, [cat_data["shopId"], cat_data["name"], cat_data["imageUrl"],cat_data["adopted"]]) end result.first end diff --git a/lib/petshopserver/repos/dogs_repo.rb b/lib/petshopserver/repos/dogs_repo.rb index 49822f85..d7231c6c 100644 --- a/lib/petshopserver/repos/dogs_repo.rb +++ b/lib/petshopserver/repos/dogs_repo.rb @@ -4,28 +4,39 @@ class DogsRepo def self.all db sql = %q[SELECT * FROM dogs] result = db.exec(sql) - result.entries + result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') } end def self.all_by_shop db, id sql = %q[SELECT * FROM dogs WHERE "shopId" = $1] result = db.exec(sql, [id]) - result.first + result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') } + + # dogs = result.entries + # dogs.each do |d| + # if d['adopted'] == 't' + # d['adopted'] = true + # else + # d['adopted'] = false + # end + # end + # dogs end def self.find db, id sql = %q[SELECT * FROM dogs WHERE id = $1] result = db.exec(sql, [id]) - result.first + Petshopserver.boolean_type_cast(result.first, 'adopted') end - def self.save db, dog_name - if dog_name[:id] - sql = %q[UPDATE dogs SET content = $1 WHERE id = $2 RETURNING *] - result = db.exec(sql, [dog_name[:name], dog_name[:id]]) + def self.save db, dog_data + if dog_data["id"] + id = dog_data["id"].to_i + sql = %q[UPDATE dogs SET adopted = $1 WHERE id = $2 RETURNING *] + result = db.exec(sql, [dog_data["adopted"], dog_data["id"]]) else - sql = %q[INSERT INTO dogs (dog_name) VALUES ($1) RETURNING *] - result = db.exec(sql, [dog_name[:name], dog_name[:id]]) + sql = %q[INSERT INTO dogs ("shopId", name, "imageUrl", happiness, adopted) VALUES ($1,$2,$3,$4, $5) RETURNING *] + result = db.exec(sql, [dog_data["shopId"], dog_data["name"], dog_data["imageUrl"],dog_data["happiness"], dog_data["adopted"]]) end result.first end diff --git a/lib/petshopserver/repos/shops_repo.rb b/lib/petshopserver/repos/shops_repo.rb index 994b1aa2..49aebe97 100644 --- a/lib/petshopserver/repos/shops_repo.rb +++ b/lib/petshopserver/repos/shops_repo.rb @@ -4,6 +4,7 @@ class ShopsRepo # find a user by user ID. Intended to be used when # someone is already authenticated. We keep their # user id in the cookie. + def self.find db, shop_id sql = %q[SELECT * FROM shops WHERE id = $1] result = db.exec(sql, [shop_id]) @@ -12,6 +13,7 @@ def self.find db, shop_id # find shops by name. Intended to be used when # someone tries to sign in. + def self.find_by_name db, name sql = %q[SELECT * FROM shops WHERE name = $1] result = db.exec(sql, [name]) diff --git a/server.rb b/server.rb index f105fd72..1ec3e40a 100644 --- a/server.rb +++ b/server.rb @@ -114,12 +114,13 @@ def mydb end put '/shops/:shop_id/cats/:id/adopt' do - headers['Content-Type'] = 'application/json' + # 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') + # 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 @@ -130,7 +131,7 @@ def mydb get '/shops/:id/dogs' do # headers['Content-Type'] = 'application/json' id = params[:id] - dogs = Petshopserver::CatsRepo.all_by_shop mydb, id + dogs = Petshopserver::DogsRepo.all_by_shop mydb, id dogs.to_json # TODO: Update database instead # RestClient.get("http://pet-shop.api.mks.io/shops/#{id}/dogs") @@ -141,8 +142,8 @@ def mydb 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') + # 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 From 3f612a5046da7aeae33cd8d47a2e9b1937422f02 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Sun, 14 Dec 2014 18:32:15 -0600 Subject: [PATCH 20/22] fix server.rb --- lib/petshopserver.rb | 14 ++----- server.rb | 95 ++++++++++++++------------------------------ 2 files changed, 33 insertions(+), 76 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index c787086f..abde90f7 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -5,14 +5,6 @@ require_relative 'petshopserver/repos/dogs_repo.rb' require_relative 'petshopserver/repos/users_repo.rb' - -# in irb - -# load 'file_name.rb' -# db = Petshopserver.create_db_connection('petshopserver') -# Petshopserver.create_tables(db) -# Petshopserver.seed_db(db) - - module Petshopserver def self.create_db_connection(dbname) PG.connect(host: 'localhost', dbname: dbname) @@ -55,9 +47,9 @@ def self.create_tables(db) ); CREATE TABLE IF NOT EXISTS userPets( id SERIAL PRIMARY KEY, - user_id INTEGER references users(id), - cat_id INTEGER references cats(id), - dog_id INTEGER references dogs(id) + "user_id" INTEGER references users(id), + "cat_id" INTEGER references cats(id), + "dog_id" INTEGER references dogs(id) ); SQL end diff --git a/server.rb b/server.rb index 1ec3e40a..6ed5ad8f 100644 --- a/server.rb +++ b/server.rb @@ -3,8 +3,8 @@ require 'rest-client' require 'rack-flash' require 'pry-byebug' -require 'json' # why? -require 'pg' # new +require 'json' +require 'pg' #bundle exec ruby server.rb #bundle exec rspec @@ -23,128 +23,93 @@ def mydb end end - -# before do -# if session['user_id'] # if it exists, the user is logged in -# user_id = session['user_id'] -# db = Petshopserver.create_db_connection 'petshopserver' -# @current_user = Petshopserver::UsersRepo.find db, user_id -# else -# @current_user = {'username' => 'anonymous', 'id' => 1} -# end -# end -# # -# for session you use a symbol - -# This is our only html view... -# get '/' do if session['user_id'] - # TODO: Grab user from database - # @current_user = $sample_user user_id = session['user_id'] @current_user = Petshopserver::UsersRepo.find mydb, user_id + cats = Petshopserver::UsersRepo.find_all_cats_by_user_id mydb, user_id + dogs = Petshopserver::UsersRepo.find_all_dogs_by_user_id mydb, user_id + @current_user['cats'] = cats + @current_user['dogs'] = dogs + @current_user.to_json end erb :index end + get '/logout' do session.delete 'user_id' redirect to '/' end -# # -# ...the rest are JSON endpoints -# - get '/shops' do - # shopscatsdogs = Petshopserver.seed_all_db(mydb) - - # headers['Content-Type'] = 'application/json' - # RestClient.get("http://pet-shop.api.mks.io/shops") - + headers['Content-Type'] = 'application/json' shops = Petshopserver::ShopsRepo.all mydb shops.to_json 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 - #user = { 'username' => 'alice', 'password' => '123' } user = Petshopserver::UsersRepo.find_by_username(mydb, username) - + if password == user['password'] - # headers['Content-Type'] = 'application/json' + headers['Content-Type'] = 'application/json' session['user_id'] = user['id'] - # - # user[:cats] =[] - # user[:dogs] =[] - - - user.to_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 + @current_user = Petshopserver::UsersRepo.find mydb, user['id'] + cats = Petshopserver::UsersRepo.find_all_cats_by_user_id mydb, user['id'] + dogs = Petshopserver::UsersRepo.find_all_dogs_by_user_id mydb, user['id'] + @current_user['cats'] = cats + @current_user['dogs'] = dogs + @current_user.to_json else status 401 end end -# user_id = session['user_id'] -# db = Petshopserver.create_db_connection 'petshopserver' -# @current_user = Petshopserver::UsersRepo.find db, user_id # # # # # Cats # # # # # + get '/shops/:id/cats' do - # headers['Content-Type'] = 'application/json' + headers['Content-Type'] = 'application/json' id = params[:id] cats = Petshopserver::CatsRepo.all_by_shop mydb, id cats.to_json - # 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' + headers['Content-Type'] = 'application/json' shop_id = params[:shop_id] - id = params[:id] + cat_id = params[:id] + user_id = session['user_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 + cat = Petshopserver::UsersRepo.adopt_cat mydb, user_id, cat_id + cat.to_json end - # # # # # Dogs # # # # # + get '/shops/:id/dogs' do - # headers['Content-Type'] = 'application/json' + headers['Content-Type'] = 'application/json' id = params[:id] dogs = Petshopserver::DogsRepo.all_by_shop mydb, id dogs.to_json - # 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 + dog_id = params[:id] + user_id = session['user_id'] + dog = Petshopserver::UsersRepo.adopt_dog mydb, user_id, dog_id + dog.to_json end From 73e3b67f7b2782cfe6c8be6c66560010bc2d870d Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Sun, 14 Dec 2014 18:33:23 -0600 Subject: [PATCH 21/22] fix petshopserver.rb add adopt and find pets by user_id --- lib/petshopserver.rb | 4 ++-- lib/petshopserver/repos/dogs_repo.rb | 10 --------- lib/petshopserver/repos/shops_repo.rb | 5 +---- lib/petshopserver/repos/users_repo.rb | 31 ++++++++++++++++++++++++++- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/lib/petshopserver.rb b/lib/petshopserver.rb index abde90f7..bfaf0e61 100644 --- a/lib/petshopserver.rb +++ b/lib/petshopserver.rb @@ -57,7 +57,7 @@ def self.create_tables(db) def self.seed_db(db) db.exec <<-SQL INSERT INTO users (username, password) values ('anonymous', 'anonymous') - INSERT INTO users (username, password) values ('Jessica', '123') + # INSERT INTO users (username, password) values ('Jessica', '123') SQL end @@ -142,7 +142,7 @@ def self.save_dogs_to_db(dogs,db) def self.boolean_type_cast object, key if object[key] == 't' object[key] = true - else + elsif object[key] == 'f' object[key] = false end object diff --git a/lib/petshopserver/repos/dogs_repo.rb b/lib/petshopserver/repos/dogs_repo.rb index d7231c6c..7827e357 100644 --- a/lib/petshopserver/repos/dogs_repo.rb +++ b/lib/petshopserver/repos/dogs_repo.rb @@ -11,16 +11,6 @@ def self.all_by_shop db, id sql = %q[SELECT * FROM dogs WHERE "shopId" = $1] result = db.exec(sql, [id]) result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') } - - # dogs = result.entries - # dogs.each do |d| - # if d['adopted'] == 't' - # d['adopted'] = true - # else - # d['adopted'] = false - # end - # end - # dogs end def self.find db, id diff --git a/lib/petshopserver/repos/shops_repo.rb b/lib/petshopserver/repos/shops_repo.rb index 49aebe97..7d14439d 100644 --- a/lib/petshopserver/repos/shops_repo.rb +++ b/lib/petshopserver/repos/shops_repo.rb @@ -4,16 +4,13 @@ class ShopsRepo # find a user by user ID. Intended to be used when # someone is already authenticated. We keep their # user id in the cookie. - + def self.find db, shop_id sql = %q[SELECT * FROM shops WHERE id = $1] result = db.exec(sql, [shop_id]) result.first end - # find shops by name. Intended to be used when - # someone tries to sign in. - def self.find_by_name db, name sql = %q[SELECT * FROM shops WHERE name = $1] result = db.exec(sql, [name]) diff --git a/lib/petshopserver/repos/users_repo.rb b/lib/petshopserver/repos/users_repo.rb index d44f4508..c48397ad 100644 --- a/lib/petshopserver/repos/users_repo.rb +++ b/lib/petshopserver/repos/users_repo.rb @@ -22,8 +22,37 @@ def self.find_by_username db, username # information in the db. we're not encrypting passwords. def self.save db, user_data sql = %q[INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *] - result = db.exec(sql, [user_data[:username], user_data[:password]]) + result = db.exec(sql, [user_data['username'], user_data['password']]) result.first end + + def self.adopt_cat db, user_id, cat_id + sql = %q[INSERT INTO userPets (user_id, cat_id) VALUES ($1, $2) RETURNING *] + sql1 = %q[UPDATE cats SET adopted = $1 WHERE id = $2 RETURNING *] + db.exec(sql1, [true, cat_id]) + result = db.exec(sql, [user_id, cat_id]) + result.first + end + + def self.find_all_cats_by_user_id db, user_id + sql = %q[SELECT c.id, c."shopId", c.name, c."imageUrl", c.adopted FROM userPets u INNER JOIN cats c ON c.id = u.cat_id WHERE user_id = $1] + result = db.exec(sql, [user_id]) + result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') } + end + + def self.adopt_dog db, user_id, dog_id + sql = %q[INSERT INTO userPets (user_id, dog_id) VALUES ($1, $2) RETURNING *] + sql1 = %q[UPDATE dogs SET adopted = $1 WHERE id = $2 RETURNING *] + db.exec(sql1, [true, dog_id]) + result = db.exec(sql, [user_id, dog_id]) + result.first + end + + def self.find_all_dogs_by_user_id db, user_id + sql = %q[SELECT d.id, d."shopId", d.name, d."imageUrl", d.happiness, d.adopted FROM userPets u INNER JOIN dogs d ON d.id = u.dog_id WHERE user_id = $1] + result = db.exec(sql, [user_id]) + result.entries.map { |x| Petshopserver.boolean_type_cast(x, 'adopted') } + end + end end From 38dbcd8929e68000360f1e64b753b5c4f9a570f7 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Sun, 14 Dec 2014 18:39:37 -0600 Subject: [PATCH 22/22] clean up server.rb --- server.rb | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/server.rb b/server.rb index 6ed5ad8f..c6f9fd36 100644 --- a/server.rb +++ b/server.rb @@ -6,8 +6,6 @@ require 'json' require 'pg' -#bundle exec ruby server.rb -#bundle exec rspec require_relative 'lib/petshopserver.rb' @@ -43,21 +41,18 @@ def mydb end get '/shops' do - headers['Content-Type'] = 'application/json' shops = Petshopserver::ShopsRepo.all mydb shops.to_json end post '/signin' do params = JSON.parse request.body.read - username = params['username'] password = params['password'] - user = Petshopserver::UsersRepo.find_by_username(mydb, username) if password == user['password'] - headers['Content-Type'] = 'application/json' + # headers['Content-Type'] = 'application/json' session['user_id'] = user['id'] @current_user = Petshopserver::UsersRepo.find mydb, user['id'] cats = Petshopserver::UsersRepo.find_all_cats_by_user_id mydb, user['id'] @@ -71,23 +66,20 @@ def mydb end - # # # # +# # # # # Cats # # # # # get '/shops/:id/cats' do - headers['Content-Type'] = 'application/json' id = params[:id] cats = Petshopserver::CatsRepo.all_by_shop mydb, id cats.to_json end put '/shops/:shop_id/cats/:id/adopt' do - headers['Content-Type'] = 'application/json' shop_id = params[:shop_id] cat_id = params[:id] user_id = session['user_id'] - cat = Petshopserver::UsersRepo.adopt_cat mydb, user_id, cat_id cat.to_json end @@ -97,14 +89,12 @@ def mydb # # # # get '/shops/:id/dogs' do - headers['Content-Type'] = 'application/json' id = params[:id] dogs = Petshopserver::DogsRepo.all_by_shop mydb, id dogs.to_json end put '/shops/:shop_id/dogs/:id/adopt' do - headers['Content-Type'] = 'application/json' shop_id = params[:shop_id] dog_id = params[:id] user_id = session['user_id']