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

Kerynn- SOA Challenge #104

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4e867ed
Set up gems
Kerynn Feb 15, 2023
19cfafa
add faker and factory bot gems
Kerynn Feb 15, 2023
f31898c
initial set up for merchants index view
Kerynn Feb 15, 2023
d866a27
add faraday gem
Kerynn Feb 15, 2023
ffd9853
get basic function working
Kerynn Feb 15, 2023
06e9cf6
create and tests merchants service
Kerynn Feb 15, 2023
0fecf11
add and test merchant facade and poro
Kerynn Feb 15, 2023
dfe69d9
use facade in controller and update views to use attributes
Kerynn Feb 15, 2023
d2f054d
update merchant object to have id attribute
Kerynn Feb 15, 2023
4d3171b
update merchant path to use merchant id
Kerynn Feb 15, 2023
94aa75c
add merchant show action and view to pass merchants with links spec
Kerynn Feb 15, 2023
31e7ad1
update merchant service to have merchant items call
Kerynn Feb 15, 2023
efae2e0
create merchant_items method for facade
Kerynn Feb 15, 2023
76c7bad
create item object
Kerynn Feb 15, 2023
d9edcaf
add find one merchant api call to service
Kerynn Feb 15, 2023
934643c
test: set up test for merchant show spec
Kerynn Feb 15, 2023
7ded04e
update method name to be consistent with rest of methods in services
Kerynn Feb 15, 2023
9a5636d
add facade for finding one merchant
Kerynn Feb 15, 2023
3b9dac4
use facades in merchant show to create views
Kerynn Feb 15, 2023
97d7afc
add total merchants to merchant index page
Kerynn Feb 15, 2023
00fbadc
set up items routes, controller and index/show views
Kerynn Feb 15, 2023
0ec8737
create items service
Kerynn Feb 15, 2023
0cce022
add items facade
Kerynn Feb 15, 2023
6d8567d
list each item name in items index page
Kerynn Feb 15, 2023
7ee9439
add links for each item
Kerynn Feb 15, 2023
6c1bfa7
add get one item to items service
Kerynn Feb 15, 2023
363770b
add find item to items facade
Kerynn Feb 15, 2023
d151ea6
add item details to item show page
Kerynn Feb 15, 2023
3855674
add link to each item show page in merchant show page
Kerynn Feb 15, 2023
116376d
remove factories not using
Kerynn Feb 16, 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
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ gem 'jbuilder', '~> 2.5'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'faraday'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails'
gem 'capybara'
gem 'pry'
gem 'launchy'
gem 'factory_bot_rails'
gem 'faker'
end

group :development do
Expand Down
23 changes: 23 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,26 @@ GEM
diff-lcs (1.4.4)
erubi (1.10.0)
execjs (2.8.1)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
faker (2.19.0)
i18n (>= 1.6, < 2)
faraday (2.7.4)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
ffi (1.15.4)
globalid (0.5.2)
activesupport (>= 5.0)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jbuilder (2.11.2)
activesupport (>= 5.0.0)
launchy (2.5.2)
addressable (~> 2.8)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -92,9 +105,13 @@ GEM
matrix (0.4.2)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.6.1)
minitest (5.14.4)
msgpack (1.4.2)
nio4r (2.5.8)
nokogiri (1.12.5)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
nokogiri (1.12.5-arm64-darwin)
racc (~> 1.4)
pg (1.2.3)
Expand Down Expand Up @@ -153,6 +170,7 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
ruby2_keywords (0.0.5)
ruby_dep (1.5.0)
sass (3.7.4)
sass-listen (~> 4.0.0)
Expand Down Expand Up @@ -199,13 +217,18 @@ GEM

PLATFORMS
arm64-darwin-20
ruby

DEPENDENCIES
bootsnap (>= 1.1.0)
byebug
capybara
coffee-rails (~> 4.2)
factory_bot_rails
faker
faraday
jbuilder (~> 2.5)
launchy
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
pry
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ItemsController < ApplicationController

def index
@items = ItemsFacade.all_items
end

def show
@item = ItemsFacade.find_item(params[:id])
end
end
11 changes: 11 additions & 0 deletions app/controllers/merchants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MerchantsController < ApplicationController

def index
@merchants = MerchantsFacade.all_merchants
end

def show
@merchant = MerchantsFacade.find_merchant(params[:id])
@items = MerchantsFacade.merchant_items(@merchant.id)
end
end
15 changes: 15 additions & 0 deletions app/facades/items_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ItemsFacade

def self.all_items
data = ItemsService.get_all_items
results = data[:data]
results.map do |item_data|
Item.new(item_data)
end
end

def self.find_item(item_id)
data = ItemsService.get_item(item_id)
Item.new(data[:data])
end
end
23 changes: 23 additions & 0 deletions app/facades/merchants_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class MerchantsFacade

def self.all_merchants
data = MerchantsService.get_all_merchants
results = data[:data]
results.map do |merchant_data|
Merchant.new(merchant_data)
end
end

def self.merchant_items(merchant_id)
data = MerchantsService.get_merchant_items(merchant_id)
results = data[:data]
results.map do |item_data|
Item.new(item_data)
end
end

def self.find_merchant(merchant_id)
data = MerchantsService.get_merchant(merchant_id)
Merchant.new(data[:data])
end
end
10 changes: 10 additions & 0 deletions app/poros/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Item
attr_reader :id, :name, :description, :unit_price

def initialize(item_data)
@id = item_data[:id].to_i
@name = item_data[:attributes][:name]
@description = item_data[:attributes][:description]
@unit_price = item_data[:attributes][:unit_price]
end
end
8 changes: 8 additions & 0 deletions app/poros/merchant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Merchant
attr_reader :id, :name

def initialize(merchant_data)
@id = merchant_data[:id].to_i
@name = merchant_data[:attributes][:name]
end
end
18 changes: 18 additions & 0 deletions app/services/items_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ItemsService

def self.get_all_items
parse_data(conn.get("/api/v1/items"))
end

def self.get_item(item_id)
parse_data(conn.get("/api/v1/items/#{item_id}"))
end

def self.parse_data(response)
JSON.parse(response.body, symbolize_names: true)
end

def self.conn
Faraday.new(url: 'http://localhost:3000')
end
end
22 changes: 22 additions & 0 deletions app/services/merchants_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class MerchantsService

def self.get_all_merchants
parse_data(conn.get("/api/v1/merchants"))
end

def self.get_merchant_items(merchant_id)
parse_data(conn.get("/api/v1/merchants/#{merchant_id}/items"))
end

def self.get_merchant(merchant_id)
parse_data(conn.get("/api/v1/merchants/#{merchant_id}"))
end

def self.parse_data(response)
JSON.parse(response.body, symbolize_names: true)
end

def self.conn
Faraday.new(url: 'http://localhost:3000')
end
end
6 changes: 6 additions & 0 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Items</h1>
<p>Total Items: <%= @items.size %></p>

<% @items.each do |item| %>
<li><%= link_to item.name, item_path(item.id) %></li>
<% end %>
3 changes: 3 additions & 0 deletions app/views/items/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1><%= @item.name %></h1>
<p>Item Description: <%= @item.description %></p>
<p>Price: $<%= @item.unit_price %></p>
6 changes: 6 additions & 0 deletions app/views/merchants/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Merchants</h1>
<p>Total Merchants: <%= @merchants.size %></p>

<% @merchants.each do |merchant| %>
<li><%= link_to merchant.name, merchant_path(merchant.id) %></li>
<% end %>
5 changes: 5 additions & 0 deletions app/views/merchants/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1><%= @merchant.name %></h1>
<p>Total Merchant Items: <%= @items.size %></p>
<% @items.each do |item| %>
<li><%= link_to item.name, item_path(item.id) %></li>
<% end %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :merchants, only: [:index, :show]
resources :items, only: [:index, :show]
end
18 changes: 18 additions & 0 deletions spec/facades/item_facade_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rails_helper'

RSpec.describe ItemsFacade do
it 'can return the collection of items' do
items = ItemsFacade.all_items

expect(items).to be_an Array
items.each do |item|
expect(item).to be_an_instance_of(Item)
end
end

it 'can return a single item' do
item = ItemsFacade.find_item(24)

expect(item).to be_an_instance_of(Item)
end
end
27 changes: 27 additions & 0 deletions spec/facades/merchant_facade_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rails_helper'

RSpec.describe MerchantsFacade do
it 'can return the collection of Merchants' do
merchants = MerchantsFacade.all_merchants

expect(merchants).to be_an Array
merchants.each do |merchant|
expect(merchant).to be_an_instance_of(Merchant)
end
end

it 'can return the collection of merchant items' do
merchant_items = MerchantsFacade.merchant_items(1)

expect(merchant_items).to be_an Array
merchant_items.each do |item|
expect(item).to be_an_instance_of(Item)
end
end

it 'can return a single merchant' do
merchant = MerchantsFacade.find_merchant(1)

expect(merchant).to be_an_instance_of(Merchant)
end
end
32 changes: 32 additions & 0 deletions spec/features/items/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rails_helper'

RSpec.describe 'Items Index' do
describe 'when I visit /items' do
it 'shows a list of items by name' do
visit items_path

expect(page.status_code).to eq 200
expect(page).to have_content("Items")
expect(page).to have_content("Total Items: 2483")
expect(page).to have_content("Item Expedita Aliquam")
expect(page).to have_content("Item Expedita Fuga")
expect(page).to have_content("Item Quo Magnam")
end

it 'has a link for each item name to item show page' do
visit items_path

expect(page.status_code).to eq 200

click_link "Item Quo Magnam"

expect(current_path).to eq item_path(9)

visit items_path

click_link "Item Expedita Fuga"

expect(current_path).to eq item_path(7)
end
end
end
14 changes: 14 additions & 0 deletions spec/features/items/show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe 'item show page' do
describe 'when I visit /items/:id' do
it 'shows the item name, description, and unit price' do
visit item_path(20)

expect(page.status_code).to eq 200
expect(page).to have_content("Item Dolorem Et")
expect(page).to have_content("Item Description: Atque voluptatibus molestiae porro. Possimus facere quam dolor voluptate officia soluta. Ipsam dolores tempore ut.")
expect(page).to have_content("Price: $693.38")
end
end
end
32 changes: 32 additions & 0 deletions spec/features/merchants/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rails_helper'

RSpec.describe 'Merchant Index' do
describe 'when I visit /merchants' do
it 'shows a list of merchants by name' do
visit merchants_path

expect(page.status_code).to eq 200
expect(page).to have_content("Merchants")
expect(page).to have_content("Total Merchants: 100")
expect(page).to have_content("Schroeder-Jerde")
expect(page).to have_content("Willms and Sons")
expect(page).to have_content("Pollich and Sons")
end

it 'has a link for each merchant name to merchant show page' do
visit merchants_path

expect(page.status_code).to eq 200

click_link "Schroeder-Jerde"

expect(current_path).to eq merchant_path(1)

visit merchants_path

click_link "Pollich and Sons"

expect(current_path).to eq merchant_path(11)
end
end
end
Loading