Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/library_plus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,38 @@ 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

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
Expand Down
85 changes: 84 additions & 1 deletion lib/library_plus/book_repo.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions lib/library_plus/user_repo.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand Down
85 changes: 84 additions & 1 deletion server.rb
Original file line number Diff line number Diff line change
@@ -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

60 changes: 60 additions & 0 deletions spec/repos/book_repo_spec.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions spec/repos/user_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions views/books/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h1>All Books</h1>
<ul>
<% @books.each do |x| %>
<% book_id = "/books/#{x['id']} "%>
<li><a href=<%= book_id %>><%= x['title'] %></a></li>
<% end %>
</ul>
<form method="POST" action="/books">

<h3>Register New Book</h3>
<label>Title:</label>
<input type="text" name="title" />

<label>Author:</label>
<input type="text" name="author" />

<button>Register</button>
</form>
1 change: 1 addition & 0 deletions views/books/noshow.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This book is currently checked out. Please select another.</p>
Loading