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
21 changes: 16 additions & 5 deletions lib/library_plus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 45 additions & 1 deletion lib/library_plus/book_repo.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 6 additions & 3 deletions lib/library_plus/user_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 42 additions & 1 deletion server.rb
Original file line number Diff line number Diff line change
@@ -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
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
19 changes: 19 additions & 0 deletions views/books/book.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<center>

<h2>Book Info</h2>
<h3><%= @book['title'] %></h3>
<h4>By: <%= @book['author'] %></h4>
<h4>Status: <%= @book['status'] || 'Returned' %></h4>

<br>

<form method='POST' action='/books/:id/checkout'>
<select name="user_id" style="width: 350px;"
<% @users.each do |u| %>
<option value="<%= u['id'] %>"><%= u['name'] %></option>
<% end %>
</select>
<input type='submit' value='Checkout Book!'>
</form>

</center>
20 changes: 20 additions & 0 deletions views/books/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<center>
<h1>All Books</h1>

<% @books.each do |b| %>
<ul>
<a href="/books/<%= b['id'] %>"
<h6><%= b['title']%></h6> By: <%= b['author']%>
</ul>
<% end %>
<br>

<form method="POST" action="/books">
<h3>Register New Book</h3>
<label>Title:</label>
<input type="text" name="title" style="width: 350px" /><br>
<label>Author:</label>
<input type="text" name="author" style="width: 350px" />

<button>Register</button>
</center>
9 changes: 7 additions & 2 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<h1>The Library Plus System</h1>
<center>
<h1>The Library Fantasmo System</h1>

<p>Welcome to your freedom!</p>
<p>Read!! One of us! One of us!</p>
<ul>
<a href='/users'>Manage Users</a><br>
<a href='/books'>Manage Books</a>
</ul>
9 changes: 9 additions & 0 deletions views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>Library</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/foundation/5.1.1/css/foundation.css">
</head>
<body>
<%= yield %>
</body>
</html>
15 changes: 15 additions & 0 deletions views/users/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<center>
<h1>All Users</h1>

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

<form method="POST" action="/users">
<h3>Register New User</h3>
<label>Name:</label>
<input type="text" name="user_name" style="width: 350px"/>

<button>Register</button>
</center>