From 467b00481d39dc09bf523073e62a701c93dbb496 Mon Sep 17 00:00:00 2001 From: Gilbert Date: Tue, 4 Mar 2014 08:59:37 -0600 Subject: [PATCH 01/16] First commit --- Gemfile | 4 +- library.rb | 37 +++++++ spec/.gitkeep | 0 spec/library_spec.rb | 223 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 library.rb delete mode 100644 spec/.gitkeep create mode 100644 spec/library_spec.rb diff --git a/Gemfile b/Gemfile index 5efe6bff..61e75f45 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.1.0' +ruby '2.1.1' gem 'rspec', '~> 2.14.0' -gem 'pry-debugger', '~> 0.2.2' +# gem 'pry-debugger', '~> 0.2.2' diff --git a/library.rb b/library.rb new file mode 100644 index 00000000..af0312bc --- /dev/null +++ b/library.rb @@ -0,0 +1,37 @@ + +class Book + attr_reader :author + + def initialize(title, author) + @author = author + end +end + +class Borrower + def initialize(name) + end +end + +class Library + def initialize(name) + @books = [] + end + + def books + end + + def add_book(title, author) + end + + def check_out_book(book_id, borrower) + end + + def check_in_book(book) + end + + def available_books + end + + def borrowed_books + end +end diff --git a/spec/.gitkeep b/spec/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/spec/library_spec.rb b/spec/library_spec.rb new file mode 100644 index 00000000..d6146d56 --- /dev/null +++ b/spec/library_spec.rb @@ -0,0 +1,223 @@ +require "./library.rb" +# require 'pry-debugger' + +describe Book do + it "has a title and author, and nil id" do + book = Book.new("The Stranger", "Albert Camus") + + # binding.pry + + expect(book.title).to eq "The Stranger" + expect(book.author).to eq "Albert Camus" + expect(book.id).to be_nil + end + + xit "has a default status of available" do + book = Book.new + expect(book.status).to eq 'available' + end + + xit "can be checked out" do + book = Book.new + did_it_work = book.check_out + expect(did_it_work).to be_true + expect(book.status).to eq 'checked_out' + end + + xit "can't be checked out twice in a row" do + book = Book.new + did_it_work = book.check_out + expect(did_it_work).to eq(true) + + did_it_work_again = book.check_out + expect(did_it_work_again).to eq(false) + + expect(book.status).to eq 'checked_out' + end + + xit "can be checked in" do + book = Book.new + book.check_out + book.check_in + expect(book.status).to eq 'available' + end +end + +describe Borrower do + xit "has a name" do + borrower = Borrower.new("Mike") + expect(borrower.name).to eq "Mike" + end +end + +describe Library do + + xit "starts with an empty array of books" do + lib = Library.new + expect(lib.books.count).to eq(0) + end + + xit "add new books and assigns it an id" do + lib = Library.new + lib.register_new_book("Nausea", "Jean-Paul Sartre") + expect(lib.books.count).to eq(1) + + created_book = lib.books.first + expect(created_book.title).to eq "Nausea" + expect(created_book.author).to eq "Jean-Paul Sartre" + expect(created_book.id).to_not be_nil + end + + xit "can add multiple books" do + lib = Library.new + lib.register_new_book("One", "Bob") + lib.register_new_book("Two", "Bob") + lib.register_new_book("Three", "Bob") + + expect(lib.books.count).to eq(3) + end + + xit "allows a Borrower to check out a book by its id" do + lib = Library.new + lib.register_new_book("Green Eggs and Ham", "Dr. Seuss") + book_id = lib.books.first.id + + # Sam wants to check out Green Eggs and Ham + sam = Borrower.new('Sam-I-am') + book = lib.check_out_book(book_id, sam) + + # The checkout should return the book + expect(book).to be_a?(Book) + expect(book.title).to eq "Green Eggs and Ham" + + # The book should now be marked as checked out + expect(book.status).to eq 'checked_out' + end + + xit "knows who borrowed a book" do + lib = Library.new + lib.register_new_book("The Brothers Karamazov", "Fyodor Dostoesvky") + book_id = lib.books.first.id + + # Big Brother wants to check out The Brothers Karamazov + bro = Borrower.new('Big Brother') + book = lib.check_out_book(book_id, bro) + + # The Library should know that he checked out the book + expect( lib.get_borrower(book_id) ).to eq 'Big Brother' + end + + xit "does not allow a book to be checked out twice in a row" do + lib = Library.new + lib.register_new_book = Book.new("Surely You're Joking Mr. Feynman", "Richard Feynman") + book_id = lib.books.first.id + + # Leslie Nielsen wants to double check on that + nielsen = Borrower.new('Leslie Nielsen') + book = lib.check_out_book(book_id, nielsen) + + # The first time should be successful + expect(book).to be_a?(Book) + + # However, you can't check out the same book twice! + book_again = lib.check_out_book(book_id, nielsen) + expect(book_again).to be_nil + + son = Borrower.new('Leslie Nielsen the 2nd') + book_again = lib.check_out_book(book_id, son) + expect(book_again).to be_nil + end + + xit "allows a Borrower to check a book back in" do + lib = Library.new + lib.register_new_book("Finnegans Wake", "James Joyce") + book_id = lib.books.first.id + + # Bob wants to check out Finnegans Wake + bob = Borrower.new('Bob Bobber') + book = lib.check_out_book(book_id, bob) + + # o wait he changed his mind + lib.check_in_book(book) + + # The book should now be marked as available! + expect(book.status).to eq 'available' + end + + xit "does not allow a Borrower to check out more than one Book at any given time" do + # yeah it's a stingy library + lib = Library.new + lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") + lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") + lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") + + jackson = Borrower.new("Michael Jackson") + book_1 = lib.books[0] + book_2 = lib.books[1] + book_3 = lib.books[2] + + # The first two books should check out fine + book = lib.check_out_book(book_1.id, jackson) + expect(book.title).to eq "Eloquent JavaScript" + + book = lib.check_out_book(book_2.id, jackson) + expect(book.title).to eq "Essential JavaScript Design Patterns" + + # However, the third should return nil + book = lib.check_out_book(book_3.id, jackson) + expect(book).to be_nil + end + + xit "returns available books" do + lib = Library.new + lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") + lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") + lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") + + # At first, all books are available + expect(lib.available_books.count).to eq(3) + expect(lib.available_books.first).to be_a?(Book) + + jordan = Borrower.new("Michael Jordan") + book = lib.check_out_book(lib.available_books.first.id, jordan) + + # But now, there should only be two available books + expect(lib.available_books.count).to eq(2) + end + + xit "after a book it returned, it can be checked out again" do + lib = Library.new + lib.register_new_book("Harry Potter", "J. K. Rowling") + book_id = lib.books.first.id + + # First, we check out the book + vick = Borrower.new("Michael Vick") + book = lib.check_out_book(book_id, vick) + expect( lib.get_borrower(book_id) ).to eq 'Michael Vick' + + # When we check in a book, the Library does not care who checks it in + lib.check_in_book(book) + + # Another person should be able to check the book out + schumacher = Borrower.new("Michael Schumacher") + book = lib.check_out_book(book_id, schumacher) + expect( lib.get_borrower(book_id) ).to eq 'Michael Schumacher' + end + + xit "returns borrowed books" do + lib = Library.new + lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") + lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") + lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") + + # At first, no books are checked out + expect(lib.borrowed_books.count).to eq(0) + + kors = Borrower.new("Michael Kors") + book = lib.check_out_book(lib.borrowed_books.first.id, kors) + + # But now there should be one checked out book + expect(lib.borrowed_books.count).to eq(1) + expect(lib.borrowed_books.first).to be_a?(Book) + end +end From 3a0e4b155b937859b06da70c6912f4a2e1fbf62d Mon Sep 17 00:00:00 2001 From: Michael Ornellas Date: Wed, 23 Apr 2014 15:17:00 -0500 Subject: [PATCH 02/16] Fix syntax error in library_spec.rb Removed extraneous `?` marks from `be_a` in the specs. If this was an intentional error, it was too subtle to be very effective and proved to be a large source of confusion. --- spec/library_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/library_spec.rb b/spec/library_spec.rb index d6146d56..bf2e5e69 100644 --- a/spec/library_spec.rb +++ b/spec/library_spec.rb @@ -87,7 +87,7 @@ book = lib.check_out_book(book_id, sam) # The checkout should return the book - expect(book).to be_a?(Book) + expect(book).to be_a(Book) expect(book.title).to eq "Green Eggs and Ham" # The book should now be marked as checked out @@ -117,7 +117,7 @@ book = lib.check_out_book(book_id, nielsen) # The first time should be successful - expect(book).to be_a?(Book) + expect(book).to be_a(Book) # However, you can't check out the same book twice! book_again = lib.check_out_book(book_id, nielsen) @@ -176,7 +176,7 @@ # At first, all books are available expect(lib.available_books.count).to eq(3) - expect(lib.available_books.first).to be_a?(Book) + expect(lib.available_books.first).to be_a(Book) jordan = Borrower.new("Michael Jordan") book = lib.check_out_book(lib.available_books.first.id, jordan) @@ -218,6 +218,6 @@ # But now there should be one checked out book expect(lib.borrowed_books.count).to eq(1) - expect(lib.borrowed_books.first).to be_a?(Book) + expect(lib.borrowed_books.first).to be_a(Book) end end From 421803fbe23020b4eec0808ffaf93938eaed608b Mon Sep 17 00:00:00 2001 From: Michael Ornellas Date: Mon, 2 Jun 2014 12:46:03 -0700 Subject: [PATCH 03/16] Update Gemfile ruby version to 2.0.0 --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 61e75f45..5a6180b5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.1.1' +ruby '2.0.0' gem 'rspec', '~> 2.14.0' # gem 'pry-debugger', '~> 0.2.2' From 2531c9a919a5de96c30a119e5fa200583e031182 Mon Sep 17 00:00:00 2001 From: Clay Stewart Date: Mon, 2 Jun 2014 15:32:22 -0500 Subject: [PATCH 04/16] rspec version bump --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 5a6180b5..f9a5bfb0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' ruby '2.0.0' -gem 'rspec', '~> 2.14.0' +gem 'rspec', '~> 2.14.1' # gem 'pry-debugger', '~> 0.2.2' From 9d24346cb514524e26f8acfa548c2a7884f96f3e Mon Sep 17 00:00:00 2001 From: Gilbert Date: Thu, 12 Jun 2014 13:30:07 -0500 Subject: [PATCH 05/16] Fix ruby version and debugger gem --- Gemfile | 2 +- Gemfile.lock | 22 ++++++++++------------ spec/library_spec.rb | 2 +- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index f9a5bfb0..1f478929 100644 --- a/Gemfile +++ b/Gemfile @@ -2,4 +2,4 @@ source 'https://rubygems.org' ruby '2.0.0' gem 'rspec', '~> 2.14.1' -# gem 'pry-debugger', '~> 0.2.2' +gem 'pry-byebug' diff --git a/Gemfile.lock b/Gemfile.lock index db516937..7e61f35f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,23 +1,21 @@ GEM remote: https://rubygems.org/ specs: + byebug (2.7.0) + columnize (~> 0.3) + debugger-linecache (~> 1.2) coderay (1.1.0) - columnize (0.3.6) - debugger (1.6.5) - columnize (>= 0.3.1) - debugger-linecache (~> 1.2.0) - debugger-ruby_core_source (~> 1.3.1) + columnize (0.8.9) debugger-linecache (1.2.0) - debugger-ruby_core_source (1.3.1) diff-lcs (1.2.5) method_source (0.8.2) pry (0.9.12.6) coderay (~> 1.0) method_source (~> 0.8) slop (~> 3.4) - pry-debugger (0.2.2) - debugger (~> 1.3) - pry (~> 0.9.10) + pry-byebug (1.3.2) + byebug (~> 2.7) + pry (~> 0.9.12) rspec (2.14.1) rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) @@ -26,11 +24,11 @@ GEM rspec-expectations (2.14.5) diff-lcs (>= 1.1.3, < 2.0) rspec-mocks (2.14.5) - slop (3.4.7) + slop (3.5.0) PLATFORMS ruby DEPENDENCIES - pry-debugger (~> 0.2.2) - rspec (~> 2.14.0) + pry-byebug + rspec (~> 2.14.1) diff --git a/spec/library_spec.rb b/spec/library_spec.rb index bf2e5e69..c26acabe 100644 --- a/spec/library_spec.rb +++ b/spec/library_spec.rb @@ -1,5 +1,5 @@ require "./library.rb" -# require 'pry-debugger' +require 'pry-byebug' describe Book do it "has a title and author, and nil id" do From 0f2bf23b2dda199173e5698703cd47ce1589a70f Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Tue, 2 Dec 2014 15:00:53 -0600 Subject: [PATCH 06/16] First commit - forgot to commit before --- library.rb | 62 +++++++++++++++++++++++++++++++++++++++++--- spec/library_spec.rb | 52 ++++++++++++++++++------------------- 2 files changed, 85 insertions(+), 29 deletions(-) diff --git a/library.rb b/library.rb index af0312bc..9727b562 100644 --- a/library.rb +++ b/library.rb @@ -1,34 +1,90 @@ class Book attr_reader :author + attr_reader :title + attr_reader :id + attr_accessor :status + @@book_id_count = 1 def initialize(title, author) @author = author + @title = title + @status = "available" + @id = @@book_id_count + @@book_id_count += 1 + end + + def check_out() + if @status == "available" + @status = "checked_out" + else + return false + end + end + + def check_in() + if @status == "checked_out" + @status = "available" + else + return false + end end end class Borrower + attr_reader :name + attr_accessor :checked def initialize(name) + @name = name + @num_checked = 0 end end class Library + attr_reader :books + attr_reader :name + attr_reader :borrowers def initialize(name) + @name = name @books = [] + @borrowers = {} end - def books + def register_new_book(title, author) + book = Book.new(title, author) + @books.push(book) end - def add_book(title, author) + def check_out_book(book_id, borrower) + checked = nil + @books.each do |book| + if book.id == book_id && book.status == "available" && borrower.num_checked.count <= 2 + book.check_out() + @borrowers[book_id] = borrower + borrower.num_checked +=1 + checked = book + end + end + checked end - def check_out_book(book_id, borrower) + def get_borrower(book_id) + @borrowers[book_id].name end def check_in_book(book) + book.check_in() + @borrowers.delete(book.id) end + def book + end + + def add_book(title, author) + end + + + def available_books end diff --git a/spec/library_spec.rb b/spec/library_spec.rb index c26acabe..18d54001 100644 --- a/spec/library_spec.rb +++ b/spec/library_spec.rb @@ -9,25 +9,25 @@ expect(book.title).to eq "The Stranger" expect(book.author).to eq "Albert Camus" - expect(book.id).to be_nil + expect(book.id).to_not be_nil end - xit "has a default status of available" do - book = Book.new + it "has a default status of available" do + book = Book.new("The Anger", "Spenser McChicken") expect(book.status).to eq 'available' end - xit "can be checked out" do - book = Book.new + it "can be checked out" do + book = Book.new("The Ger", "Spenser McChicken") did_it_work = book.check_out expect(did_it_work).to be_true expect(book.status).to eq 'checked_out' end - xit "can't be checked out twice in a row" do - book = Book.new + it "can't be checked out twice in a row" do + book = Book.new("The Dan", "Spenser McChicken") did_it_work = book.check_out - expect(did_it_work).to eq(true) + expect(did_it_work).to be_true did_it_work_again = book.check_out expect(did_it_work_again).to eq(false) @@ -35,8 +35,8 @@ expect(book.status).to eq 'checked_out' end - xit "can be checked in" do - book = Book.new + it "can be checked in" do + book = Book.new("The Danger", "Spenser McChicken") book.check_out book.check_in expect(book.status).to eq 'available' @@ -44,7 +44,7 @@ end describe Borrower do - xit "has a name" do + it "has a name" do borrower = Borrower.new("Mike") expect(borrower.name).to eq "Mike" end @@ -52,13 +52,13 @@ describe Library do - xit "starts with an empty array of books" do - lib = Library.new + it "starts with an empty array of books" do + lib = Library.new("Way up High") expect(lib.books.count).to eq(0) end - xit "add new books and assigns it an id" do - lib = Library.new + it "add new books and assigns it an id" do + lib = Library.new("Way Down Low") lib.register_new_book("Nausea", "Jean-Paul Sartre") expect(lib.books.count).to eq(1) @@ -68,8 +68,8 @@ expect(created_book.id).to_not be_nil end - xit "can add multiple books" do - lib = Library.new + it "can add multiple books" do + lib = Library.new("SMAWESOME") lib.register_new_book("One", "Bob") lib.register_new_book("Two", "Bob") lib.register_new_book("Three", "Bob") @@ -77,8 +77,8 @@ expect(lib.books.count).to eq(3) end - xit "allows a Borrower to check out a book by its id" do - lib = Library.new + it "allows a Borrower to check out a book by its id" do + lib = Library.new("HelloWorld") lib.register_new_book("Green Eggs and Ham", "Dr. Seuss") book_id = lib.books.first.id @@ -94,8 +94,8 @@ expect(book.status).to eq 'checked_out' end - xit "knows who borrowed a book" do - lib = Library.new + it "knows who borrowed a book" do + lib = Library.new("Bibleoteca") lib.register_new_book("The Brothers Karamazov", "Fyodor Dostoesvky") book_id = lib.books.first.id @@ -107,9 +107,9 @@ expect( lib.get_borrower(book_id) ).to eq 'Big Brother' end - xit "does not allow a book to be checked out twice in a row" do - lib = Library.new - lib.register_new_book = Book.new("Surely You're Joking Mr. Feynman", "Richard Feynman") + it "does not allow a book to be checked out twice in a row" do + lib = Library.new("ANUSTART") + lib.register_new_book("Surely You're Joking Mr. Feynman", "Richard Feynman") book_id = lib.books.first.id # Leslie Nielsen wants to double check on that @@ -128,8 +128,8 @@ expect(book_again).to be_nil end - xit "allows a Borrower to check a book back in" do - lib = Library.new + it "allows a Borrower to check a book back in" do + lib = Library.new("WAHAHAH") lib.register_new_book("Finnegans Wake", "James Joyce") book_id = lib.books.first.id From 0e7e97e6714c44fd32cff0aac5b4c511de8703cc Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Tue, 2 Dec 2014 15:45:38 -0600 Subject: [PATCH 07/16] Passes current tests --- library.rb | 26 ++++++++++++++++++++++---- spec/library_spec.rb | 18 +++++++++--------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/library.rb b/library.rb index 9727b562..37b00479 100644 --- a/library.rb +++ b/library.rb @@ -33,10 +33,8 @@ def check_in() class Borrower attr_reader :name - attr_accessor :checked def initialize(name) @name = name - @num_checked = 0 end end @@ -44,10 +42,13 @@ class Library attr_reader :books attr_reader :name attr_reader :borrowers + attr_accessor :checked_list def initialize(name) @name = name @books = [] @borrowers = {} + @checked_list = {} + end def register_new_book(title, author) @@ -57,11 +58,14 @@ def register_new_book(title, author) def check_out_book(book_id, borrower) checked = nil + if !(@checked_list.has_key?(borrower)) + @checked_list[borrower] = 0 + end @books.each do |book| - if book.id == book_id && book.status == "available" && borrower.num_checked.count <= 2 + if (book.id == book_id) && (book.status == "available") && (@checked_list[borrower] < 2) book.check_out() @borrowers[book_id] = borrower - borrower.num_checked +=1 + checked_list[borrower] += 1 checked = book end end @@ -86,8 +90,22 @@ def add_book(title, author) def available_books + available = [] + @books.each do |book| + if book.status == "available" + available.push(book) + end + end + return available end def borrowed_books + borrowed = [] + @books.each do |book| + if book.status == "checked_out" + borrowed.push(book) + end + end + return borrowed end end diff --git a/spec/library_spec.rb b/spec/library_spec.rb index 18d54001..04078f9a 100644 --- a/spec/library_spec.rb +++ b/spec/library_spec.rb @@ -144,9 +144,9 @@ expect(book.status).to eq 'available' end - xit "does not allow a Borrower to check out more than one Book at any given time" do + it "does not allow a Borrower to check out more than one Book at any given time" do # yeah it's a stingy library - lib = Library.new + lib = Library.new("STINGYPETE") lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") @@ -168,8 +168,8 @@ expect(book).to be_nil end - xit "returns available books" do - lib = Library.new + it "returns available books" do + lib = Library.new("BRICK HOUSE") lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") @@ -185,8 +185,8 @@ expect(lib.available_books.count).to eq(2) end - xit "after a book it returned, it can be checked out again" do - lib = Library.new + it "after a book it returned, it can be checked out again" do + lib = Library.new("Hello") lib.register_new_book("Harry Potter", "J. K. Rowling") book_id = lib.books.first.id @@ -204,8 +204,8 @@ expect( lib.get_borrower(book_id) ).to eq 'Michael Schumacher' end - xit "returns borrowed books" do - lib = Library.new + it "returns borrowed books" do + lib = Library.new("Last one!") lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") @@ -214,7 +214,7 @@ expect(lib.borrowed_books.count).to eq(0) kors = Borrower.new("Michael Kors") - book = lib.check_out_book(lib.borrowed_books.first.id, kors) + book = lib.check_out_book(lib.available_books.first.id, kors) # But now there should be one checked out book expect(lib.borrowed_books.count).to eq(1) From 9674e3f330e27fdd09a30582f833baa495bea8d7 Mon Sep 17 00:00:00 2001 From: Gilbert Date: Wed, 3 Dec 2014 23:28:06 -0600 Subject: [PATCH 08/16] Starting code for project --- Gemfile | 2 ++ Gemfile.lock | 40 +++++++++++++-------- lib/library_plus.rb | 34 ++++++++++++++++++ lib/library_plus/book_repo.rb | 1 + lib/library_plus/user_repo.rb | 27 +++++++++++++++ server.rb | 8 +++++ spec/.gitkeep | 0 spec/repos/user_repo_spec.rb | 65 +++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 13 +++++++ views/index.erb | 3 ++ 10 files changed, 178 insertions(+), 15 deletions(-) create mode 100644 lib/library_plus.rb create mode 100644 lib/library_plus/book_repo.rb create mode 100644 lib/library_plus/user_repo.rb create mode 100644 server.rb delete mode 100644 spec/.gitkeep create mode 100644 spec/repos/user_repo_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 views/index.erb diff --git a/Gemfile b/Gemfile index 1f478929..f4b5c425 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,5 @@ ruby '2.0.0' gem 'rspec', '~> 2.14.1' gem 'pry-byebug' +gem 'sinatra', '~> 1.4.5' +gem 'pg' diff --git a/Gemfile.lock b/Gemfile.lock index db516937..180a130e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,23 +1,26 @@ GEM remote: https://rubygems.org/ specs: + byebug (3.5.1) + columnize (~> 0.8) + debugger-linecache (~> 1.2) + slop (~> 3.6) coderay (1.1.0) - columnize (0.3.6) - debugger (1.6.5) - columnize (>= 0.3.1) - debugger-linecache (~> 1.2.0) - debugger-ruby_core_source (~> 1.3.1) + columnize (0.8.9) debugger-linecache (1.2.0) - debugger-ruby_core_source (1.3.1) diff-lcs (1.2.5) method_source (0.8.2) - pry (0.9.12.6) - coderay (~> 1.0) - method_source (~> 0.8) + pg (0.17.1) + pry (0.10.1) + coderay (~> 1.1.0) + method_source (~> 0.8.1) slop (~> 3.4) - pry-debugger (0.2.2) - debugger (~> 1.3) - pry (~> 0.9.10) + pry-byebug (2.0.0) + byebug (~> 3.4) + pry (~> 0.10) + rack (1.5.2) + rack-protection (1.5.3) + rack rspec (2.14.1) rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) @@ -26,11 +29,18 @@ GEM rspec-expectations (2.14.5) diff-lcs (>= 1.1.3, < 2.0) rspec-mocks (2.14.5) - slop (3.4.7) + sinatra (1.4.5) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (~> 1.3, >= 1.3.4) + slop (3.6.0) + tilt (1.4.1) PLATFORMS ruby DEPENDENCIES - pry-debugger (~> 0.2.2) - rspec (~> 2.14.0) + pg + pry-byebug + rspec (~> 2.14.1) + sinatra (~> 1.4.5) diff --git a/lib/library_plus.rb b/lib/library_plus.rb new file mode 100644 index 00000000..9148018a --- /dev/null +++ b/lib/library_plus.rb @@ -0,0 +1,34 @@ +require 'pg' + +module Library + def self.create_db_connection(dbname) + PG.connect(host: 'localhost', dbname: dbname) + end + + def self.clear_db(db) + db.exec <<-SQL + DELETE FROM users; + /* TODO: Clear rest of the tables (books, etc.) */ + SQL + end + + def self.create_tables(db) + db.exec <<-SQL + CREATE TABLE users( + id SERIAL PRIMARY KEY, + name VARCHAR + ); + /* TODO: Create rest of the tables (books, etc.) */ + SQL + end + + def self.drop_tables(db) + db.exec <<-SQL + DROP TABLE users; + /* TODO: Drop rest of the tables (books, etc.) */ + SQL + end +end + +require_relative 'library_plus/book_repo' +require_relative 'library_plus/user_repo' diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb new file mode 100644 index 00000000..46409041 --- /dev/null +++ b/lib/library_plus/book_repo.rb @@ -0,0 +1 @@ +# TODO diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb new file mode 100644 index 00000000..ef6ee87c --- /dev/null +++ b/lib/library_plus/user_repo.rb @@ -0,0 +1,27 @@ +module Library + class UserRepo + + 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 users").to_a + end + + def self.find(db, user_data) + # TODO: Insert SQL statement + end + + def self.save(db, user_data) + if user_data['id'] + # TODO: Update SQL statement + else + # TODO: Insert SQL statement + end + end + + def self.destroy(db, user_data) + # TODO: Delete SQL statement + end + + end +end diff --git a/server.rb b/server.rb new file mode 100644 index 00000000..91a87bcd --- /dev/null +++ b/server.rb @@ -0,0 +1,8 @@ +require 'sinatra' +require './lib/library_plus' + +# set :bind, '0.0.0.0' # This is needed for Vagrant + +get '/' do + erb :index +end diff --git a/spec/.gitkeep b/spec/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/spec/repos/user_repo_spec.rb b/spec/repos/user_repo_spec.rb new file mode 100644 index 00000000..94028dde --- /dev/null +++ b/spec/repos/user_repo_spec.rb @@ -0,0 +1,65 @@ +require 'spec_helper' + +describe Library::UserRepo 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::UserRepo.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::UserRepo.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 + + xit "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" + end + + xit "updates users" do + user1 = Library::UserRepo.save(db, :name => "Alice") + user2 = Library::UserRepo.save(db, :name => "Alicia") + expect(user2['id']).to eq(user1['id']) + expect(user2['name']).to eq "Alicia" + + # Check for persistence + user3 = Library::UserRepo.find(user1['id']) + expect(user3['name']).to eq "Alicia" + end + + xit "destroys users" do + user = Library::UserRepo.save(db, :name => "Alice") + expect(user_count(db)).to eq 1 + + Library::UserRepo.destroy(db, user['id']) + expect(user_count(db)).to eq 0 + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..962938ee --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,13 @@ +require 'library_plus' + +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 diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..aba3235d --- /dev/null +++ b/views/index.erb @@ -0,0 +1,3 @@ +

The Library Plus System

+ +

Welcome to your freedom!

From 31945facbfa3d57be1aea31c64fc3746b97847db Mon Sep 17 00:00:00 2001 From: Gilbert Date: Thu, 4 Dec 2014 16:11:12 -0600 Subject: [PATCH 09/16] Correct some variable names --- lib/library_plus/user_repo.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index ef6ee87c..f293fe6a 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -7,7 +7,7 @@ def self.all(db) db.exec("SELECT * FROM users").to_a end - def self.find(db, user_data) + def self.find(db, user_id) # TODO: Insert SQL statement end @@ -19,7 +19,7 @@ def self.save(db, user_data) end end - def self.destroy(db, user_data) + def self.destroy(db, user_id) # TODO: Delete SQL statement end From fd9cd8119e85585a7f41880f40bf9fd4014b1503 Mon Sep 17 00:00:00 2001 From: Gilbert Date: Fri, 5 Dec 2014 11:05:34 -0600 Subject: [PATCH 10/16] Fix spec typos --- spec/repos/user_repo_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/repos/user_repo_spec.rb b/spec/repos/user_repo_spec.rb index 94028dde..44d0278b 100644 --- a/spec/repos/user_repo_spec.rb +++ b/spec/repos/user_repo_spec.rb @@ -27,7 +27,7 @@ def user_count(db) it "creates users" do expect(user_count(db)).to eq 0 - user = Library::UserRepo.save(db, :name => "Alice") + user = Library::UserRepo.save(db, { 'name' => "Alice" }) expect(user['id']).to_not be_nil expect(user['name']).to eq "Alice" @@ -39,14 +39,14 @@ def user_count(db) end xit "finds users" do - user = Library::UserRepo.save(db, :name => "Alice") + user = Library::UserRepo.save(db, { 'name' => "Alice" }) retrieved_user = Library::UserRepo.find(db, user['id']) expect(retrieved_user['name']).to eq "Alice" end xit "updates users" do - user1 = Library::UserRepo.save(db, :name => "Alice") - user2 = Library::UserRepo.save(db, :name => "Alicia") + user1 = Library::UserRepo.save(db, { 'name' => "Alice" }) + user2 = Library::UserRepo.save(db, { 'id' => user1['id'], 'name' => "Alicia" }) expect(user2['id']).to eq(user1['id']) expect(user2['name']).to eq "Alicia" @@ -56,7 +56,7 @@ def user_count(db) end xit "destroys users" do - user = Library::UserRepo.save(db, :name => "Alice") + user = Library::UserRepo.save(db, { 'name' => "Alice" }) expect(user_count(db)).to eq 1 Library::UserRepo.destroy(db, user['id']) From 402ab5712c8e8441c5d730325560df6d619114e8 Mon Sep 17 00:00:00 2001 From: Gilbert Date: Fri, 5 Dec 2014 11:53:54 -0600 Subject: [PATCH 11/16] Pass in db connection --- 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 44d0278b..dc810e5d 100644 --- a/spec/repos/user_repo_spec.rb +++ b/spec/repos/user_repo_spec.rb @@ -51,7 +51,7 @@ def user_count(db) 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 d3432c83d421f5dd42c469e507a3436a3fb8eee8 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Fri, 5 Dec 2014 14:53:37 -0600 Subject: [PATCH 12/16] Implemnts search, create, and update user --- lib/library_plus/user_repo.rb | 8 ++++++++ server.rb | 13 +++++++++++++ spec/repos/user_repo_spec.rb | 6 +++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index f293fe6a..0211f586 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -8,15 +8,23 @@ def self.all(db) end def self.find(db, user_id) + result = db.exec("SELECT * FROM users WHERE id = $1;", [user_id]) + result.first # TODO: Insert SQL statement end def self.save(db, user_data) if user_data['id'] + db.exec("UPDATE users SET name = $1 WHERE id = $2;",[user_data['name'], user_data['id']]) # TODO: Update SQL statement else + db.exec("INSERT INTO users (name) VALUES ($1);", [user_data['name']]) + user_data['id'] # TODO: Insert SQL statement end + result = db.exec("SELECT * FROM users WHERE name = $1;", [user_data['name']]) + result.first + end def self.destroy(db, user_id) diff --git a/server.rb b/server.rb index 91a87bcd..7bd63067 100644 --- a/server.rb +++ b/server.rb @@ -6,3 +6,16 @@ get '/' do erb :index end + +get '/books' do + erb :"books/index" +end + +get '/books' do + #Create a new books + erb :"book/show" +end + +get '/books/:id' do + erb :"books/show" +end diff --git a/spec/repos/user_repo_spec.rb b/spec/repos/user_repo_spec.rb index 44d0278b..0ee49bb5 100644 --- a/spec/repos/user_repo_spec.rb +++ b/spec/repos/user_repo_spec.rb @@ -38,20 +38,20 @@ 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" end - xit "updates users" do + it "updates users" do user1 = Library::UserRepo.save(db, { 'name' => "Alice" }) 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 f648ef252ed7f6a4ced558e618c3aa451af62ec3 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Sat, 6 Dec 2014 15:35:23 -0600 Subject: [PATCH 13/16] Create/drop table for all tables --- Gemfile | 1 + Gemfile.lock | 41 +++++-- lib/library_plus.rb | 18 ++- lib/library_plus/user_repo.rb | 10 +- library.rb | 111 ----------------- server.rb | 27 +++- spec/library_spec.rb | 223 ---------------------------------- spec/repos/user_repo_spec.rb | 2 +- views/index.erb | 2 + 9 files changed, 85 insertions(+), 350 deletions(-) delete mode 100644 library.rb delete mode 100644 spec/library_spec.rb diff --git a/Gemfile b/Gemfile index f4b5c425..ed9f0190 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,4 @@ gem 'rspec', '~> 2.14.1' gem 'pry-byebug' gem 'sinatra', '~> 1.4.5' gem 'pg' +gem 'sinatra-reloader' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index ed06d011..ebaa436a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,35 +1,60 @@ GEM remote: https://rubygems.org/ specs: - byebug (2.7.0) - columnize (~> 0.3) + backports (3.6.4) + byebug (3.5.1) + columnize (~> 0.8) debugger-linecache (~> 1.2) + slop (~> 3.6) coderay (1.1.0) columnize (0.8.9) debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) + multi_json (1.10.1) pg (0.17.1) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - pry-byebug (1.3.2) - byebug (~> 2.7) - pry (~> 0.9.12) + pry-byebug (2.0.0) + byebug (~> 3.4) + pry (~> 0.10) + rack (1.5.2) + rack-protection (1.5.3) + rack + rack-test (0.6.2) + rack (>= 1.0) rspec (2.14.1) rspec-core (~> 2.14.0) rspec-expectations (~> 2.14.0) rspec-mocks (~> 2.14.0) - rspec-core (2.14.7) + rspec-core (2.14.8) rspec-expectations (2.14.5) diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.5) - slop (3.5.0) + rspec-mocks (2.14.6) + sinatra (1.4.5) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (~> 1.3, >= 1.3.4) + sinatra-contrib (1.4.2) + backports (>= 2.0) + multi_json + rack-protection + rack-test + sinatra (~> 1.4.0) + tilt (~> 1.3) + sinatra-reloader (1.0) + sinatra-contrib + slop (3.6.0) + tilt (1.4.1) PLATFORMS ruby DEPENDENCIES + pg pry-byebug rspec (~> 2.14.1) + sinatra (~> 1.4.5) + sinatra-reloader diff --git a/lib/library_plus.rb b/lib/library_plus.rb index 9148018a..c7537e4f 100644 --- a/lib/library_plus.rb +++ b/lib/library_plus.rb @@ -8,23 +8,39 @@ def self.create_db_connection(dbname) def self.clear_db(db) db.exec <<-SQL DELETE FROM users; + DELETE FROM books; + DELETE FROM checkouts; /* TODO: Clear rest of the tables (books, etc.) */ SQL end def self.create_tables(db) db.exec <<-SQL - CREATE TABLE users( + CREATE TABLE IF NOT EXISTS users( id SERIAL PRIMARY KEY, name VARCHAR ); + CREATE TABLE IF NOT EXISTS books( + id SERIAL PRIMARY KEY, + title VARCHAR, + author VARCHAR + ); + CREATE TABLE IF NOT EXISTS checkouts( + id SERIAL PRIMARY KEY, + user_id INTEGER, + book_id INTEGER, + status VARCHAR, + created_at TIMESTAMP without time zone); /* TODO: Create rest of the tables (books, etc.) */ SQL + end def self.drop_tables(db) db.exec <<-SQL DROP TABLE users; + DROP TABLE books; + DROP TABLE checkouts; /* TODO: Drop rest of the tables (books, etc.) */ SQL end diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index 0211f586..1c8616dd 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -15,21 +15,21 @@ def self.find(db, user_id) def self.save(db, user_data) if user_data['id'] - db.exec("UPDATE users SET name = $1 WHERE id = $2;",[user_data['name'], user_data['id']]) + result = db.exec("UPDATE users SET name = $1 WHERE id = $2 RETURNING *;",[user_data['name'], user_data['id']]) # TODO: Update SQL statement else - db.exec("INSERT INTO users (name) VALUES ($1);", [user_data['name']]) - user_data['id'] + result = db.exec("INSERT INTO users (name) VALUES ($1) RETURNING *;", [user_data['name']]) # TODO: Insert SQL statement end - result = db.exec("SELECT * FROM users WHERE name = $1;", [user_data['name']]) result.first end def self.destroy(db, user_id) + db.exec("DELETE FROM users WHERE id = $1;", [user_id]) + # TODO: Delete SQL statement end end -end +end \ No newline at end of file diff --git a/library.rb b/library.rb deleted file mode 100644 index 37b00479..00000000 --- a/library.rb +++ /dev/null @@ -1,111 +0,0 @@ - -class Book - attr_reader :author - attr_reader :title - attr_reader :id - attr_accessor :status - @@book_id_count = 1 - - def initialize(title, author) - @author = author - @title = title - @status = "available" - @id = @@book_id_count - @@book_id_count += 1 - end - - def check_out() - if @status == "available" - @status = "checked_out" - else - return false - end - end - - def check_in() - if @status == "checked_out" - @status = "available" - else - return false - end - end -end - -class Borrower - attr_reader :name - def initialize(name) - @name = name - end -end - -class Library - attr_reader :books - attr_reader :name - attr_reader :borrowers - attr_accessor :checked_list - def initialize(name) - @name = name - @books = [] - @borrowers = {} - @checked_list = {} - - end - - def register_new_book(title, author) - book = Book.new(title, author) - @books.push(book) - end - - def check_out_book(book_id, borrower) - checked = nil - if !(@checked_list.has_key?(borrower)) - @checked_list[borrower] = 0 - end - @books.each do |book| - if (book.id == book_id) && (book.status == "available") && (@checked_list[borrower] < 2) - book.check_out() - @borrowers[book_id] = borrower - checked_list[borrower] += 1 - checked = book - end - end - checked - end - - def get_borrower(book_id) - @borrowers[book_id].name - end - - def check_in_book(book) - book.check_in() - @borrowers.delete(book.id) - end - - def book - end - - def add_book(title, author) - end - - - - def available_books - available = [] - @books.each do |book| - if book.status == "available" - available.push(book) - end - end - return available - end - - def borrowed_books - borrowed = [] - @books.each do |book| - if book.status == "checked_out" - borrowed.push(book) - end - end - return borrowed - end -end diff --git a/server.rb b/server.rb index 7bd63067..34944a1d 100644 --- a/server.rb +++ b/server.rb @@ -1,16 +1,41 @@ require 'sinatra' +require 'sinatra/reloader' require './lib/library_plus' -# set :bind, '0.0.0.0' # This is needed for Vagrant +set :bind, '0.0.0.0' # This is needed for Vagrant get '/' do erb :index end +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') + if params['user_name'] + user = {'name' => params["user_name"]} + Library::UserRepo.save(db, user) + elsif params['user_id'] + id = params["user_id"] + Library::UserRepo.destroy(db, id) + end + redirect to('/users') +end + get '/books' do + db = Library.create_db_connection('library_dev') + @books = Library::BookRepo.all(db) erb :"books/index" end +post '/books' do + +end + get '/books' do #Create a new books erb :"book/show" diff --git a/spec/library_spec.rb b/spec/library_spec.rb deleted file mode 100644 index 04078f9a..00000000 --- a/spec/library_spec.rb +++ /dev/null @@ -1,223 +0,0 @@ -require "./library.rb" -require 'pry-byebug' - -describe Book do - it "has a title and author, and nil id" do - book = Book.new("The Stranger", "Albert Camus") - - # binding.pry - - expect(book.title).to eq "The Stranger" - expect(book.author).to eq "Albert Camus" - expect(book.id).to_not be_nil - end - - it "has a default status of available" do - book = Book.new("The Anger", "Spenser McChicken") - expect(book.status).to eq 'available' - end - - it "can be checked out" do - book = Book.new("The Ger", "Spenser McChicken") - did_it_work = book.check_out - expect(did_it_work).to be_true - expect(book.status).to eq 'checked_out' - end - - it "can't be checked out twice in a row" do - book = Book.new("The Dan", "Spenser McChicken") - did_it_work = book.check_out - expect(did_it_work).to be_true - - did_it_work_again = book.check_out - expect(did_it_work_again).to eq(false) - - expect(book.status).to eq 'checked_out' - end - - it "can be checked in" do - book = Book.new("The Danger", "Spenser McChicken") - book.check_out - book.check_in - expect(book.status).to eq 'available' - end -end - -describe Borrower do - it "has a name" do - borrower = Borrower.new("Mike") - expect(borrower.name).to eq "Mike" - end -end - -describe Library do - - it "starts with an empty array of books" do - lib = Library.new("Way up High") - expect(lib.books.count).to eq(0) - end - - it "add new books and assigns it an id" do - lib = Library.new("Way Down Low") - lib.register_new_book("Nausea", "Jean-Paul Sartre") - expect(lib.books.count).to eq(1) - - created_book = lib.books.first - expect(created_book.title).to eq "Nausea" - expect(created_book.author).to eq "Jean-Paul Sartre" - expect(created_book.id).to_not be_nil - end - - it "can add multiple books" do - lib = Library.new("SMAWESOME") - lib.register_new_book("One", "Bob") - lib.register_new_book("Two", "Bob") - lib.register_new_book("Three", "Bob") - - expect(lib.books.count).to eq(3) - end - - it "allows a Borrower to check out a book by its id" do - lib = Library.new("HelloWorld") - lib.register_new_book("Green Eggs and Ham", "Dr. Seuss") - book_id = lib.books.first.id - - # Sam wants to check out Green Eggs and Ham - sam = Borrower.new('Sam-I-am') - book = lib.check_out_book(book_id, sam) - - # The checkout should return the book - expect(book).to be_a(Book) - expect(book.title).to eq "Green Eggs and Ham" - - # The book should now be marked as checked out - expect(book.status).to eq 'checked_out' - end - - it "knows who borrowed a book" do - lib = Library.new("Bibleoteca") - lib.register_new_book("The Brothers Karamazov", "Fyodor Dostoesvky") - book_id = lib.books.first.id - - # Big Brother wants to check out The Brothers Karamazov - bro = Borrower.new('Big Brother') - book = lib.check_out_book(book_id, bro) - - # The Library should know that he checked out the book - expect( lib.get_borrower(book_id) ).to eq 'Big Brother' - end - - it "does not allow a book to be checked out twice in a row" do - lib = Library.new("ANUSTART") - lib.register_new_book("Surely You're Joking Mr. Feynman", "Richard Feynman") - book_id = lib.books.first.id - - # Leslie Nielsen wants to double check on that - nielsen = Borrower.new('Leslie Nielsen') - book = lib.check_out_book(book_id, nielsen) - - # The first time should be successful - expect(book).to be_a(Book) - - # However, you can't check out the same book twice! - book_again = lib.check_out_book(book_id, nielsen) - expect(book_again).to be_nil - - son = Borrower.new('Leslie Nielsen the 2nd') - book_again = lib.check_out_book(book_id, son) - expect(book_again).to be_nil - end - - it "allows a Borrower to check a book back in" do - lib = Library.new("WAHAHAH") - lib.register_new_book("Finnegans Wake", "James Joyce") - book_id = lib.books.first.id - - # Bob wants to check out Finnegans Wake - bob = Borrower.new('Bob Bobber') - book = lib.check_out_book(book_id, bob) - - # o wait he changed his mind - lib.check_in_book(book) - - # The book should now be marked as available! - expect(book.status).to eq 'available' - end - - it "does not allow a Borrower to check out more than one Book at any given time" do - # yeah it's a stingy library - lib = Library.new("STINGYPETE") - lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") - lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") - lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") - - jackson = Borrower.new("Michael Jackson") - book_1 = lib.books[0] - book_2 = lib.books[1] - book_3 = lib.books[2] - - # The first two books should check out fine - book = lib.check_out_book(book_1.id, jackson) - expect(book.title).to eq "Eloquent JavaScript" - - book = lib.check_out_book(book_2.id, jackson) - expect(book.title).to eq "Essential JavaScript Design Patterns" - - # However, the third should return nil - book = lib.check_out_book(book_3.id, jackson) - expect(book).to be_nil - end - - it "returns available books" do - lib = Library.new("BRICK HOUSE") - lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") - lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") - lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") - - # At first, all books are available - expect(lib.available_books.count).to eq(3) - expect(lib.available_books.first).to be_a(Book) - - jordan = Borrower.new("Michael Jordan") - book = lib.check_out_book(lib.available_books.first.id, jordan) - - # But now, there should only be two available books - expect(lib.available_books.count).to eq(2) - end - - it "after a book it returned, it can be checked out again" do - lib = Library.new("Hello") - lib.register_new_book("Harry Potter", "J. K. Rowling") - book_id = lib.books.first.id - - # First, we check out the book - vick = Borrower.new("Michael Vick") - book = lib.check_out_book(book_id, vick) - expect( lib.get_borrower(book_id) ).to eq 'Michael Vick' - - # When we check in a book, the Library does not care who checks it in - lib.check_in_book(book) - - # Another person should be able to check the book out - schumacher = Borrower.new("Michael Schumacher") - book = lib.check_out_book(book_id, schumacher) - expect( lib.get_borrower(book_id) ).to eq 'Michael Schumacher' - end - - it "returns borrowed books" do - lib = Library.new("Last one!") - lib.register_new_book("Eloquent JavaScript", "Marijn Haverbeke") - lib.register_new_book("Essential JavaScript Design Patterns", "Addy Osmani") - lib.register_new_book("JavaScript: The Good Parts", "Douglas Crockford") - - # At first, no books are checked out - expect(lib.borrowed_books.count).to eq(0) - - kors = Borrower.new("Michael Kors") - book = lib.check_out_book(lib.available_books.first.id, kors) - - # But now there should be one checked out book - expect(lib.borrowed_books.count).to eq(1) - expect(lib.borrowed_books.first).to be_a(Book) - end -end diff --git a/spec/repos/user_repo_spec.rb b/spec/repos/user_repo_spec.rb index 0ee49bb5..d907dc5c 100644 --- a/spec/repos/user_repo_spec.rb +++ b/spec/repos/user_repo_spec.rb @@ -55,7 +55,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 diff --git a/views/index.erb b/views/index.erb index aba3235d..1fd5f074 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,5 @@

The Library Plus System

Welcome to your freedom!

+ +Manage Users! \ No newline at end of file From 84d24a395d200282bb4bf8b303ae027211ab46b3 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Sat, 6 Dec 2014 21:15:10 -0600 Subject: [PATCH 14/16] Added book create, save, status, find, destroy, checkout, checkin. All functional on server except checkin. No graphics yet --- lib/library_plus.rb | 7 ++-- lib/library_plus/book_repo.rb | 71 ++++++++++++++++++++++++++++++++++- server.rb | 35 ++++++++++++++--- views/index.erb | 3 +- 4 files changed, 105 insertions(+), 11 deletions(-) diff --git a/lib/library_plus.rb b/lib/library_plus.rb index c7537e4f..0ce23e58 100644 --- a/lib/library_plus.rb +++ b/lib/library_plus.rb @@ -23,14 +23,15 @@ def self.create_tables(db) CREATE TABLE IF NOT EXISTS books( id SERIAL PRIMARY KEY, title VARCHAR, - author VARCHAR + author VARCHAR, + status VARCHAR DEFAULT ('available') ); CREATE TABLE IF NOT EXISTS checkouts( id SERIAL PRIMARY KEY, user_id INTEGER, book_id INTEGER, - status VARCHAR, - created_at TIMESTAMP without time zone); + status VARCHAR DEFAULT ('checked_out'), + created_at TIMESTAMP without time zone DEFAULT NOW()); /* TODO: Create rest of the tables (books, etc.) */ SQL diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index 46409041..8f8a50dd 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -1 +1,70 @@ -# TODO +module Library + class BookRepo + + def self.all(db) + db.exec("SELECT * FROM books;").to_a + end + + def self.save(db, book_data) + if book_data['id'] + sql = %q[UPDATE books + SET title = $1 AND SET author = $2 + WHERE id = $3 + RETURNING *; + ] + result = db.exec(sql, [book_data['title'], book_data['author'], book_data['id']]) + else + sql = %q[INSERT INTO books (title, author) + VALUES ($1, $2) + RETURNING *; + ] + result = db.exec(sql, [book_data['title'], book_data['author']]) + end + result.first + end + + def self.status(db, book_id) + sql = %q[UPDATE books + SET status = $1 + WHERE id = $2 + RETURNING *; + ] + book = self.find(db, book_id) + if book['status'] == 'available' + result = db.exec(sql, ['checked_out', book_id]) + else + result = db.exec(sql, ['available', book_id]) + end + result.first + end + + def self.find(db, book_id) + result = db.exec("SELECT * FROM books WHERE id = $1;", [book_id]) + result.first + end + + def self.destroy(db, book_id) + db.exec("DELETE FROM books WHERE id = $1;", [book_id]) + end + + def self.check_out_book(db, user_id, book_id) + sql = %q[INSERT INTO checkouts (user_id, book_id) + VALUES ($1, $2) + RETURNING *; + ] + result = db.exec(sql, [user_id, book_id]) + result.first + end + + def self.check_in_book(db, book_id) + sql = %q[UPDATE checkouts + SET status = 'available' + WHERE book_id = $1 + RETURNING *; + ] + result = db.exec(sql, [book_id]) + result.first + end + + end +end \ No newline at end of file diff --git a/server.rb b/server.rb index 34944a1d..58bfb2d1 100644 --- a/server.rb +++ b/server.rb @@ -26,6 +26,13 @@ redirect to('/users') end +get '/users/:id' do + id = params['id'] + db = Library.create_db_connection('library_dev') + @user = Library::UserRepo.find(db, id) + erb :"users/show" +end + get '/books' do db = Library.create_db_connection('library_dev') @books = Library::BookRepo.all(db) @@ -33,14 +40,30 @@ end post '/books' do - -end - -get '/books' do - #Create a new books - erb :"book/show" + db = Library.create_db_connection('library_dev') + puts params + if params['book_title'] && params['book_author'] + user = {'title' => params['book_title'], 'author' => params['book_author']} + Library::BookRepo.save(db, user) + elsif params['book_id'] + id = params["book_id"] + Library::BookRepo.destroy(db, id) + end + redirect to('/books') end get '/books/:id' do + id = params['id'] + db = Library.create_db_connection('library_dev') + @book = Library::BookRepo.find(db, id) + @users = Library::UserRepo.all(db) erb :"books/show" end + +post '/books/:id/checkout' do + puts params + db = Library.create_db_connection('library_dev') + Library::BookRepo.check_out_book(db, params['user_id'], params['id']) + Library::BookRepo.status(db, params['id']) + redirect to('/books/' + params['id']) +end \ No newline at end of file diff --git a/views/index.erb b/views/index.erb index 1fd5f074..9c1f103a 100644 --- a/views/index.erb +++ b/views/index.erb @@ -2,4 +2,5 @@

Welcome to your freedom!

-Manage Users! \ No newline at end of file +Manage Users! +Manage Books! \ No newline at end of file From 7858a5eaf994aa46f869e5a0c2b39de3c1132338 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Sun, 7 Dec 2014 11:22:18 -0600 Subject: [PATCH 15/16] Added check in to website --- lib/library_plus/book_repo.rb | 16 ++++++++++++++++ server.rb | 10 ++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index 8f8a50dd..0a84e82c 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -66,5 +66,21 @@ def self.check_in_book(db, book_id) result.first end + def self.check_out_history_book(db, book_id) + sql = %q[SELECT * from checkouts + WHERE book_id = $1 + ORDER By created_at; + ] + result = db.exec(sql, [book_id]) + end + + def self.check_out_history_user(db, user_id) + sql = %q[SELECT * from checkouts + WHERE user_id = $1 + ORDER BY created_at; + ] + result = db.exec(sql, [book_id]) + end + end end \ No newline at end of file diff --git a/server.rb b/server.rb index 58bfb2d1..05af0a62 100644 --- a/server.rb +++ b/server.rb @@ -61,9 +61,15 @@ end post '/books/:id/checkout' do - puts params db = Library.create_db_connection('library_dev') Library::BookRepo.check_out_book(db, params['user_id'], params['id']) Library::BookRepo.status(db, params['id']) redirect to('/books/' + params['id']) -end \ No newline at end of file +end + +post '/books/:id/checkin' do + db = Library.create_db_connection('library_dev') + Library::BookRepo.check_in_book(db, params['id']) + Library::BookRepo.status(db, params['id']) + redirect to('/books/' + params['id']) +end From 9496953ff18837040292ec05e0a9a74096e07735 Mon Sep 17 00:00:00 2001 From: Spenser Filler Date: Mon, 8 Dec 2014 13:56:03 -0600 Subject: [PATCH 16/16] Merge with Repo --- server.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server.rb b/server.rb index 05af0a62..71532d3f 100644 --- a/server.rb +++ b/server.rb @@ -57,6 +57,7 @@ db = Library.create_db_connection('library_dev') @book = Library::BookRepo.find(db, id) @users = Library::UserRepo.all(db) + @history = Library::BookRepo.check_out_history_book(db, params['id']) erb :"books/show" end @@ -73,3 +74,10 @@ Library::BookRepo.status(db, params['id']) redirect to('/books/' + params['id']) end + +# get '/books/:id/history' do +# puts params['id'] +# db = Library.create_db_connection('library_dev') +# @history = Library::BookRepo.check_out_history_book(db, params['id']) +# erb :"books/show" +# end \ No newline at end of file