diff --git a/lib/library_plus.rb b/lib/library_plus.rb index 9148018a..d4210b23 100644 --- a/lib/library_plus.rb +++ b/lib/library_plus.rb @@ -9,16 +9,29 @@ def self.clear_db(db) db.exec <<-SQL DELETE FROM users; /* TODO: Clear rest of the tables (books, etc.) */ + DELETE FROM books; + DELETE FROM checkouts; 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 ); /* TODO: Create rest of the tables (books, etc.) */ + CREATE TABLE if not exists books( + id SERIAL PRIMARY KEY, + title VARCHAR, + author VARCHAR + ); + CREATE TABLE if not exists checkouts( + user_id INTEGER, + book_id INTEGER, + status VARCHAR default available, + created_at TIMESTAMP without time zone + ); SQL end @@ -26,6 +39,8 @@ def self.drop_tables(db) db.exec <<-SQL DROP TABLE users; /* TODO: Drop rest of the tables (books, etc.) */ + DROP TABLE books; + DROP TABLE checkouts; SQL end end diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index 46409041..e151006c 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -1 +1,84 @@ -# TODO +module Library + class BookRepo + # TODO + def self.all(db) + + db.exec("SELECT * FROM books").to_a + end + + def self.find(db, book_id) + # TODO: Insert SQL statement + + result = db.exec("SELECT * FROM books WHERE id = $1", [book_id]) + result.entries[0] + + + end + + def self.save(db, book_data) + if book_data['id'] + # TODO: Update SQL statement + title = book_data['title'] + author = book_data['author'] + id = book_data['id'] + db.exec("UPDATE books SET title = $1, author = $2 WHERE id = $3", [title, author, id]) + new_data = Library::BookRepo.find(db, id) + new_data + + else + # TODO: Insert SQL statement + title = book_data['title'] + author = book_data['author'] + returned = db.exec("INSERT into books (title, author) VALUES ($1, $2) returning id", [title, author]) + # new_data = Library::UserRepo.find(db, id) + book_data['id'] = returned.entries[0]['id'] + # require 'pry-byebug'; binding.pry + book_data + end + end + + def self.checkout_status(db, book_id) + status = db.exec("SELECT status FROM checkouts where book_id = $1", [book_id]).to_a + if status.entries[0] == nil + status = "available" + else + status[-1]['status'] + end + end + + def self.checkout(db, book_id, user_name) + response = db.exec("SELECT status FROM checkouts where book_id = $1", [book_id]).to_a + user_id = db.exec("SELECT id FROM users where name = $1", [user_name]).to_a[0]['id'] + if response.entries[0] == nil + user_id = db.exec("SELECT id FROM users where name = $1", [user_name]).to_a[0]['id'] + status = db.exec("INSERT INTO checkouts (user_id, book_id, status) VALUES ($1, $2, $3) returning status", [user_id, book_id, "checked_out"]).to_a[0]['status'] + # response = "This book has not been checked out." + status + elsif response.entries[0]['status'] == "available" || response.entries[0]['status'] == "returned" + # former_user_id = ("SELECT user_id from checkouts where book_id = $1", [book_id]).to_a[0]['user_id'] + # if former_user_id + status = db.exec("INSERT INTO checkouts (user_id, book_id, status) VALUES ($1, $2, $3) returning status", [user_id, book_id, "checked_out"]).to_a[0]['status'] #SET status = $1 WHERE book_id = $2 returning status", ["checked_out", book_id]).to_a[0]['status'] + status + else + status = "This book is currently checked out, please select another." + end + end + + def self.checkin(db, book_id) + db.exec("UPDATE checkouts SET status = $1 WHERE book_id = $2", ["returned", book_id]) + + end + + def self.checkout_history(db, book_id) + response = db.exec("select distinct u.name as name, b.title as title from checkouts c join users u on u.id = c.user_id join books b on b.id = c.book_id where c.book_id = $1", [book_id]).to_a + response + end + + def self.user_history(db, user_name_id) + end + +# library_dev=# select u.name as users, b.title as books from checkouts c join users u on u.id = c.user_id join books b on b.id = c.book_id; + + + end +end diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index f293fe6a..2bf8e93e 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -1,6 +1,7 @@ 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. @@ -9,13 +10,29 @@ def self.all(db) def self.find(db, user_id) # TODO: Insert SQL statement + + result = db.exec("SELECT * FROM users WHERE id = $1", [user_id]) + result.entries[0] + end def self.save(db, user_data) if user_data['id'] # TODO: Update SQL statement + name = user_data['name'] + id = user_data['id'] + db.exec("UPDATE users SET name = $1 WHERE id = $2", [name, id]) + new_data = Library::UserRepo.find(db, id) + new_data + else # TODO: Insert SQL statement + name = user_data['name'] + returned = db.exec("INSERT into users (name) VALUES ($1) returning id", [name]) + # new_data = Library::UserRepo.find(db, id) + user_data['id'] = returned.entries[0]['id'] + # require 'pry-byebug'; binding.pry + user_data end end diff --git a/server.rb b/server.rb index 91a87bcd..5ed10b8e 100644 --- a/server.rb +++ b/server.rb @@ -1,8 +1,91 @@ require 'sinatra' 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 + db = Library.create_db_connection('library_dev') + Library.create_tables(db) erb :index end + +get '/users' do + db = Library.create_db_connection('library_dev') + Library.create_tables(db) + @users = Library::UserRepo.all(db) + puts params + erb :"users/index" +end + +post '/users' do + puts params + db = Library.create_db_connection('library_dev') + Library::UserRepo.save(db, params) + # @users = Library::UserRepo.all(db) + redirect to ('/users') +end + +get '/books' do + db = Library.create_db_connection('library_dev') + Library.create_tables(db) + @books = Library::BookRepo.all(db) + erb :'books/index' +end + +post '/books' do + db = Library.create_db_connection('library_dev') + Library::BookRepo.save(db, params) + @books = Library::BookRepo.all(db) + redirect to ('/books') +end + +get '/books/:id' do + db = Library.create_db_connection('library_dev') + Library.create_tables(db) + book_id = params['id'].to_i + user_name = "whatever" + @results = Library::BookRepo.find(db, book_id) + @users = Library::UserRepo.all(db) + @status = Library::BookRepo.checkout_status(db, book_id) + puts @status + @history = Library::BookRepo.checkout_history(db, book_id) + # @checkout = Library::BookRepo.checkout(db, book_id, user_name) + erb :"books/show" +end + +post '/books/:id/checkout' do + puts params + user_name = params[:user_name] + book_id = params[:id] + db = Library.create_db_connection('library_dev') + Library.create_tables(db) + status = Library::BookRepo.checkout_status(db,book_id) + Library::BookRepo.checkout(db, book_id, user_name) + # book_id = params['id'].to_i + # user_name = "whatever" + # @results = Library::BookRepo.find(db, book_id) + # @users = Library::UserRepo.all(db) + # @checkout = Library::BookRepo.checkout(db, book_id, user_name) + # db = Library.create_db_connection('library_dev') + # Library.create_tables(db) + # puts params + # id = params['id'].to_i + # @results = Library::BookRepo.find(db, id) + # @users = Library::UserRepo.all(db) + # @checkout = Library::BookRepo.checkout(db, id) + status = Library::BookRepo.checkout_status(db,book_id) + redirect to ("books/#{book_id}") +end + +post '/books/:id/checkin' do + book_id = params[:id] + db = Library.create_db_connection('library_dev') + Library.create_tables(db) + Library::BookRepo.checkin(db, book_id) + redirect to ("books/#{book_id}") +end + +get '/books/:id/unavailable' do + erb :"books/unavailable" +end + diff --git a/spec/repos/book_repo_spec.rb b/spec/repos/book_repo_spec.rb new file mode 100644 index 00000000..1b9985b8 --- /dev/null +++ b/spec/repos/book_repo_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +describe Library::BookRepo do + + def book_count(db) + db.exec("SELECT COUNT(*) FROM books")[0]['count'].to_i + end + + let(:db) { Library.create_db_connection('library_test') } + + before(:each) do + Library.clear_db(db) + end + + it "gets all books" do + db.exec("INSERT INTO books (title, author) VALUES ($1, $2)", ["Oryx and Krake", "Margaret Atwood"]) + db.exec("INSERT INTO books (title, author) VALUES ($1, $2)", ["Station Eleven", "Emily Mandel"]) + + books = Library::BookRepo.all(db) + expect(books).to be_a Array + expect(books.count).to eq 2 + + authors = books.map {|u| u['author'] } + expect(authors).to include "Margaret Atwood", "Emily Mandel" + end + + it "creates books" do + expect(book_count(db)).to eq 0 + + book = Library::BookRepo.save(db, { 'title' => 'Good Omens', 'author' => 'Neil Gaiman' }) + expect(book['id']).to_not be_nil + expect(book['title']).to eq "Good Omens" + expect(book['author']).to eq "Neil Gaiman" + + # Check for persistence + expect(book_count(db)).to eq 1 + + book = db.exec("SELECT * FROM books")[0] + expect(book['title']).to eq "Good Omens" + expect(book['author']).to eq "Neil Gaiman" + end + + it "finds users" do + book = Library::BookRepo.save(db, { 'title' => 'Good Omens', 'author' => 'Neil Gaiman' }) + retrieved_book = Library::BookRepo.find(db, book['id']) + expect(retrieved_book['title']).to eq "Good Omens" + expect(retrieved_book['author']).to eq "Neil Gaiman" + end + + it "updates users" do + book1 = Library::BookRepo.save(db, { 'title' => 'Good Omens', 'author' => 'Neil Gaiman' }) + book2 = Library::BookRepo.save(db, { 'id' => book1['id'], 'author' => 'Neil Gaiman and Terry Pratchett' }) + expect(book2['id']).to eq(book1['id']) + expect(book2['author']).to eq "Neil Gaiman and Terry Pratchett" + + # Check for persistence + book3 = Library::BookRepo.find(db, book1['id']) + expect(book3['author']).to eq "Neil Gaiman and Terry Pratchett" + end +end \ No newline at end of file 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 diff --git a/views/books/index.erb b/views/books/index.erb new file mode 100644 index 00000000..69fbac61 --- /dev/null +++ b/views/books/index.erb @@ -0,0 +1,18 @@ +
This book is currently checked out. Please select another.
\ No newline at end of file diff --git a/views/books/show.erb b/views/books/show.erb new file mode 100644 index 00000000..9b2fbb0b --- /dev/null +++ b/views/books/show.erb @@ -0,0 +1,56 @@ +Checkout Status: <%= @status %>
+This book is currently checked out. Please select another.
\ No newline at end of file diff --git a/views/index.erb b/views/index.erb index aba3235d..9bba72da 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,7 @@Welcome to your freedom!
+