Skip to content

Commit

Permalink
bookstore final code
Browse files Browse the repository at this point in the history
  • Loading branch information
User authored and User committed Jun 12, 2017
0 parents commit 32b2d6c
Show file tree
Hide file tree
Showing 48 changed files with 902 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bookshop_final/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require( 'sinatra' )
require( 'sinatra/contrib/all' )
require_relative('controllers/authors_controller')
require_relative('controllers/books_controller')

get '/' do
erb( :index )
end

61 changes: 61 additions & 0 deletions bookshop_final/controllers/authors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require('sinatra')
require('sinatra/contrib/all')
require('pry-byebug')
require_relative('../models/author.rb')

# INDEX - READ
get '/authors' do
@authors = Author.all()
erb ( :"authors/index" )
end

# NEW - CREATE
get '/authors/new' do
erb( :"authors/new" )
end

# CREATE
post '/authors' do
@authors = Author.new(params)
@authors.save()
redirect '/authors'
end

# DESTROY
post '/authors/:id/delete' do
@author = Author.find( params[:id] )
@author.delete()
erb( :"authors/destroy")
end

# EDIT
get '/authors/:id/edit' do
@author = Author.find( params[:id] )
erb( :"authors/edit")
end

## SHOW
get '/authors/:id' do
@author = Author.find( params[:id] )
erb( :"authors/show" )
end

# EDIT
get '/authors/:id/edit' do
@author = Author.find( params[:id] )
erb( :"books/author")
end

# UPDATE
post '/authors/:id' do
@author = Author.new( params )
@author.update()
erb( :"authors/update")
end

# DELETE
post '/authors/:id/delete' do
@author = Book.find( params[:id] )
@author.delete()
erb( :"authors/destroy")
end
54 changes: 54 additions & 0 deletions bookshop_final/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require('sinatra')
require('sinatra/contrib/all')
require('pry-byebug')
require_relative('../models/book.rb')
require_relative('../models/author.rb')

# READ
get '/books' do
@books = Book.all()
erb ( :"books/index" )
end

# CREATE
get '/books/new' do
@books = Book.all()
@authors = Author.all()
erb( :"books/new" )
end

# CREATE
post '/books' do
@books = Book.new(params)
@books.save()
redirect '/books'
end

# SHOW
get '/books/:id' do
@book = Book.find( params[:id] )
erb( :"books/show" )
end

# EDIT
get '/books/:id/edit' do
@book = Book.find( params[:id] )
erb( :"books/edit")
end

# UPDATE
post '/books/:id' do
@book = Book.new( params )
@book.update()
erb( :"books/update")
end

# DELETE
post '/books/:id/delete' do
@book = Book.find( params[:id] )
@book.delete()
erb( :"books/destroy")
end



19 changes: 19 additions & 0 deletions bookshop_final/db/bookshop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
DROP TABLE books;
DROP TABLE authors;

CREATE TABLE authors
(
id SERIAL8 PRIMARY KEY,
first_name VARCHAR(255),
last_name VARCHAR(255)
);

CREATE TABLE books
(
id SERIAL8 PRIMARY KEY,
title VARCHAR(255),
quantity INT4,
buy_price REAL,
sell_price REAL,
author_id INT4 REFERENCES authors(id)
);
178 changes: 178 additions & 0 deletions bookshop_final/db/seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
require_relative( '../models/author.rb' )
require_relative( '../models/book.rb' )
require('pry-byebug')

Book.delete_all()
Author.delete_all()

author1 = Author.new({
'first_name' => "Roald",
'last_name' => "Dahl"
})

author1.save()

author2 = Author.new({
'first_name' => "David",
'last_name' => "McKee"
})

author2.save()

author3 = Author.new({
'first_name' => "Ian",
'last_name' => "Whybrow"
})

author3.save()

author4 = Author.new({
'first_name' => "Julia",
'last_name' => "Donaldson"
})

author4.save()

book1 = Book.new({
'title' => "The Enormous Crocodile",
'quantity' => 30,
'buy_price' => 5.99,
'sell_price' => 6.99,
'author_id' => author1.id
})

book1.save()

book2 = Book.new({
'title' => "James and the Giant Peach",
'quantity' => 25,
'buy_price' => 7.99,
'sell_price' => 8.99,
'author_id' => author1.id
})

book2.save()

book3 = Book.new({
'title' => "Charlie and the Chocolate Factory",
'quantity' => 35,
'buy_price' => 9.99,
'sell_price' => 10.99,
'author_id' => author1.id
})

book3.save()

book4 = Book.new({
'title' => "The Fantastic Mr Fox",
'quantity' => 35,
'buy_price' => 11.99,
'sell_price' => 12.99,
'author_id' => author1.id
})

book4.save()

book5 = Book.new({
'title' => "Elmer the Patchwork Elephant",
'quantity' => 15,
'buy_price' => 13.99,
'sell_price' => 14.99,
'author_id' => author2.id
})

book5.save()

book6 = Book.new({
'title' => "Elmer and Super El",
'quantity' => 15,
'buy_price' => 15.99,
'sell_price' => 16.99,
'author_id' => author2.id
})

book6.save()

book7 = Book.new({
'title' => "Elmer and Wilbur",
'quantity' => 15,
'buy_price' => 17.99,
'sell_price' => 18.99,
'author_id' => author2.id
})

book7.save()

book8 = Book.new({
'title' => "Harry and the Dinosaurs at the Museum",
'quantity' => 35,
'buy_price' => 19.99,
'sell_price' => 20.99,
'author_id' => author3.id
})

book8.save()

book9 = Book.new({
'title' => "Harry and the Dinosaurs Go Wild",
'quantity' => 35,
'buy_price' =>21.99,
'sell_price' => 22.99,
'author_id' => author3.id
})

book9.save()

book10 = Book.new({
'title' => "Harry and the Dinosaurs United",
'quantity' => 3,
'buy_price' => 23.99,
'sell_price' => 24.99,
'author_id' => author3.id
})

book10.save()

book11 = Book.new({
'title' => "The Gruffalo",
'quantity' => 6,
'buy_price' => 23.99,
'sell_price' => 24.99,
'author_id' => author4.id
})

book11.save()

book12 = Book.new({
'title' => "What the Ladybird Said",
'quantity' => 6,
'buy_price' => 23.99,
'sell_price' => 24.99,
'author_id' => author4.id
})

book12.save()

book13 = Book.new({
'title' => "Postman Bear",
'quantity' => 7,
'buy_price' => 13.99,
'sell_price' => 14.99,
'author_id' => author4.id
})

book13.save()

book14 = Book.new({
'title' => "Hide and Seek Pig",
'quantity' => 7,
'buy_price' => 12.99,
'sell_price' => 13.99,
'author_id' => author4.id
})

book14.save()


binding.pry
nil
15 changes: 15 additions & 0 deletions bookshop_final/db/sql_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require( 'pg' )

class SqlRunner

def self.run( sql )
begin
db = PG.connect({ dbname: 'bookshop', host: 'localhost' })
result = db.exec( sql )
ensure
db.close
end
return result
end

end
Loading

0 comments on commit 32b2d6c

Please sign in to comment.