Skip to content
31 changes: 25 additions & 6 deletions lib/library_plus.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
require 'pg'
require 'pry-byebug'

module Library

def self.create_db(dbname)
success = system("createdb #{dbname}")
end

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.) */
DROP TABLE IF EXISTS checkouts;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS 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,
status VARCHAR
);
CREATE TABLE IF NOT EXISTS checkouts(
book_id INTEGER references books(id),
user_id INTEGER references users(id),
status VARCHAR,
checkout_date VARCHAR
);
SQL
end

def self.drop_tables(db)
db.exec <<-SQL
DROP TABLE users;
/* TODO: Drop rest of the tables (books, etc.) */
DROP TABLE IF EXISTS checkouts; # Drop Dependent Tables First
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS books;
SQL
end
end
Expand Down
91 changes: 90 additions & 1 deletion lib/library_plus/book_repo.rb
Original file line number Diff line number Diff line change
@@ -1 +1,90 @@
# TODO
require 'pry-byebug'

module Library
class BookRepo
def self.all(db)
db.exec("SELECT * FROM books").to_a
end

def self.find(db, book_id)
db.exec("SELECT * FROM books WHERE id = #{book_id}").entries.last
end

def self.checkout(db, book_id, user_id)
book = db.exec("SELECT * FROM books WHERE id = #{book_id}").entries.last
if book['status'] == 'available'
# Update Status to 'checked-out'
db.exec("UPDATE books SET status = 'checked_out' WHERE id = #{book_id}")

# Add book to checkout history table
db.exec("INSERT INTO checkouts (book_id, user_id, status, checkout_date) VALUES (#{book_id}, #{user_id}, 'checked_out', '#{Time.now}')")
end
find(db, book_id)
end

def self.checkin(db, book_id)
book = db.exec("SELECT * FROM books WHERE id = #{book_id}").entries.last
if book['status'] == 'checked_out'

# Update books table to 'available'
db.exec("UPDATE books SET status = 'available' WHERE id = #{book_id}")

# Update checkout table to 'returned'
db.exec("UPDATE checkouts SET status = 'returned' WHERE book_id = #{book_id} AND status = 'checked_out'")
end
find(db, book_id)
end

def self.save(db, book_data)
if book_data['id']
# Update Title
if book_data['title']
db.exec("UPDATE books SET title = '#{book_data['title']}' WHERE id = #{book_data['id']}")
end
if book_data['author']
db.exec("UPDATE books SET author = '#{book_data['author']}' WHERE id = #{book_data['id']}")
end
else
# Enter book_data['title'], book_data['author'] and Status / Unique ID gets assigned automatically
db.exec("INSERT INTO books (title, author, status) VALUES ('#{book_data['title']}', '#{book_data['author']}', 'available')")
end
# puts get_users(db, user_data)
get_books(db, book_data).last
end

def self.lose(db, book_id)
# Update books table to 'available'
db.exec("UPDATE checkouts SET status = 'lost' WHERE book_id = #{book_id} AND status = 'checked_out'")
db.exec("UPDATE books SET status = 'lost' WHERE id = #{book_id}")
end

def self.get_books(db, book_data)
if book_data['id']
db.exec("SELECT * FROM books WHERE id = #{book_data['id']}").entries
elsif book_data['title'] && book_data['author']
db.exec("SELECT * FROM books WHERE title = '#{book_data['title']}' AND author = '#{book_data['author']}'").entries
elsif book_data['title']
db.exec("SELECT * FROM books WHERE title = '#{book_data['title']}'").entries
elsif book_data['author']
db.exec("SELECT * FROM books WHERE title = '#{book_data['author']}'").entries
end
end

def self.get_status(db, book_id)
find(db, book_id)['status']
end

def self.get_history(db, id)
if id['book_id']
db.exec("SELECT * FROM checkouts WHERE book_id = #{id['book_id']}").entries
elsif id['user_id']
db.exec("SELECT * FROM checkouts WHERE user_id = #{id['user_id']}").entries
end
end

def self.get_checkedOutBooks(db, user_id)
get_history(db, user_id).select{ |entry| entry['status'] == 'checked_out'}
end

end
end
21 changes: 17 additions & 4 deletions lib/library_plus/user_repo.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'pry-byebug'

module Library
class UserRepo

Expand All @@ -8,19 +10,30 @@ def self.all(db)
end

def self.find(db, user_id)
# TODO: Insert SQL statement
db.exec("SELECT * FROM users WHERE id = #{user_id}").entries.last
end

def self.save(db, user_data)
if user_data['id']
# TODO: Update SQL statement
# Update Name
db.exec("UPDATE users SET name = '#{user_data['name']}' WHERE id = #{user_data['id']}")
else
# TODO: Insert SQL statement
# Enter Name and Unique ID gets assigned automatically
db.exec("INSERT INTO users (name) VALUES ('#{user_data['name']}')")
end
get_users(db, user_data).last
end

def self.destroy(db, user_id)
# TODO: Delete SQL statement
db.exec("DELETE FROM users WHERE id = #{user_id}")
end

def self.get_users(db, user_data)
if user_data['id']
db.exec("SELECT * FROM users WHERE id = #{user_data['id']}").entries
else
db.exec("SELECT * FROM users WHERE name = '#{user_data['name']}'").entries
end
end

end
Expand Down
87 changes: 86 additions & 1 deletion server.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,93 @@
require 'sinatra'
require './lib/library_plus'
require 'pry-byebug'

# set :bind, '0.0.0.0' # This is needed for Vagrant
set :bind, '0.0.0.0' # This is needed for Vagrant

Library.create_db('library_dev')
db = Library.create_db_connection('library_dev')
Library.create_tables(db)

get '/' do
erb :index
end

# User Index
get '/users' do
db = Library.create_db_connection('library_dev')
@users = Library::UserRepo.all(db)
erb :"users/index"
end

post '/users' do
# puts params
username = params[:user_name]
db = Library.create_db_connection('library_dev')
Library::UserRepo.save(db, { 'name' => username })
@users = Library::UserRepo.all(db)
erb :"users/index"
end

get '/users/*' do
user_id = params[:splat][0]
db = Library.create_db_connection('library_dev')
@checked_out_books = Library::BookRepo.get_checkedOutBooks(db, { 'user_id' => user_id })
erb :"users/summary"
end

# Book Index
get '/books' do
db = Library.create_db_connection('library_dev')
@books = Library::BookRepo.all(db)
erb :"books/index"
end

post '/books' do
# puts params
title = params[:title]
author = params[:author]
db = Library.create_db_connection('library_dev')
Library::BookRepo.save(db, { 'title' => title, 'author' => author })
@books = Library::BookRepo.all(db)
erb :"books/index"
end

get '/books/:book_id' do
# We need to choose a User that will checkout the book
book_id = params[:book_id]
db = Library.create_db_connection('library_dev')
@book_history = Library::BookRepo.get_history(db, book_id)
erb :"books/summary"
end

get '/books/:book_id/checkout' do
# We need to choose a User that will checkout the book
@book_id = params[:book_id]
db = Library.create_db_connection('library_dev')
@users = Library::UserRepo.all(db)
erb :"books/checkout/index"
end

post '/books/:book_id/checkout/:user_id' do
# We need to choose a User that will checkout the book
book_id = params[:book_id]
user_id = params[:user_id]
db = Library.create_db_connection('library_dev')
Library::BookRepo.checkout(db, book_id, user_id)
checkout_log = Library::BookRepo.get_checkedOutBooks(db, { 'user_id' => user_id })
books = Library::BookRepo.all(db)
@checked_out_books = []
checkout_log.each do |log|
book = Library::BookRepo.find(db, log['book_id'])
@checked_out_books.push(book)
end
binding.pry
erb :"users/summary"
end

post '/books/*/return' do
book_id = params[:splat][0]
db = Library.create_db_connection('library_dev')
Library::BookRepo.checkin(db, book_id)
erb :"books/index"
end
Loading