Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

James Knepper library-plus #434

Open
wants to merge 3 commits into
base: library-plus
Choose a base branch
from
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ gem 'rspec', '~> 2.14.1'
gem 'pry-byebug'
gem 'sinatra', '~> 1.4.5'
gem 'pg'
gem 'sinatra-contrib'
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
backports (3.6.4)
byebug (3.5.1)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
Expand All @@ -10,6 +11,7 @@ GEM
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
method_source (0.8.2)
multi_json (1.10.1)
pg (0.17.1)
pry (0.10.1)
coderay (~> 1.1.0)
Expand All @@ -21,6 +23,8 @@ GEM
rack (1.5.2)
rack-protection (1.5.3)
rack
rack-test (0.6.2)
rack (>= 1.0)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
Expand All @@ -33,6 +37,13 @@ GEM
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
sinatra-contrib (1.4.2)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (~> 1.3)
slop (3.6.0)
tilt (1.4.1)

Expand All @@ -44,3 +55,4 @@ DEPENDENCIES
pry-byebug
rspec (~> 2.14.1)
sinatra (~> 1.4.5)
sinatra-contrib
15 changes: 13 additions & 2 deletions lib/library_plus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Library
def self.create_db_connection(dbname)
PG.connect(host: 'localhost', dbname: dbname)
PG.connect(host: 'localhost', dbname: dbname )
end

def self.clear_db(db)
Expand All @@ -14,18 +14,29 @@ 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.) */
CREATE TABLE IF NOT EXISTS books(
title VARCHAR,
author VARCHAR,
id SERIAL PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS checkout(
book_id INTEGER,
user_id INTEGER,
status VARCHAR
);
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
25 changes: 25 additions & 0 deletions lib/library_plus/book_repo.rb
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# TODO
module Library
class BookRepo

def self.all(db)
db.exec("select * from books").to_a
end

def self.save(db, title, author)
db.exec("INSERT into books (title, author) values ($1, $2)", [title, author])
end

def self.check_out(db, book_id, user_id)
db.exec("INSERT into checkout (book_id, user_id, status) values ($1, $2, $3)", [book_id, user_id, 'checked out'])
end

def self.check_in(book_id)
db.exec("DELETE FROM checkout WHERE id = #{book_id}")
end

def self.all_check_out(db)
db.exec("SELECT * FROM checkout")
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
Expand Up @@ -9,18 +9,26 @@ def self.all(db)

def self.find(db, user_id)
# TODO: Insert SQL statement
db.exec("SELECT * from users WHERE id = #{user_id}")[0]
end

#Library::UserRepo.save(db, { 'name' => "alice"})
#Library::UserRepo.save(db, {'id' => 1, 'name => "Alice"})

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']])[0]
else
# TODO: Insert SQL statement
db.exec("INSERT into users (name) values ($1) returning *", [user_data['name']])[0]
# result = db.exec("INSERT into users (name) values ($1) returning *", [user_data['name']])
end
end

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

end
Expand Down
78 changes: 77 additions & 1 deletion server.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,84 @@
require 'sinatra'
require './lib/library_plus'
require 'bundler/setup'
require 'sinatra/reloader'
require './lib/library_plus/user_repo'
require './lib/library_plus/book_repo'

# 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
@new_user = {'name' => params[:user_name]}
db = Library.create_db_connection('library_dev')
Library::UserRepo.save(db, @new_user)
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')
@title = params[:title]
@author = params[:author]
Library::BookRepo.save(db, @title, @author)
redirect to ('/books')
end

get '/books/:id' do
# @id = params[:id]
db = Library.create_db_connection('library_dev')
@books = Library::BookRepo.all(db)
@books.each do |book|
if book['id'] == params[:id]
@showbook = book
end
end
z = false
@checkedbook = Library::BookRepo.all_check_out(db)
@checkedbook.each do |book|
if @showbook['id'] == book['book_id']
@status = 'checked out'
z = true
end
end
if z == false
@status = 'available'
end
@users = Library::UserRepo.all(db)
erb :"books/show"
end

post '/books/:id/checkout' do
y = params[:id]
@x = params[:userid]
# z = false
# @error = ""
db = Library.create_db_connection('library_dev')
# @checkedbooks = Library::BookRepo.all_check_out(db)
# @checkedbooks.each do |book|
# if params[:id] == book['id']
# @error = "That book is already checked out."
# else
# z = true
# end
# end
# if z == false
Library::BookRepo.check_out(db, y, @x)
# end
redirect to ('/books')
end

82 changes: 80 additions & 2 deletions spec/repos/user_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def user_count(db)
expect(names).to include "Alice", "Bob"
end

describe "self.save" do
it "creates users" do
expect(user_count(db)).to eq 0

Expand All @@ -37,14 +38,18 @@ def user_count(db)
user = db.exec("SELECT * FROM users")[0]
expect(user['name']).to eq "Alice"
end
end

xit "finds users" do
describe "self.find" 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
end

xit "updates users" do
describe "self.save" 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'])
Expand All @@ -54,7 +59,9 @@ def user_count(db)
user3 = Library::UserRepo.find(db, user1['id'])
expect(user3['name']).to eq "Alicia"
end
end

describe "self.destroy" do
xit "destroys users" do
user = Library::UserRepo.save(db, { 'name' => "Alice" })
expect(user_count(db)).to eq 1
Expand All @@ -63,3 +70,74 @@ def user_count(db)
expect(user_count(db)).to eq 0
end
end
end



# require 'spec_helper'

# describe Library::UserRepo do

# def user_count(db)
# db.exec("SELECT COUNT(*) FROM users")[0]["count"].to_i
# end

# let(:db) { Library.create_db_connection('library_test') } #instance variable inside each it block

# before(:each) do
# Library.clear_db(db) #clears each database before each it block.
# end


# it "gets all users" do
# db.exec("INSERT INTO users (name) VALUES ($1)", ["Alice"])
# db.exec("INSERT INTO users (name) VALUES ($1)", ["Bob"])

# users = Library::UserRepo.all(db)
# expect(users).to be_a Array
# expect(users.count).to eq 2

# names = users.map {|u| u['name'] }
# expect(names).to include "Alice", "Bob"
# end


# it "creates users" do
# expect(user_count(db)).to eq 0

# user = Library::UserRepo.save(db, :name => "Alice")
# expect(user['id']).to_not be_nil
# expect(user['name']).to eq "Alice"

# # Check for persistence
# expect(user_count(db)).to eq 1

# user = db.exec("SELECT * FROM users")[0]
# expect(user['name']).to eq "Alice"
# end

# xit "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, :name => "Alicia")
# expect(user2['id']).to eq(user1['id'])
# expect(user2['name']).to eq "Alicia"

# # Check for persistence
# user3 = Library::UserRepo.find(db, user1['id'])
# expect(user3['name']).to eq "Alicia"
# end

# xit "destroys users" do
# user = Library::UserRepo.save(db, :name => "Alice")
# expect(user_count(db)).to eq 1

# Library::UserRepo.destroy(db, user['id'])
# expect(user_count(db)).to eq 0
# end
# end
31 changes: 31 additions & 0 deletions views/books/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<style>
div {
border: 2px solid red;
border-radius: 10px;
width: 300px;
}
</style>

<center>
<h1>Books</h1>
<% @books.each do |book| %>
<h2><a href='/books/<%= book['id'] %>'><%= book['title'] %></a></h2>

<% end %>

<div>
<form method="post" action="/books">
<h3>Regiser New Book</h3>
<label>Title:</label>
<input type="text" name="title"/>
<br>
<label>Author:</label>
<input type='text' name='author'/>

<button>Register</button>
</form>
</div>
<br>
<a href='/'>HOME</a>

</center>
Loading