Skip to content
83 changes: 83 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# # file: app.rb
require_relative './lib/item_repository'
require_relative './lib/order_repository'
require_relative 'lib/database_connection'

class Application

def initialize(database_name, io, item_repository, order_repository)
DatabaseConnection.connect(database_name)
@io = io
@item_repository = item_repository
@order_repository = order_repository
end

def run(item_repository = ItemRepository.new, order_repository = OrderRepository.new)
@item_repository = item_repository
@order_repository = order_repository

@io.puts "Welcome to the shop management program!"
@io.puts "What do you want to do?
\n1 = list all shop items\n2 = create a new item
\n3 = list all orders\n4 = create a new order"
user_choice = @io.gets.chomp
while true do
case user_choice
when "1"
@io.puts "Here is the list of all items:"
@item_repository.all.each do |item|
@io.puts "##{item.id} #{item.name} - Unit price: #{item.price} - Quantity: #{item.quantity}"
end
break
when "2"
item = Item.new
@io.puts "Please enter new item name."
item.name = @io.gets.chomp
@io.puts "Please enter new item price."
item.price = @io.gets.chomp
@io.puts "Please enter new item quantity."
item.quantity = @io.gets.chomp
item_repository = ItemRepository.new
item_repository.create(item)
break
when "3"
@io.puts "Here is the list of all orders:"
@order_repository.all.each do |order|
@io.puts "##{order.id} #{order.customer_name} - Date: #{order.date}"
end
break
when "4"
order = Order.new
@io.puts "Please enter customer name."
order.customer_name = @io.gets.chomp
@io.puts "Please enter date."
order.date = @io.gets.chomp
order_repository = OrderRepository.new
order_repository.create(order)
break


else
@io.puts "Please choose 1, 2, 3 or 4."
user_choice = @io.gets.chomp
end
end

end

end

# Don't worry too much about this if statement. It is basically saying "only
# run the following code if this is the main file being run, instead of having
# been required or loaded by another file.
# If you want to learn more about __FILE__ and $0, see here: https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Variables_and_Constants#Pre-defined_Variables
if __FILE__ == $0
app = Application.new(
'shop_manager',
Kernel,
ItemRepository.new,
OrderRepository.new
)
app.run
end

28 changes: 28 additions & 0 deletions lib/database_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# file: lib/database_connection.rb

require 'pg'

# This class is a thin "wrapper" around the
# PG library. We'll use it in our project to interact
# with the database using SQL.

class DatabaseConnection
# This method connects to PostgreSQL using the
# PG gem. We connect to 127.0.0.1, and select
# the database name given in argument.
def self.connect(database_name)
@connection = PG.connect({ host: '127.0.0.1', dbname: database_name })
end

# This method executes an SQL query
# on the database, providing some optional parameters
# (you will learn a bit later about when to provide these parameters).
def self.exec_params(query, params)
if @connection.nil?
raise 'DatabaseConnection.exec_params: Cannot run a SQL query as the connection to'\
'the database was never opened. Did you make sure to call first the method '\
'`DatabaseConnection.connect` in your app.rb file (or in your tests spec_helper.rb)?'
end
@connection.exec_params(query, params)
end
end
4 changes: 4 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Item

attr_accessor :id, :name, :price, :quantity
end
63 changes: 63 additions & 0 deletions lib/item_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require_relative './item'

class ItemRepository
def all
# shows all items
sql = 'SELECT id, name, price, quantity FROM items;'
result = DatabaseConnection.exec_params(sql, [])

items = []

result.each do |record|
items << record_to_item(record)
end

return items
end

def find(id)
sql = 'SELECT id, name, price, quantity FROM items WHERE id = $1;'
sql_params = [id]

result = DatabaseConnection.exec_params(sql, sql_params)
record = result[0]

return record_to_item(record)

end

def create(item)
sql = 'INSERT INTO items
(name, price, quantity)
VALUES($1, $2, $3);'
sql_params = [item.name, item.price, item.quantity]

DatabaseConnection.exec_params(sql, sql_params)

return nil
end

def delete(id)
sql = 'DELETE FROM items
WHERE id = $1;'
sql_params = [id]

DatabaseConnection.exec_params(sql, sql_params)

return nil
end

private

def record_to_item(record)
item = Item.new
item.id = record['id']
item.name = record['name']
item.price = record['price']
item.quantity = record['quantity']

return item
end


end
5 changes: 5 additions & 0 deletions lib/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Order

attr_accessor :id, :customer_name, :date, :items
@items = []
end
75 changes: 75 additions & 0 deletions lib/order_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require_relative './order'

class OrderRepository
def all
sql = 'SELECT id, customer_name, date FROM orders'
result = DatabaseConnection.exec_params(sql, [])

orders = []

result.each do |record|

# order = Order.new
# order.id = record['id']
# order.customer_name = record['customer_name']
# order.date = record['date']

orders << record_to_order(record)
end

return orders
end

def find(id)
sql = 'SELECT id, customer_name, date FROM orders WHERE id = $1;'
sql_params = [id]

result = DatabaseConnection.exec_params(sql, sql_params)
record = result[0]

return record_to_order(record)

end

def create(order)
sql = 'INSERT INTO orders
(customer_name, date)
VALUES($1, $2);'
sql_params = [order.customer_name, order.date]

DatabaseConnection.exec_params(sql, sql_params)

return nil
end

def delete(id)
sql = 'DELETE FROM orders
WHERE id = $1;'
sql_params = [id]

DatabaseConnection.exec_params(sql, sql_params)

return nil
end

def find_with_items(id)
sql = 'SELECT items.id, items.name, items.price
FROM items
JOIN items_orders ON items_orders.item_id = items.id
JOIN orders ON items_orders.order_id = orders.id
WHERE orders.id = $1;'
sql_params = [id]
result = DatabaseConnection.exec_params(sql, sql_params)
end

private
def record_to_order(record)
order = Order.new
order.id = record['id']
order.customer_name = record['customer_name']
order.date = record['date']
return order
end


end
Loading