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: 17 additions & 4 deletions lib/library_plus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,37 @@ def self.create_db_connection(dbname)
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(
id SERIAL PRIMARY KEY,
user_id VARCHAR,
book_id VARCHAR,
status VARCHAR DEFAULT 'checked_out',
created_at 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;
DROP TABLE checkouts;
SQL
end
end
Expand Down
47 changes: 46 additions & 1 deletion lib/library_plus/book_repo.rb
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
# TODO
module Library
class BookRepo

def self.index(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.show(db, book_id)
# TODO: Insert SQL statement
res = db.exec_params("SELECT * FROM books WHERE id = $1",[book_id])
return res.first
end

def self.show_checkouts(db, book_id)
# TODO: Insert SQL statement
res = db.exec_params("SELECT * FROM checkouts WHERE book_id = $1",[book_id])
return res.first
end

def self.checkout(db, book_id, user_id)
exist = db.exec_params("SELECT * FROM checkouts WHERE book_id = $1",[book_id])
if (exist.first == nil)
db.exec_params("INSERT INTO checkouts (book_id, user_id) VALUES ($1, $2)", [book_id, user_id])
return 'added'
else
db.exec_params("UPDATE checkouts SET status = 'checked_out' WHERE book_id = $1", [book_id])
return 'added'
end
end

def self.create(db, book_data)
# TODO: Insert SQL statement
db.exec_params("INSERT INTO books (title, author) VALUES ($1, $2)",[ book_data['title'], book_data['author'] ])
res = db.exec_params("SELECT * FROM books WHERE title = $1",[book_data['title']])
return res.first
end

def self.destroy(db, book_id)
# TODO: Delete SQL statement
db.exec_params("DELETE FROM books WHERE id= $1 ",[book_id])
end

end
end
7 changes: 7 additions & 0 deletions lib/library_plus/user_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ def self.all(db)

def self.find(db, user_id)
# TODO: Insert SQL statement
res = db.exec_params("SELECT * FROM users WHERE id = $1",[user_id])
return res.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
res = db.exec_params("SELECT * FROM users WHERE name = $1",[user_data['name']])
return res.first
end

def self.destroy(db, user_id)
# TODO: Delete SQL statement
db.exec_params("DELETE FROM users WHERE id= $1 ",[user_id])
end

end
Expand Down
45 changes: 45 additions & 0 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,48 @@
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[:title] })
@users = Library::UserRepo.all(db)
erb :"users/index"
end

get '/books' do
db = Library.create_db_connection('library_dev')
@books = Library::BookRepo.index(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.index(db)
erb :"books/index"
end

get '/books/:show' do
db = Library.create_db_connection('library_dev')
@users = Library::UserRepo.all(db)
@book = Library::BookRepo.show(db, params[:show])
@status= Library::BookRepo.show_checkouts(db, params[:show])
if (@status == nil)
@status = {'status' => 'available'}
end

erb :"books/show"
end

post '/books/:id/checkout' do
db = Library.create_db_connection('library_dev')
Library::BookRepo.checkout(db,params[:id], params[:user_id])
@books = Library::BookRepo.index(db)
erb :"books/index"
end
17 changes: 17 additions & 0 deletions views/books/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>All Books</h1>

<%
@books.each do |x|
%>

<ul><a href="/books/<%= x['id'] %>"><%= x['title'] %> by <%= x['author'] %></a></ul>
<% end %>

<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>
20 changes: 20 additions & 0 deletions views/books/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>Book Page</h1>

<p>Title: <%= @book['title'] %></p>

<p>Author: <%= @book['author'] %></p>

<%if (@status['status'] == "checked_out")%>
<p>Currently Checked Out</p>
<%else%>
<p>Available</p>
<%end%>

<form action ="/books/<%= @book['id'] %>/checkout" method="post">
<select name="user_id"=>
<% @users.each do |u| %>
<option value="<%= u['id'] %>"><%= u['name'] %></option>
<% end %>
</select>
<input type="submit" value="Check Out">
</form>
4 changes: 4 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<h1>The Library Plus System</h1>

<p>Welcome to your freedom!</p>

<a href="/users">Manage Users</a>

<a href="/books">Manage Books</a>
15 changes: 15 additions & 0 deletions views/users/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>All Users</h1>

<%
@users.each do |x|
%>

<ul><%= x['name'] %></ul>
<% end %>

<form method="POST" action="/users">
<h3>Register New User</h3>
<label>Name:</label>
<input type="text" name="title"/>
<button>Register</button>
</form>