diff --git a/lib/library_plus.rb b/lib/library_plus.rb index 9148018a..3a7fffcf 100644 --- a/lib/library_plus.rb +++ b/lib/library_plus.rb @@ -2,30 +2,41 @@ module Library def self.create_db_connection(dbname) - PG.connect(host: 'localhost', dbname: dbname) + PG.connect(dbname: dbname) end def self.clear_db(db) db.exec <<-SQL DELETE FROM users; - /* TODO: Clear rest of the tables (books, etc.) */ + DELETE FROM books; 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 checkout( + id SERIAL PRIMARY KEY, + user_id integer REFERENCES users ( id), + book_id integer REFERENCES books ( id), + status VARCHAR DEFAULT 'returned', + created_at TIMESTAMP DEFAULT current_timestamp + ); 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 diff --git a/lib/library_plus/book_repo.rb b/lib/library_plus/book_repo.rb index 46409041..2d742411 100644 --- a/lib/library_plus/book_repo.rb +++ b/lib/library_plus/book_repo.rb @@ -1 +1,45 @@ -# TODO +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_id) + result = db.exec_params("SELECT books.*, checkout.status FROM books LEFT JOIN checkout ON books.id = checkout.book_id WHERE books.id = $1", [book_id]) + return result.first + end + + def self.create(db, book_data) + book_id = db.exec_params("INSERT INTO books (title, author) VALUES ($1, $2) returning id", [book_data['title'], book_data['author']]) + puts book_id + # db.exec_params("INSERT INTO checkout (book_id) VALUES ($1)", [book_id]) + result = db.exec("SELECT * FROM books WHERE title = $1", [book_data['title']]) + return result.first + end + + def self.destroy(db, book_id) + # TODO: Delete SQL statement + end + + def self.checkout(db, book_id, user_id) + result = db.exec_params("SELECT status FROM checkout WHERE book_id = $1", [book_id.to_i]) + if (result == nil) + db.exec_params("INSERT INTO checkout (book_id) VALUES ($1)", [book_id]) + db.exec_params("UPDATE checkout SET status = 'checked_out', user_id = $1 WHERE book_id = $2", [user_id, book_id.to_i]) + result = db.exec_params("SELECT books.*, checkout.status FROM books LEFT JOIN checkout ON books.id = checkout.book_id WHERE books.id = $1", [book_id.to_i]) + return result.first + elsif result == 'checked_out' + return false + else + db.exec_params("UPDATE checkout SET status = 'checked_out' WHERE book_id = $1", [book_id.to_i]) + db.exec_params("UPDATE checkout SET user_id = $1 WHERE book_id = $2", [user_id, book_id.to_i]) + result = db.exec_params("SELECT books.*, checkout.status FROM books LEFT JOIN checkout ON books.id = checkout.book_id WHERE books.id = $1", [book_id.to_i]) + return result.first + end + end + + end +end diff --git a/lib/library_plus/user_repo.rb b/lib/library_plus/user_repo.rb index f293fe6a..7a8a7a6b 100644 --- a/lib/library_plus/user_repo.rb +++ b/lib/library_plus/user_repo.rb @@ -8,15 +8,18 @@ def self.all(db) end def self.find(db, user_id) - # TODO: Insert SQL statement + result = db.exec_params("SELECT * FROM users WHERE id = $1", [user_id]) + result.first end def self.save(db, user_data) if user_data['id'] - # TODO: Update SQL statement + db.exec_params("UPDATE users SET name = $2 WHERE id = $1", [user_data['id'], user_data['name']]) else - # TODO: Insert SQL statement + db.exec_params("INSERT INTO users (name) VALUES ($1)", [user_data['name']]) 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..2c9ef567 100644 --- a/server.rb +++ b/server.rb @@ -1,8 +1,49 @@ 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 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') + Library::UserRepo.save(db, {'name' => params[:user_name]}) + @users = Library::UserRepo.all(db) + erb :"users/index" +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') + Library::BookRepo.create(db, {'title' => params[:title], 'author' => params[:author]}) + @books = Library::BookRepo.all(db) + erb :"books/index" +end + +get '/books/:id' do + db = Library.create_db_connection('library_dev') + @book = Library::BookRepo.find(db, params[:id]) + @users = Library::UserRepo.all(db) + erb :"books/book" +end + +post '/books/:id/checkout' do + db = Library.create_db_connection('library_dev') + @book = Library::BookRepo.checkout(db, params[:book_id], params[:user_id]) + puts @book + @users = Library::UserRepo.all(db) + erb :"books/book" +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/book.erb b/views/books/book.erb new file mode 100644 index 00000000..da131867 --- /dev/null +++ b/views/books/book.erb @@ -0,0 +1,19 @@ +
+ +

Book Info

+

<%= @book['title'] %>

+

By: <%= @book['author'] %>

+

Status: <%= @book['status'] || 'Returned' %>

+ +
+ +
+ + +
+ +
\ No newline at end of file diff --git a/views/books/index.erb b/views/books/index.erb new file mode 100644 index 00000000..0a651b5f --- /dev/null +++ b/views/books/index.erb @@ -0,0 +1,20 @@ +
+

All Books

+ +<% @books.each do |b| %> + + <% end %> +
+ +
+

Register New Book

+ +
+ + + + +
\ No newline at end of file diff --git a/views/index.erb b/views/index.erb index aba3235d..d2b9b5ad 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,8 @@ -

The Library Plus System

+
+

The Library Fantasmo System

-

Welcome to your freedom!

+

Read!! One of us! One of us!

+
\ No newline at end of file diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 00000000..ac17999f --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,9 @@ + + + Library + + + + <%= yield %> + + \ No newline at end of file diff --git a/views/users/index.erb b/views/users/index.erb new file mode 100644 index 00000000..a6e9db3a --- /dev/null +++ b/views/users/index.erb @@ -0,0 +1,15 @@ +
+

All Users

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

Register New User

+ + + + +
\ No newline at end of file