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
4 changes: 2 additions & 2 deletions lib/library_plus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def self.clear_db(db)

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.) */
# TODO: Create rest of the tables (books, etc.)
SQL
end

Expand Down
53 changes: 53 additions & 0 deletions lib/library_plus/book_repo.rb
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
# TODO
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 = $1", [book_id]).to_a[0]
end

def self.get_status(db, book_id)
db.exec("SELECT status from checkouts where bookid = $1", [book_id]).to_a[0]
end

def self.getborrower(db, book_id)
db.exec("SELECT name from users u join checkouts c on c.userid = u.id join books b on c.bookid = b.id where c.bookid = $1", [book_id]).to_a[0]
end

#Libary::UserRepo.save(db, {'id' => "1"})
def self.save(db, book_data)
if book_data['id']
# TODO: Update SQL statement
db.exec("UPDATE books SET title = $1, author = $2 WHERE id = $3 returning *", [book_data['title'], book_data['author'], book_data['id']]).to_a[0]
else
# TODO: Insert SQL statement
newbook = db.exec("INSERT INTO books (title, author) VALUES ($1, $2) returning *", [book_data['title'], book_data['author']]).to_a[0]
Library::BookRepo.addtocheckouts(db, newbook['id'])
end
end

def self.addtocheckouts(db, book_id)
db.exec("INSERT INTO checkouts (bookid, status, time) values ($1, 'available', $2)",[book_id, DateTime.now])
end

def self.checkout(db, user_id, book_id)
@findbook = Library::BookRepo.get_status(db, book_id)
if @findbook["status"] == 'available'
db.exec("UPDATE checkouts SET status = 'checked out', userid = $1, time = $2 where bookid = $3",[user_id, DateTime.now, book_id])
end
end

def self.return(db, book_id)
db.exec("UPDATE checkouts SET status = 'available', userid = null, time = $1 where bookid = $2", [DateTime.now, book_id])
end

def self.destroy(db, user_id)
# TODO: Delete SQL statement
end

end
end

8 changes: 8 additions & 0 deletions lib/library_plus/user_repo.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Library::UserRepo.all(db)
#Library::BookRepo.all(db)

module Library
class UserRepo

Expand All @@ -9,13 +12,18 @@ def self.all(db)

def self.find(db, user_id)
# TODO: Insert SQL statement
db.exec("SELECT * FROM users WHERE id = $1", [user_id]).to_a[0]
end

#Libary::UserRepo.save(db, {'id' => "1"})
def self.save(db, user_data)
if user_data['id']
# TODO: Update SQL statement
db.exec("UPDATE users SET name = $1 WHERE id = $2 returning *", [user_data['name'], user_data['id']]).to_a[0]
else
# TODO: Insert SQL statement
db.exec("INSERT INTO users (name) VALUES ($1) returning *", [user_data['name']]).to_a[0]

end
end

Expand Down
48 changes: 48 additions & 0 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,51 @@
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,params)
redirect to('/users')
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')
book = Library::BookRepo.save(db, params)
redirect to("/books")
end

get '/books/:id' do
db = Library.create_db_connection('library_dev')
@book = Library::BookRepo.find(db,params[:id])
@bookstat = Library::BookRepo.get_status(db, params[:id])
@borrower = nil
if @bookstat["status"] == "checked out"
@borrower = Library::BookRepo.getborrower(db, params[:id])
end
@users = Library::UserRepo.all(db)
erb :"books/show"
end

post '/books/:id/checkout' do
db = Library.create_db_connection('library_dev')
Library::BookRepo.checkout(db,params["userid"], params["id"])
redirect to("/books/"+params[:id])
end

post '/books/:id/return' do
db = Library.create_db_connection('library_dev')
Library::BookRepo.return(db, params[:id])
redirect to("/books/"+params[:id])
end
3 changes: 2 additions & 1 deletion spec/repos/user_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ 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
user1 = Library::UserRepo.save(db, { 'name' => "Alice" })
user2 = Library::UserRepo.save(db, { 'id' => user1['id'], 'name' => "Alicia" })
Expand Down
30 changes: 30 additions & 0 deletions views/books/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html>
<head>
<title></title>
<style media="screen" type="text/css">

body {
padding: 20px;
margin-left: 20px;
}
form {
width: 30%;
margin-top: 50px;
}
</style>
</head>
<body><h1>All Books</h1>
<% @books.each{|book| %>
<a href=<%="/books/"+book["id"]%>><%= book["title"] %></a> by <%= book["author"] %> <br>
<% }%>

<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>
</body>
</html>
33 changes: 33 additions & 0 deletions views/books/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<html>
<head>
<title></title>
<style media="screen" type="text/css">

body {
padding: 20px;
margin-left: 20px;
}
form {
width: 30%;
margin-top: 50px;
}
</style>
</head>
<body>
<h1><%= @book["title"] %> by <%= @book["author"] %></h1>
<p> Book status:</p>
<% if @bookstat["status"] == "available" %>
<p>This book is available.</p>
<p>Select a user to check out this book to: </p>
<form method="post" action= <%="/books/" + @book["id"] + "/checkout"%>><select name ="userid">
<% @users.each{ |user| %>
<option value= <%= user["id"]%>><%=user["name"]%></option>
<% } %>
</select>
<input type ="submit"></form>
<% elsif @bookstat["status"] == "checked out" %>
<p>This book has been checked out by <%= @borrower["name"] %>.</p> <form method="post" action=<%="/books/" + @book["id"] + "/return"%>><button>Return</button></form>
<% end %>
<a href="/">Back to home.</a>
</body>
</html>
21 changes: 20 additions & 1 deletion views/index.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
<h1>The Library Plus System</h1>
<html>
<head>
<style media="screen" type="text/css">

body {
padding: 20px;
margin-left: 20px;
}
form {
width: 30%;
}
</style>
</head>
<h1>The Library+ System</h1>
<p>Welcome to your freedom!</p>
<a href="/users">View all users</a> | <a href="/books">View all books</a><br><br>
<p>Find a book:</p><br>
<form method="post" action="/books"><input name="title" type= "text">
<input type="submit">
</form>
</body>
</html>
29 changes: 29 additions & 0 deletions views/users/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>
<head>
<title></title>
<style media="screen" type="text/css">

body {
padding: 20px;
margin-left: 20px;
}
form {
width: 30%;
margin-top: 50px;
}
</style>
</head>
<body><h1>All Users</h1>
<% @users.each{|user| %>
<%= user["name"] %> <br>
<% }%>

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

<button>Register</button>
</form>
</body>
</html>