Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
203c846
add new table design, add new database schema
Shakhrai8 May 27, 2023
ecd04c1
add database connection, 2 seed files for the rspec and main app
Shakhrai8 May 27, 2023
8330145
combined 2 seeds
Shakhrai8 May 27, 2023
3f7eb56
add the main design, updated the test sql seed
Shakhrai8 May 27, 2023
b35c88b
initial methods setup and spec setup
Shakhrai8 May 27, 2023
87e17d2
add .all, .find, .create methods(items repo) and tests for them, pass…
Shakhrai8 May 27, 2023
839fb8c
add .delete, .update methods for item repository, add tests for .dele…
Shakhrai8 May 27, 2023
0c1351a
add .all method and test to the order repo, passes test
Shakhrai8 May 27, 2023
82bc425
add .find method and test to the order repo, passes test
Shakhrai8 May 27, 2023
73be466
add .create method and test to the order repo, passes test
Shakhrai8 May 27, 2023
32540c0
add .delete method and test to order repo, passes test
Shakhrai8 May 27, 2023
453234b
add .update method and test to the order repo, passes test
Shakhrai8 May 27, 2023
3b8409f
add .all method which shows the orders and the linked items, add test…
Shakhrai8 May 27, 2023
69cfa7e
add .find method which finds order by id and linked items, add test, …
Shakhrai8 May 27, 2023
5611ba7
add app interface, add new methods, add tests, pass tests
Shakhrai8 May 27, 2023
a477871
add new tests, pass tests
Shakhrai8 May 28, 2023
d379d18
updated the order_items table with a new column
Shakhrai8 May 28, 2023
8064589
updated the seed
Shakhrai8 May 28, 2023
0ae2c13
reworked the case 4 block
Shakhrai8 May 28, 2023
3dba33d
add new methods
Shakhrai8 May 28, 2023
5e3d330
add new initialize parameter
Shakhrai8 May 28, 2023
b522d3b
add new tests, pass tests
Shakhrai8 May 28, 2023
78d839d
interface adjustments
Shakhrai8 May 28, 2023
8cfe2d6
add new tests
Shakhrai8 May 28, 2023
91223c2
add new tests, pass tests
Shakhrai8 May 29, 2023
6e9c9b0
add test unit for case 2, pass test
Shakhrai8 May 29, 2023
b5d6f55
updated design
Shakhrai8 May 29, 2023
741b432
add tests, pass tests
Shakhrai8 May 29, 2023
c15e46e
correcting via rubocop
Shakhrai8 May 29, 2023
e0def1a
refactored
Shakhrai8 May 29, 2023
5cedcb9
refactored
Shakhrai8 May 29, 2023
e09e55f
refactored with rubocop
Shakhrai8 May 29, 2023
b632390
add input validation for id
Shakhrai8 May 29, 2023
8423088
Update README.md
Shakhrai8 Jun 2, 2023
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: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ group :test do
end

group :development, :test do
gem 'rubocop', '1.20'
gem 'rubocop', '1.51'
end

gem "pg", "~> 1.3"
66 changes: 34 additions & 32 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,43 @@ GEM
specs:
ansi (1.5.0)
ast (2.4.2)
diff-lcs (1.4.4)
diff-lcs (1.5.0)
docile (1.4.0)
parallel (1.20.1)
parser (3.0.2.0)
json (2.6.3)
parallel (1.23.0)
parser (3.2.2.1)
ast (~> 2.4.1)
pg (1.3.5)
rainbow (3.0.0)
regexp_parser (2.1.1)
pg (1.5.3)
rainbow (3.1.1)
regexp_parser (2.8.0)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rubocop (1.20.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.51.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.0.0.0)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.9.1, < 2.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
simplecov (0.21.2)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.28.1)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
Expand All @@ -46,23 +48,23 @@ GEM
simplecov
terminal-table
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
terminal-table (3.0.1)
simplecov_json_formatter (0.1.4)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
unicode-display_width (2.0.0)
unicode-display_width (2.4.2)

PLATFORMS
ruby
arm64-darwin-22

DEPENDENCIES
pg (~> 1.3)
rspec
rubocop (= 1.20)
rubocop (= 1.51)
simplecov
simplecov-console

RUBY VERSION
ruby 3.0.2p107

BUNDLED WITH
2.2.26
2.2.22
95 changes: 1 addition & 94 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,94 +1 @@
Shop Manager Project
=================

* Feel free to use Google, your notes, books, etc. but work on your own
* If you refer to the solution of another coach or student, please put a link to that in your README
* If you have a partial solution, **still check in a partial solution**
* You must submit a pull request to this repo with your code next Monday morning

Challenge:
-------

Please start by [forking this repo](https://github.com/makersacademy/shop-manager-challenge/fork), then clone your fork to your local machine. Work into that directory.

We are going to write a small terminal program allowing the user to manage a shop database containing some items and orders.

User stories:
-------

```
As a shop manager
So I can know which items I have in stock
I want to keep a list of my shop items with their name and unit price.

As a shop manager
So I can know which items I have in stock
I want to know which quantity (a number) I have for each item.

As a shop manager
So I can manage items
I want to be able to create a new item.

As a shop manager
So I can know which orders were made
I want to keep a list of orders with their customer name.

As a shop manager
So I can know which orders were made
I want to assign each order to their corresponding item.

As a shop manager
So I can know which orders were made
I want to know on which date an order was placed.

As a shop manager
So I can manage orders
I want to be able to create a new order.
```

Here's an example of the terminal output your program should generate (yours might be slightly different — that's totally OK):

```
Welcome to the shop management program!

What do you want to do?
1 = list all shop items
2 = create a new item
3 = list all orders
4 = create a new order

1 [enter]

Here's a list of all shop items:

#1 Super Shark Vacuum Cleaner - Unit price: 99 - Quantity: 30
#2 Makerspresso Coffee Machine - Unit price: 69 - Quantity: 15
(...)
```

Technical Approach:
-----

In this unit, you integrated a database by using the `PG` gem, and test-driving and building Repository classes. You can continue to use this approach when building this challenge.

[You'll also need to mock IO](https://github.com/makersacademy/golden-square/blob/main/mocking_bites/05_unit_testing_terminal_io_bite.md) in your integration or unit tests, since the program will ask for user input.

Notes on test coverage
----------------------

Please ensure you have the following **AT THE TOP** of your spec_helper.rb in order to have test coverage stats generated
on your pull request:

```ruby
require 'simplecov'
require 'simplecov-console'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
# SimpleCov::Formatter::HTMLFormatter
])
SimpleCov.start
```

You can see your test coverage when you run your tests. If you want this in a graphical form, uncomment the `HTMLFormatter` line and see what happens!
## SHOP-MANAGER CHALLENGE
117 changes: 117 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require 'date'
require_relative './lib/order_repository'
require_relative './lib/item_repository'
require_relative './lib/order_item_repository'
require_relative './lib/database_connection'

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

def run
@io.puts "----------------------------------------------"
@io.puts "Welcome to the shop management program!\n\n"
@io.puts "What do you want to do?"
@io.puts " 1 = list all shop items"
@io.puts " 2 = create a new item"
@io.puts " 3 = list all orders"
@io.puts " 4 = create a new order"

@io.print "\nEnter your choice: "
choice = @io.gets.chomp.to_i

case choice
when 1
@io.puts "\nHere's a list of all shop items:"
items = @item_repository.all
items.each do |item|
@io.puts " ##{item.id} #{item.name} - Unit price: #{item.unit_price}" \
" - Quantity: #{item.quantity}"
end
when 2
@io.puts "\nCreating a new item..."
@io.print "Enter item name: "
item_name = @io.gets.chomp
@io.print "Enter unit price: "
unit_price = @io.gets.chomp.to_f
@io.print "Enter quantity: "
quantity = @io.gets.chomp.to_i

new_item = Item.new
new_item.name = item_name
new_item.unit_price = unit_price
new_item.quantity = quantity

@item_repository.create(new_item)
@io.puts "\nNew item created successfully.\n"
when 3
@io.puts "\nHere's a list of all orders:"
orders = @order_repository.all
orders.each do |order|
@io.puts " ##{order.id} - Customer Name: #{order.customer_name}" \
" - Order Date: #{order.order_date}\n"
end
when 4
@io.puts "\nCreating a new order..."
@io.print "Enter customer name: "
customer_name = @io.gets.chomp
order_date = Date.today

new_order = Order.new
new_order.customer_name = customer_name
new_order.order_date = order_date

@order_repository.create(new_order) # Create the order

@io.puts "\nNew order created successfully.\n"

# Prompt to add items to the order
loop do
@io.print "Do you want to add an item to this order? (y/n):\n "
add_item_choice = @io.gets.chomp.downcase

break if add_item_choice != 'y'
items_show = @item_repository.all
items_show.each do |item|
@io.puts " ##{item.id} #{item.name} - Unit price: #{item.unit_price}" \
" - Quantity: #{item.quantity}"
end
order_all = @order_repository.all
@io.puts "----------------------------------------------------"
@io.print "Enter the ID of the item to add: "
item_id_input = @io.gets.chomp
item_id = item_id_input.to_i

if item_id.to_s == item_id_input && !item_id.zero?
item = @item_repository.find(item_id)
if item
@order_item_repository.create(order_all.last.id, item.id)
@io.puts "Item added to the order.\n"
else
@io.puts "Invalid item ID. Please try again.\n"
end
else
@io.puts "Invalid item ID. Please try again.\n"
end
end
else
@io.puts "\nInvalid choice. Please try again."
end
end
end

if __FILE__ == $0
app = Application.new(
'shop_manager',
Kernel,
ItemRepository.new,
OrderRepository.new,
OrderItemRepository.new
)
app.run
end
26 changes: 26 additions & 0 deletions lib/database_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
3 changes: 3 additions & 0 deletions lib/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Item
attr_accessor :id, :name, :unit_price, :quantity
end
Loading