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

Michael Bashour - Library plus #436

Open
wants to merge 5 commits into
base: master
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ ruby '2.0.0'

gem 'rspec', '~> 2.14.1'
gem 'pry-byebug'
gem 'sinatra', '~> 1.4.5'
gem 'pg'
40 changes: 25 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
GEM
remote: https://rubygems.org/
specs:
byebug (3.5.1)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
slop (~> 3.6)
coderay (1.1.0)
columnize (0.3.6)
debugger (1.6.5)
columnize (>= 0.3.1)
debugger-linecache (~> 1.2.0)
debugger-ruby_core_source (~> 1.3.1)
columnize (0.8.9)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.3.1)
diff-lcs (1.2.5)
method_source (0.8.2)
pry (0.9.12.6)
coderay (~> 1.0)
method_source (~> 0.8)
pg (0.17.1)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-debugger (0.2.2)
debugger (~> 1.3)
pry (~> 0.9.10)
pry-byebug (2.0.0)
byebug (~> 3.4)
pry (~> 0.10)
rack (1.5.2)
rack-protection (1.5.3)
rack
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
Expand All @@ -26,11 +29,18 @@ GEM
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.5)
slop (3.4.7)
sinatra (1.4.5)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
slop (3.6.0)
tilt (1.4.1)

PLATFORMS
ruby

DEPENDENCIES
pry-debugger (~> 0.2.2)
rspec (~> 2.14.0)
pg
pry-byebug
rspec (~> 2.14.1)
sinatra (~> 1.4.5)
34 changes: 34 additions & 0 deletions lib/library_plus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'pg'

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

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM users;
/* TODO: Clear rest of the tables (books, etc.) */
SQL
end

def self.create_tables(db)
db.exec <<-SQL
CREATE TABLE users(
id SERIAL PRIMARY KEY,
name VARCHAR
);
/* TODO: Create rest of the tables (books, etc.) */
SQL
end

def self.drop_tables(db)
db.exec <<-SQL
DROP TABLE users;
/* TODO: Drop rest of the tables (books, etc.) */
SQL
end
end

require_relative 'library_plus/book_repo'
require_relative 'library_plus/user_repo'
1 change: 1 addition & 0 deletions lib/library_plus/book_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO
38 changes: 38 additions & 0 deletions lib/library_plus/user_repo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Library ::UserRepo <-- how to reference this class
# This is a user class under the Library module
# Library::UserRepo.all(db)
# Library::BookRepo.all(db)

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.
db.exec("SELECT * FROM users").to_a
end

def self.find(db, user_id)
# TODO: Insert SQL statement
end
# Use Create if !exists and Update if does
# TODO: Update SQL statement
# Library::UserRepo.save(db, { :name => "Alice" })

def self.save(db, user_data)
if user_data['id']

#
else
db.exec("INSERT INTO users (name) VALUES ($1)", [user_data[:name]])
results = db.exec("SELECT * FROM users")
results
end
end

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

end
end
8 changes: 8 additions & 0 deletions server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'sinatra'
require './lib/library_plus'

# set :bind, '0.0.0.0' # This is needed for Vagrant

get '/' do
erb :index
end
Empty file removed spec/.gitkeep
Empty file.
65 changes: 65 additions & 0 deletions spec/repos/user_repo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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') }

before(:each) do
Library.clear_db(db)
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

xit "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, { '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'])
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
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'library_plus'

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
end
3 changes: 3 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>The Library Plus System</h1>

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