From b76a3dba795f185173d38e4a00ebdf8d1941d9d9 Mon Sep 17 00:00:00 2001 From: charles milam Date: Fri, 5 Dec 2014 11:27:48 -0600 Subject: [PATCH 01/39] Initial commit --- .gitignore | 2 ++ Gemfile | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e941da68..27e6018e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .bundle vendor/bundle .DS_Store +.gems + diff --git a/Gemfile b/Gemfile index f4b5c425..63c1e33f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.0.0' +ruby '2.1.3' gem 'rspec', '~> 2.14.1' gem 'pry-byebug' From 07e00dbcd6f2ac64958318e7c4744ccff9622eba Mon Sep 17 00:00:00 2001 From: charles milam Date: Fri, 5 Dec 2014 12:17:24 -0600 Subject: [PATCH 02/39] Modify method create_db_connection, remove localhost argument from PG connection. RESOLVES Issue #1 --- lib/library_plus.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/library_plus.rb b/lib/library_plus.rb index 9148018a..9e80bdbd 100644 --- a/lib/library_plus.rb +++ b/lib/library_plus.rb @@ -2,7 +2,7 @@ module Library def self.create_db_connection(dbname) - PG.connect(host: 'localhost', dbname: dbname) + PG.connect(dbname: dbname) end def self.clear_db(db) From 13861e4e22569094dcac6468c48275aa89bde5c6 Mon Sep 17 00:00:00 2001 From: charles milam Date: Fri, 5 Dec 2014 16:25:58 -0600 Subject: [PATCH 03/39] Modify save to add new user --- lib/library_plus/user_repo.rb | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index ef6ee87c..2a272e25 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -12,11 +12,32 @@ def self.find(db, user_data) end def self.save(db, user_data) - if user_data['id'] - # TODO: Update SQL statement + if user_data["id"] + sql_users = %Q[ + SELECT * + FROM users + WHERE id = user[:id] + ] + db.exec sql else - # TODO: Insert SQL statement + # prepared statement + statement_insert_user = %Q[ + insert into users + (name) + values ($1) + ] + db.prepare("insert_user", statement_insert_user) + # execute prepared statement + db.exec_prepared("insert_user", [user_data[:name]]) end + # grab the user via query and return it + sql_users = %Q[ + SELECT * + FROM users + WHERE name = $1 + ] + users = db.exec(sql_users, [user_data[:name]]) + users.first end def self.destroy(db, user_data) From 1af08a9827113a9e0616f5dcc960355a6a0abdc9 Mon Sep 17 00:00:00 2001 From: charles milam Date: Fri, 5 Dec 2014 17:22:28 -0600 Subject: [PATCH 04/39] Refactor --- lib/library_plus/user_repo.rb | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index 2a272e25..b1cdd94a 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -12,32 +12,29 @@ def self.find(db, user_data) end def self.save(db, user_data) - if user_data["id"] + # update user data if user exists, otherwise create new user + if user_data[:id] sql_users = %Q[ - SELECT * - FROM users - WHERE id = user[:id] + UPDATE users + set name =$1 + where id = $2 + returning * ] - db.exec sql + result = db.exec sql_users, user_data[:name], user_data[:id] else # prepared statement statement_insert_user = %Q[ insert into users (name) values ($1) + returning * ] db.prepare("insert_user", statement_insert_user) # execute prepared statement - db.exec_prepared("insert_user", [user_data[:name]]) + result = db.exec_prepared("insert_user", [user_data[:name]]) end - # grab the user via query and return it - sql_users = %Q[ - SELECT * - FROM users - WHERE name = $1 - ] - users = db.exec(sql_users, [user_data[:name]]) - users.first + + return result.first end def self.destroy(db, user_data) From f0dae4e0054df28ac889cb5555d2906b7d3ee51b Mon Sep 17 00:00:00 2001 From: charles milam Date: Fri, 5 Dec 2014 21:44:46 -0600 Subject: [PATCH 05/39] Modify method find to correctly find users by id. --- lib/library_plus/user_repo.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index b1cdd94a..ada6ba80 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -8,7 +8,15 @@ def self.all(db) end def self.find(db, user_data) - # TODO: Insert SQL statement + # find user by id + sql = %Q[ + select * + from users + where id = $1 + ] + result = db.exec sql, [user_data] + + return result.first end def self.save(db, user_data) From 5aa5f856572c2153098065a3617287b803c048ec Mon Sep 17 00:00:00 2001 From: charles milam Date: Fri, 5 Dec 2014 21:45:08 -0600 Subject: [PATCH 06/39] Modify to perform test on find method. --- spec/repos/user_repo_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/repos/user_repo_spec.rb b/spec/repos/user_repo_spec.rb index 94028dde..4e4fda11 100644 --- a/spec/repos/user_repo_spec.rb +++ b/spec/repos/user_repo_spec.rb @@ -38,7 +38,7 @@ def user_count(db) expect(user['name']).to eq "Alice" end - xit "finds users" do + it "finds users" do user = Library::UserRepo.save(db, :name => "Alice") retrieved_user = Library::UserRepo.find(db, user['id']) expect(retrieved_user['name']).to eq "Alice" From b331b1cb3a6bcda9983fe66caf51d7741e46b1ab Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 00:25:25 -0600 Subject: [PATCH 07/39] Modify for further testing of save method. --- spec/repos/user_repo_spec.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/repos/user_repo_spec.rb b/spec/repos/user_repo_spec.rb index 4e4fda11..edb6389c 100644 --- a/spec/repos/user_repo_spec.rb +++ b/spec/repos/user_repo_spec.rb @@ -44,14 +44,15 @@ def user_count(db) expect(retrieved_user['name']).to eq "Alice" end - xit "updates users" do + it "updates users" do user1 = Library::UserRepo.save(db, :name => "Alice") - user2 = Library::UserRepo.save(db, :name => "Alicia") + user2 = Library::UserRepo.save(db, { :id => user1['id'], :name => "Alicia" }) + expect(user2['id']).to eq(user1['id']) expect(user2['name']).to eq "Alicia" # Check for persistence - user3 = Library::UserRepo.find(user1['id']) + user3 = Library::UserRepo.find(db, user1['id']) expect(user3['name']).to eq "Alicia" end From ffacf354710f36b7332c08417593ba1a7d51c205 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 00:26:34 -0600 Subject: [PATCH 08/39] Modify save method to handle all tests sucessfully. --- lib/library_plus/user_repo.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index ada6ba80..dc459a55 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -14,32 +14,29 @@ def self.find(db, user_data) from users where id = $1 ] - result = db.exec sql, [user_data] + result = db.exec(sql, [user_data]) return result.first end def self.save(db, user_data) # update user data if user exists, otherwise create new user - if user_data[:id] + if user_data.include?(:id) sql_users = %Q[ UPDATE users set name =$1 where id = $2 returning * ] - result = db.exec sql_users, user_data[:name], user_data[:id] + result = db.exec(sql_users, [user_data[:name], user_data[:id]]) else - # prepared statement - statement_insert_user = %Q[ + sql_insert_user = %Q[ insert into users (name) values ($1) returning * ] - db.prepare("insert_user", statement_insert_user) - # execute prepared statement - result = db.exec_prepared("insert_user", [user_data[:name]]) + result = db.exec(sql_insert_user, [user_data[:name]]) end return result.first From ec32e28653868761c7fbbd7a6a25e212c026fa76 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 00:31:18 -0600 Subject: [PATCH 09/39] Modify method destroy to delete all records from users table. --- lib/library_plus/user_repo.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index dc459a55..72404ee0 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -43,7 +43,10 @@ def self.save(db, user_data) end def self.destroy(db, user_data) - # TODO: Delete SQL statement + sql = %Q[ + delete from users + ] + db.exec(sql) end end From c456038d21de119babb2709e6a4d3dfdbb5e635d Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 00:31:47 -0600 Subject: [PATCH 10/39] Modify to perform test of destroy method. --- spec/repos/user_repo_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/repos/user_repo_spec.rb b/spec/repos/user_repo_spec.rb index edb6389c..f66b422b 100644 --- a/spec/repos/user_repo_spec.rb +++ b/spec/repos/user_repo_spec.rb @@ -56,7 +56,7 @@ def user_count(db) expect(user3['name']).to eq "Alicia" end - xit "destroys users" do + it "destroys users" do user = Library::UserRepo.save(db, :name => "Alice") expect(user_count(db)).to eq 1 From 87a65811d810206611c64c89541fdc09f9de5f7e Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 15:25:06 -0600 Subject: [PATCH 11/39] Add /users endpoint. --- server.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server.rb b/server.rb index 91a87bcd..604843ad 100644 --- a/server.rb +++ b/server.rb @@ -6,3 +6,7 @@ get '/' do erb :index end + +get "/users" do + erb :users +end From 3ed577016ebb297b76ad3d218440a41c5ddf396c Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 15:25:19 -0600 Subject: [PATCH 12/39] Initial commit. --- views/users.erb | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 views/users.erb diff --git a/views/users.erb b/views/users.erb new file mode 100644 index 00000000..892e095b --- /dev/null +++ b/views/users.erb @@ -0,0 +1,3 @@ +
+

Manage Library Users

+
\ No newline at end of file From b73ec60e5968523e1232ee127f3f90d50f0c01a0 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 15:25:37 -0600 Subject: [PATCH 13/39] Initial commit. --- views/layout.erb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 views/layout.erb diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 00000000..c10136e9 --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,21 @@ + + + + Library+ + + + + +<%= yield %> +


+ + + \ No newline at end of file From 50c6141f29efb0a0817e185f1d7e22bc86a2a2af Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 16:05:59 -0600 Subject: [PATCH 14/39] Modify to link to users index --- views/index.erb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/views/index.erb b/views/index.erb index aba3235d..a808cd0b 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,9 @@ -

The Library Plus System

+
+

The Library Plus System

-

Welcome to your freedom!

+

Welcome to your freedom!

+ +
\ No newline at end of file From 67c049336654932abaf04bf17ecb08f6fce09505 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 16:06:29 -0600 Subject: [PATCH 15/39] Modify /users endpoint to point to users/index --- server.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.rb b/server.rb index 604843ad..9878017b 100644 --- a/server.rb +++ b/server.rb @@ -4,9 +4,9 @@ # set :bind, '0.0.0.0' # This is needed for Vagrant get '/' do - erb :index + erb :"index" end get "/users" do - erb :users + erb :"users/index" end From 7e282be3b955e8377ec8594d6a74eaa0f4be8fc6 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 16:07:09 -0600 Subject: [PATCH 16/39] Add h2 element --- views/users/index.erb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 views/users/index.erb diff --git a/views/users/index.erb b/views/users/index.erb new file mode 100644 index 00000000..6facc134 --- /dev/null +++ b/views/users/index.erb @@ -0,0 +1,4 @@ +
+

Manage Library Users

+

All Users

+
\ No newline at end of file From dbb6dcb279c8750bccd0efcc0571b47b1bc7c255 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 19:23:36 -0600 Subject: [PATCH 17/39] Modify /users endpoint to make db connection and retrieve user info. --- server.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server.rb b/server.rb index 9878017b..638a2dee 100644 --- a/server.rb +++ b/server.rb @@ -8,5 +8,8 @@ end get "/users" do + db = Library.create_db_connection('library_dev') + @users = Library::UserRepo.all(db) + erb :"users/index" end From 59765f6169b6349f51d18ac7dbca6b010a38becd Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 19:24:22 -0600 Subject: [PATCH 18/39] Modify, cleanup html code layout. --- views/index.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/views/index.erb b/views/index.erb index a808cd0b..a27af26a 100644 --- a/views/index.erb +++ b/views/index.erb @@ -4,6 +4,5 @@

Welcome to your freedom!

\ No newline at end of file From 67ca14d44e85720fb595ed276461a8d9d8464781 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 19:24:47 -0600 Subject: [PATCH 19/39] Modify to display a list of all users. --- views/users/index.erb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/views/users/index.erb b/views/users/index.erb index 6facc134..1bc84b73 100644 --- a/views/users/index.erb +++ b/views/users/index.erb @@ -1,4 +1,9 @@

Manage Library Users

-

All Users

+

All Users

+
    + <% @users.each do |u| %> +
  • <%= u["name"] %>
  • + <% end %> +
\ No newline at end of file From 021b4f09568b00ef12dd0365a72b26a91cf245e4 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 21:49:29 -0600 Subject: [PATCH 20/39] Add post /users endpoint to create new user in database. --- server.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server.rb b/server.rb index 638a2dee..562ebc59 100644 --- a/server.rb +++ b/server.rb @@ -10,6 +10,15 @@ get "/users" do db = Library.create_db_connection('library_dev') @users = Library::UserRepo.all(db) - + erb :"users/index" end + +post "/users" do + db = Library.create_db_connection('library_dev') + @name = params[:user_name] + + Library::UserRepo.save(db, :name => @name) + + redirect back +end From d7620254a4467c216dd61341a906674c5d1e210d Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 21:50:37 -0600 Subject: [PATCH 21/39] Add form for registering new users. --- views/users/index.erb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/views/users/index.erb b/views/users/index.erb index 1bc84b73..cdfb3676 100644 --- a/views/users/index.erb +++ b/views/users/index.erb @@ -1,9 +1,16 @@

Manage Library Users

-

All Users

+

All Current Users

    <% @users.each do |u| %>
  • <%= u["name"] %>
  • <% end %>
+
+

Register New User

+ + + + +
\ No newline at end of file From 6c5330121f0b8ea91464c1b6b9c8b693c3f4e00a Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 21:52:41 -0600 Subject: [PATCH 22/39] Initial commit. --- lib/library_plus/book_repo.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index 46409041..c3fcd26b 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -1 +1,5 @@ -# TODO +module Library + class BookRepo + + end +end From de46395ffd455f8e66bb6c2ba93c2028978a10ba Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 21:58:35 -0600 Subject: [PATCH 23/39] Add methods for CRUD operations. --- lib/library_plus/book_repo.rb | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index c3fcd26b..f44ae7a5 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -1,5 +1,52 @@ module Library class BookRepo + def self.all(db) + # Other code should not have to deal with the PG:Result. + # Therefore, convert the results into a plain array. + db.exec("SELECT * FROM books").to_a + end + def self.find(db, book_data) + # find book by id + sql = %Q[ + select * + from books + where id = $1 + ] + result = db.exec(sql, [book_data]) + + return result.first + end + + def self.save(db, book_data) + # update book data if book exists, otherwise create new book + if book_data.include?(:id) + sql_users = %Q[ + UPDATE books + set title =$1 + set author = $2 + where id = $3 + returning * + ] + result = db.exec(sql_users, [book_data[:title], book_data[:author], book_data[:id]]) + else + sql_insert_user = %Q[ + insert into books + (title, author) + values ($1, $2) + returning * + ] + result = db.exec(sql_insert_user, [book_data[:title], book_data[:author]]) + end + + return result.first + end + + def self.destroy(db, book_data) + sql = %Q[ + delete from books + ] + db.exec(sql) + end end end From ec3abfecc610e75a1aaa7ba2235614a02ecde297 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sat, 6 Dec 2014 22:00:37 -0600 Subject: [PATCH 24/39] Initial commit. --- spec/repos/book_repo_spec.rb | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 spec/repos/book_repo_spec.rb diff --git a/spec/repos/book_repo_spec.rb b/spec/repos/book_repo_spec.rb new file mode 100644 index 00000000..228e6352 --- /dev/null +++ b/spec/repos/book_repo_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' + +describe Library::BookRepo do + + def user_count(db) + db.exec("SELECT COUNT(*) FROM users")[0]["count"].to_i + end + + let(:db) { Library.create_db_connection('library_test') } + + before(:each) do + Library.clear_db(db) + end + + it "gets all users" do + db.exec("INSERT INTO users (name) VALUES ($1)", ["Alice"]) + db.exec("INSERT INTO users (name) VALUES ($1)", ["Bob"]) + + users = Library::BookRepo.all(db) + expect(users).to be_a Array + expect(users.count).to eq 2 + + names = users.map {|u| u['name'] } + expect(names).to include "Alice", "Bob" + end + + it "creates users" do + expect(user_count(db)).to eq 0 + + user = Library::BookRepo.save(db, :name => "Alice") + expect(user['id']).to_not be_nil + expect(user['name']).to eq "Alice" + + # Check for persistence + expect(user_count(db)).to eq 1 + + user = db.exec("SELECT * FROM users")[0] + expect(user['name']).to eq "Alice" + end + + it "finds users" do + user = Library::BookRepo.save(db, :name => "Alice") + retrieved_user = Library::BookRepo.find(db, user['id']) + expect(retrieved_user['name']).to eq "Alice" + end + + it "updates users" do + user1 = Library::BookRepo.save(db, :name => "Alice") + user2 = Library::BookRepo.save(db, { :id => user1['id'], :name => "Alicia" }) + + expect(user2['id']).to eq(user1['id']) + expect(user2['name']).to eq "Alicia" + + # Check for persistence + user3 = Library::BookRepo.find(db, user1['id']) + expect(user3['name']).to eq "Alicia" + end + + it "destroys users" do + user = Library::BookRepo.save(db, :name => "Alice") + expect(user_count(db)).to eq 1 + + Library::BookRepo.destroy(db, user['id']) + expect(user_count(db)).to eq 0 + end +end From dfaa5e577b6c61f7fb8970af0a0ad77b1d72b138 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 13:53:28 -0600 Subject: [PATCH 25/39] Modify to test for get of all books, and create book. --- spec/repos/book_repo_spec.rb | 69 ++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/spec/repos/book_repo_spec.rb b/spec/repos/book_repo_spec.rb index 228e6352..6f962bac 100644 --- a/spec/repos/book_repo_spec.rb +++ b/spec/repos/book_repo_spec.rb @@ -2,8 +2,8 @@ describe Library::BookRepo do - def user_count(db) - db.exec("SELECT COUNT(*) FROM users")[0]["count"].to_i + def book_count(db) + db.exec("SELECT COUNT(*) FROM books")[0]["count"].to_i end let(:db) { Library.create_db_connection('library_test') } @@ -12,55 +12,56 @@ def user_count(db) Library.clear_db(db) end - it "gets all users" do - db.exec("INSERT INTO users (name) VALUES ($1)", ["Alice"]) - db.exec("INSERT INTO users (name) VALUES ($1)", ["Bob"]) + it "gets all books" do + db.exec("INSERT INTO books (title, author) VALUES ($1, $2)", ["To Kill a Mockingbird", "Harper Lee"]) + db.exec("INSERT INTO books (title, author) VALUES ($1, $2)", ["Fellowship of the Ring", "J.R.R. Tolkien"]) - users = Library::BookRepo.all(db) - expect(users).to be_a Array - expect(users.count).to eq 2 + books = Library::BookRepo.all(db) + expect(books).to be_a Array + expect(books.count).to eq 2 - names = users.map {|u| u['name'] } - expect(names).to include "Alice", "Bob" + books = books.map {|u| u['title']} + expect(books).to include "To Kill a Mockingbird", "Fellowship of the Ring" end - it "creates users" do - expect(user_count(db)).to eq 0 + it "creates books" do + expect(book_count(db)).to eq 0 - user = Library::BookRepo.save(db, :name => "Alice") - expect(user['id']).to_not be_nil - expect(user['name']).to eq "Alice" + book = Library::BookRepo.save(db, :title => "To Kill a Mockingbird", :author => "Harper Lee") + expect(book['id']).to_not be_nil + expect(book['title']).to eq "To Kill a Mockingbird" + expect(book["author"]).to eq "Harper Lee" # Check for persistence - expect(user_count(db)).to eq 1 + expect(book_count(db)).to eq 1 - user = db.exec("SELECT * FROM users")[0] - expect(user['name']).to eq "Alice" + book = db.exec("SELECT * FROM books")[0] + expect(book['title']).to eq "To Kill a Mockingbird" end - it "finds users" do - user = Library::BookRepo.save(db, :name => "Alice") - retrieved_user = Library::BookRepo.find(db, user['id']) - expect(retrieved_user['name']).to eq "Alice" + xit "finds books" do + book = Library::BookRepo.save(db, :title => "Alice") + retrieved_user = Library::BookRepo.find(db, book['id']) + expect(retrieved_user['title']).to eq "Alice" end - it "updates users" do - user1 = Library::BookRepo.save(db, :name => "Alice") - user2 = Library::BookRepo.save(db, { :id => user1['id'], :name => "Alicia" }) + xit "updates books" do + book1 = Library::BookRepo.save(db, :title => "Alice") + book2 = Library::BookRepo.save(db, { :id => book1['id'], :title => "Alicia" }) - expect(user2['id']).to eq(user1['id']) - expect(user2['name']).to eq "Alicia" + expect(book2['id']).to eq(book1['id']) + expect(book2['title']).to eq "Alicia" # Check for persistence - user3 = Library::BookRepo.find(db, user1['id']) - expect(user3['name']).to eq "Alicia" + book3 = Library::BookRepo.find(db, book1['id']) + expect(book3['title']).to eq "Alicia" end - it "destroys users" do - user = Library::BookRepo.save(db, :name => "Alice") - expect(user_count(db)).to eq 1 + xit "destroys books" do + book = Library::BookRepo.save(db, :title => "Alice") + expect(book_count(db)).to eq 1 - Library::BookRepo.destroy(db, user['id']) - expect(user_count(db)).to eq 0 + Library::BookRepo.destroy(db, book['id']) + expect(book_count(db)).to eq 0 end end From 49cf30574468342e455ba5ffdfa09fbad43a26b7 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 13:55:19 -0600 Subject: [PATCH 26/39] Modify for create, clear and drop of books table. --- lib/library_plus.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/library_plus.rb b/lib/library_plus.rb index 9e80bdbd..5e8b6622 100644 --- a/lib/library_plus.rb +++ b/lib/library_plus.rb @@ -8,7 +8,7 @@ def self.create_db_connection(dbname) def self.clear_db(db) db.exec <<-SQL DELETE FROM users; - /* TODO: Clear rest of the tables (books, etc.) */ + DELETE FROM books; SQL end @@ -18,17 +18,26 @@ def self.create_tables(db) id SERIAL PRIMARY KEY, name VARCHAR ); - /* TODO: Create rest of the tables (books, etc.) */ + + create table books( + id SERIAL PRIMARY KEY, + title VARCHAR, + author VARCHAR + ); SQL end def self.drop_tables(db) db.exec <<-SQL DROP TABLE users; - /* TODO: Drop rest of the tables (books, etc.) */ + DROP TABLE books; SQL end + end require_relative 'library_plus/book_repo' require_relative 'library_plus/user_repo' + +# db = Library.create_db_connection "library_test" +# Library.create_tables db From d24feb441498ce047513a12af4e74762b9549562 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 13:55:47 -0600 Subject: [PATCH 27/39] Final. --- lib/library_plus/user_repo.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index 72404ee0..d290d9a2 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -51,3 +51,4 @@ def self.destroy(db, user_data) end end + From 029dfdc4ebaac3706b0699e854a5932a5bf98fcd Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 14:00:13 -0600 Subject: [PATCH 28/39] Modify to test find book by id. --- spec/repos/book_repo_spec.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spec/repos/book_repo_spec.rb b/spec/repos/book_repo_spec.rb index 6f962bac..f8537cbd 100644 --- a/spec/repos/book_repo_spec.rb +++ b/spec/repos/book_repo_spec.rb @@ -39,10 +39,11 @@ def book_count(db) expect(book['title']).to eq "To Kill a Mockingbird" end - xit "finds books" do - book = Library::BookRepo.save(db, :title => "Alice") - retrieved_user = Library::BookRepo.find(db, book['id']) - expect(retrieved_user['title']).to eq "Alice" + it "finds books" do + book = Library::BookRepo.save(db, :title => "To Kill a Mockingbird", :author => "Harper Lee") + retrieved_book = Library::BookRepo.find(db, book['id']) + expect(retrieved_book['title']).to eq "To Kill a Mockingbird" + expect(retrieved_book["author"]).to eq "Harper Lee" end xit "updates books" do From 24e23d3c592542ad89957028c2f075ea6001cb18 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 14:12:25 -0600 Subject: [PATCH 29/39] Modify method save to pass tests. --- lib/library_plus/book_repo.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index f44ae7a5..753d2e2b 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -23,8 +23,7 @@ def self.save(db, book_data) if book_data.include?(:id) sql_users = %Q[ UPDATE books - set title =$1 - set author = $2 + set title =$1, author = $2 where id = $3 returning * ] From 4a8961153a4dcc7acbb3ffa3cb4b8c2f4777c35f Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 14:12:59 -0600 Subject: [PATCH 30/39] Modify to test update book. --- spec/repos/book_repo_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/repos/book_repo_spec.rb b/spec/repos/book_repo_spec.rb index f8537cbd..ec32045f 100644 --- a/spec/repos/book_repo_spec.rb +++ b/spec/repos/book_repo_spec.rb @@ -46,16 +46,16 @@ def book_count(db) expect(retrieved_book["author"]).to eq "Harper Lee" end - xit "updates books" do - book1 = Library::BookRepo.save(db, :title => "Alice") - book2 = Library::BookRepo.save(db, { :id => book1['id'], :title => "Alicia" }) + it "updates books" do + book1 = Library::BookRepo.save(db, :title => "To Kill a Raven", :author => "Harper Lee") + book2 = Library::BookRepo.save(db, { :id => book1['id'], :title => "To Kill a Mockingbird", :author => book1["author"] }) expect(book2['id']).to eq(book1['id']) - expect(book2['title']).to eq "Alicia" + expect(book2['title']).to eq "To Kill a Mockingbird" # Check for persistence book3 = Library::BookRepo.find(db, book1['id']) - expect(book3['title']).to eq "Alicia" + expect(book3['title']).to eq "To Kill a Mockingbird" end xit "destroys books" do From c398e8afec33396ecde21771a252bc6856d4666f Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 19:45:12 -0600 Subject: [PATCH 31/39] Refactor methods find and save. --- lib/library_plus/book_repo.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index 753d2e2b..fe90255a 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -15,7 +15,7 @@ def self.find(db, book_data) ] result = db.exec(sql, [book_data]) - return result.first + result.first end def self.save(db, book_data) @@ -38,7 +38,7 @@ def self.save(db, book_data) result = db.exec(sql_insert_user, [book_data[:title], book_data[:author]]) end - return result.first + result.first end def self.destroy(db, book_data) From 0db6c82ebc1b56c3a8563683eec49ea2be5778e9 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 20:33:46 -0600 Subject: [PATCH 32/39] Add test for method checkouts. --- spec/repos/book_repo_spec.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/spec/repos/book_repo_spec.rb b/spec/repos/book_repo_spec.rb index ec32045f..32fad81d 100644 --- a/spec/repos/book_repo_spec.rb +++ b/spec/repos/book_repo_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require "./lib/library_plus/user_repo" describe Library::BookRepo do @@ -58,11 +59,20 @@ def book_count(db) expect(book3['title']).to eq "To Kill a Mockingbird" end - xit "destroys books" do - book = Library::BookRepo.save(db, :title => "Alice") + it "destroys books" do + book = Library::BookRepo.save(db, :title => "To Kill a Mockingbird", :author => "Harper Lee") expect(book_count(db)).to eq 1 Library::BookRepo.destroy(db, book['id']) expect(book_count(db)).to eq 0 end + + it "checks out books" do + user = Library::UserRepo.save(db, :name => "Alice") + book = Library::BookRepo.save(db, :title => "Foundation", :author => "Isaac Asimov") + + checkout = Library::BookRepo.check_out(db, :book_id => book['id'], :user_id => user["id"]) + expect(checkout["user_id"]).to_not be_nil + expect(checkout["book_id"]).to_not be_nil + end end From 92b75892f3baaee0d81bc77a9c1ed65d6016355b Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 20:35:24 -0600 Subject: [PATCH 33/39] Modify create tables method to create checkouts table. Modifiy method drop_tables to cascade for foreign keys. Modify method clear to cascade for foreign keys. --- lib/library_plus.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/library_plus.rb b/lib/library_plus.rb index 5e8b6622..b66ff02b 100644 --- a/lib/library_plus.rb +++ b/lib/library_plus.rb @@ -7,8 +7,9 @@ def self.create_db_connection(dbname) def self.clear_db(db) db.exec <<-SQL - DELETE FROM users; - DELETE FROM books; + DELETE FROM users cascade; + DELETE FROM books cascade; + DELETE FROM checkouts; SQL end @@ -24,13 +25,21 @@ def self.create_tables(db) title VARCHAR, author VARCHAR ); + + create table checkouts( + book_id integer + references books(id) on delete cascade, + user_id integer + references users(id) on delete cascade + ); SQL end def self.drop_tables(db) db.exec <<-SQL - DROP TABLE users; - DROP TABLE books; + DROP TABLE if exists users cascade; + DROP TABLE if exists books cascade; + DROP TABLE if exists checkouts SQL end @@ -40,4 +49,5 @@ def self.drop_tables(db) require_relative 'library_plus/user_repo' # db = Library.create_db_connection "library_test" +# Library.drop_tables db # Library.create_tables db From 5b0d7d11ee734fcfa332ac8c7a3c4cca869954e8 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 21:16:04 -0600 Subject: [PATCH 34/39] Add endpoints get and push for /checkouts. --- server.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/server.rb b/server.rb index 562ebc59..60d98411 100644 --- a/server.rb +++ b/server.rb @@ -22,3 +22,38 @@ redirect back end + +get "/books" do + db = Library.create_db_connection('library_dev') + @books = Library::BookRepo.all(db) + + erb :"books/index" +end + +post "/books" do + db = Library.create_db_connection('library_dev') + @title = params[:title] + @author = params[:author] + + Library::BookRepo.save(db, :title => @title, :author => @author) + + redirect back +end + +get "/checkouts" do + db = Library.create_db_connection('library_dev') + @books = Library::BookRepo.all(db) + @users = Library::UserRepo.all(db) + + erb :"checkouts/index" +end + +post "/checkouts" do + db = Library.create_db_connection('library_dev') + @book_id = params[:book_id] + @user_id = params[:user_id] + + Library::BookRepo.check_out(db, :book_id => @book_id, :user_id => @user_id) + + redirect back +end From 73021c304258017b3126359793578456f8589076 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 21:16:39 -0600 Subject: [PATCH 35/39] Initial commit. --- views/checkouts/index.erb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 views/checkouts/index.erb diff --git a/views/checkouts/index.erb b/views/checkouts/index.erb new file mode 100644 index 00000000..ba4b1cce --- /dev/null +++ b/views/checkouts/index.erb @@ -0,0 +1,26 @@ +
+

Checkout Library Books

+

All Books

+

Book ID - Book Title/Author

+
    + <% @books.each do |b| %> +
  • <%= b["id"] %> - <%= b["title"] %> : <%= b["author"] %>
  • + <% end %> +
+

All Registered Users

+

User ID - User Name

+
    + <% @users.each do |u| %> +
  • <%= u["id"] %> - <%= u["name"] %>
  • + <% end %> +
+
+

Checkout Book

+ + + + + + +
+
\ No newline at end of file From 335dda5b25100c900c9d4e0f78b4f8e7d41cbc45 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 21:16:54 -0600 Subject: [PATCH 36/39] Add link to checkout books. --- views/index.erb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/views/index.erb b/views/index.erb index a27af26a..3e2f42cb 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,8 +1,9 @@
-

The Library Plus System

- +

The Library+ System

Welcome to your freedom!

\ No newline at end of file From b085a4acbe68ad87ac6107620ff5d8da409ecd02 Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 22:13:40 -0600 Subject: [PATCH 37/39] Add endpoint get /checkouts/books-out --- server.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/server.rb b/server.rb index 60d98411..b3f74a1f 100644 --- a/server.rb +++ b/server.rb @@ -57,3 +57,10 @@ redirect back end + +get "/checkouts/books-out" do + db = Library.create_db_connection("library_dev") + @checkouts = Library::BookRepo.get_check_outs(db) + + erb :"checkouts/books-out" +end \ No newline at end of file From 946daf1c2115297abab08a40ec5fff11b2d1472a Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 22:14:23 -0600 Subject: [PATCH 38/39] Add link to view checked out books. --- views/index.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/views/index.erb b/views/index.erb index 3e2f42cb..a413607a 100644 --- a/views/index.erb +++ b/views/index.erb @@ -5,5 +5,6 @@
  • Manage Users
  • Manage Books
  • Checkout Books
  • +
  • View Books Checked Out
  • \ No newline at end of file From 3038134e20ee676fbf38e4fb3237f175189849fe Mon Sep 17 00:00:00 2001 From: charles milam Date: Sun, 7 Dec 2014 22:15:04 -0600 Subject: [PATCH 39/39] Add method get_check_outs --- lib/library_plus/book_repo.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index fe90255a..0bbb69ca 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -41,6 +41,35 @@ def self.save(db, book_data) result.first end + def self.check_out(db, check_out_data) + check_out_data.each {|c| puts c} + sql = %Q[ + insert into checkouts + (book_id, user_id) + values ($1, $2) + returning * + ] + result = db.exec(sql, [check_out_data[:book_id], check_out_data[:user_id]]) + end + + def self.get_check_outs(db) + sql = %Q[ + SELECT + books.title, + books.author, + books.id, + users.name + FROM + books, + checkouts, + users + WHERE + books.id = checkouts.book_id AND + users.id = checkouts.user_id + ] + db.exec(sql).to_a + end + def self.destroy(db, book_data) sql = %Q[ delete from books