From 72301dceef452f7935c86468dc53cfff3a134a08 Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:14:21 -0600 Subject: [PATCH 01/12] test: merchants service --- spec/services/merchants_service_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 spec/services/merchants_service_spec.rb diff --git a/spec/services/merchants_service_spec.rb b/spec/services/merchants_service_spec.rb new file mode 100644 index 00000000..78ce0eea --- /dev/null +++ b/spec/services/merchants_service_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe MerchantsService do + it 'returns a list of the first 20 merchants' do + merchants = MerchantsService.get_merchants + + expect(merchants).to be_a(Hash) + expect(merchants).to have_key(:data) + expect(merchants[:data]).to be_an(Array) + + merchants[:data].each do |merchant| + expect(merchant).to have_key(:id) + expect(merchant[:type]).to eq("merchant") + expect(merchant).to have_key(:attributes) + expect(merchant[:attributes]).to have_key(:name) + expect(merchant[:attributes][:name]).to be_a(String) + end + end +end \ No newline at end of file From 89f5c029309263e204365f90fc3b62154cb87603 Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:14:37 -0600 Subject: [PATCH 02/12] test: merchant poro --- spec/poros/merchant_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 spec/poros/merchant_spec.rb diff --git a/spec/poros/merchant_spec.rb b/spec/poros/merchant_spec.rb new file mode 100644 index 00000000..2ea490d9 --- /dev/null +++ b/spec/poros/merchant_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +RSpec.describe Merchant do + before :each do + merchants = JSON.parse(file_fixture("merchants_response.json").read, symbolize_names: true) + merchant_data = merchants[:data].first + + @merchant = Merchant.new(merchant_data) + end + + it 'exists and has attributes' do + expect(@merchant).to be_instance_of(Merchant) + expect(@merchant.id).to eq(1) + expect(@merchant.name).to eq("Schroeder-Jerde") + end +end \ No newline at end of file From 9a15d2acb179f11869e63d201f4e99fc4354626b Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:15:13 -0600 Subject: [PATCH 03/12] feat: implement merchants service/facade/poro --- app/facades/merchants_facade.rb | 8 ++++++++ app/poros/merchant.rb | 8 ++++++++ app/services/merchants_service.rb | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 app/facades/merchants_facade.rb create mode 100644 app/poros/merchant.rb create mode 100644 app/services/merchants_service.rb diff --git a/app/facades/merchants_facade.rb b/app/facades/merchants_facade.rb new file mode 100644 index 00000000..63eec291 --- /dev/null +++ b/app/facades/merchants_facade.rb @@ -0,0 +1,8 @@ +class MerchantsFacade + def self.get_merchants + merchants_data = MerchantsService.get_merchants[:data] + merchants_data.map do |merchant_data| + Merchant.new(merchant_data) + end + end +end \ No newline at end of file diff --git a/app/poros/merchant.rb b/app/poros/merchant.rb new file mode 100644 index 00000000..32f0c15b --- /dev/null +++ b/app/poros/merchant.rb @@ -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 \ No newline at end of file diff --git a/app/services/merchants_service.rb b/app/services/merchants_service.rb new file mode 100644 index 00000000..d6050839 --- /dev/null +++ b/app/services/merchants_service.rb @@ -0,0 +1,18 @@ +class MerchantsService + + def self.get_merchants + response = conn.get("/api/v1/merchants") + JSON.parse(response.body, symbolize_names: true) + end + + def self.get_merchant(merchant_id) + response = conn.get("/merchants/#{merchant_id}") + JSON.parse(response.body, symbolize_names: true) + end + + private + + def self.conn + Faraday.new(url: "http://localhost:3000/") + end +end \ No newline at end of file From 702a005574fa754bdbd1cdcd26f94966a1f34370 Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:15:25 -0600 Subject: [PATCH 04/12] test: merchants index --- spec/features/merchants/index_spec.rb | 16 ++++++++++++++++ spec/fixtures/files/merchants_response.json | 1 + 2 files changed, 17 insertions(+) create mode 100644 spec/features/merchants/index_spec.rb create mode 100644 spec/fixtures/files/merchants_response.json diff --git a/spec/features/merchants/index_spec.rb b/spec/features/merchants/index_spec.rb new file mode 100644 index 00000000..136df6e9 --- /dev/null +++ b/spec/features/merchants/index_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +RSpec.describe "merchants index page" do + + before :each do + @merchants = MerchantsFacade.get_merchants + end + + it 'lists all the merchants' do + visit merchants_path + + @merchants.each do |merchant| + expect(page).to have_content(merchant.name) + end + end +end \ No newline at end of file diff --git a/spec/fixtures/files/merchants_response.json b/spec/fixtures/files/merchants_response.json new file mode 100644 index 00000000..71ee1473 --- /dev/null +++ b/spec/fixtures/files/merchants_response.json @@ -0,0 +1 @@ +{"data":[{"id":"1","type":"merchant","attributes":{"name":"Schroeder-Jerde"}},{"id":"2","type":"merchant","attributes":{"name":"Klein, Rempel and Jones"}},{"id":"3","type":"merchant","attributes":{"name":"Willms and Sons"}},{"id":"4","type":"merchant","attributes":{"name":"Cummings-Thiel"}},{"id":"5","type":"merchant","attributes":{"name":"Williamson Group"}},{"id":"6","type":"merchant","attributes":{"name":"Williamson Group"}},{"id":"7","type":"merchant","attributes":{"name":"Bernhard-Johns"}},{"id":"8","type":"merchant","attributes":{"name":"Osinski, Pollich and Koelpin"}},{"id":"9","type":"merchant","attributes":{"name":"Hand-Spencer"}},{"id":"10","type":"merchant","attributes":{"name":"Bechtelar, Jones and Stokes"}},{"id":"11","type":"merchant","attributes":{"name":"Pollich and Sons"}},{"id":"12","type":"merchant","attributes":{"name":"Kozey Group"}},{"id":"13","type":"merchant","attributes":{"name":"Tillman Group"}},{"id":"14","type":"merchant","attributes":{"name":"Dicki-Bednar"}},{"id":"15","type":"merchant","attributes":{"name":"Adams-Kovacek"}},{"id":"16","type":"merchant","attributes":{"name":"Bosco, Howe and Davis"}},{"id":"17","type":"merchant","attributes":{"name":"Ullrich-Moen"}},{"id":"18","type":"merchant","attributes":{"name":"Koepp LLC"}},{"id":"19","type":"merchant","attributes":{"name":"Brown Inc"}},{"id":"20","type":"merchant","attributes":{"name":"Schulist, Wilkinson and Leannon"}}]} \ No newline at end of file From 984d975cf2e3c32cb1866363fd94920f03a4e6c3 Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:18:54 -0600 Subject: [PATCH 05/12] feat: merchants index page lists merchants --- Gemfile | 3 +++ Gemfile.lock | 11 +++++++++++ app/controllers/merchants_controller.rb | 5 +++++ app/views/merchants/index.html.erb | 5 +++++ config/routes.rb | 1 + 5 files changed, 25 insertions(+) create mode 100644 app/controllers/merchants_controller.rb create mode 100644 app/views/merchants/index.html.erb diff --git a/Gemfile b/Gemfile index d4fbf7c0..fc257b3c 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,8 @@ 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] @@ -54,5 +56,6 @@ group :development do end + # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] diff --git a/Gemfile.lock b/Gemfile.lock index 9c8920e9..55d2c8f2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -72,6 +72,10 @@ GEM diff-lcs (1.4.4) erubi (1.10.0) execjs (2.8.1) + faraday (2.6.0) + faraday-net_http (>= 2.0, < 3.1) + ruby2_keywords (>= 0.0.4) + faraday-net_http (3.0.1) ffi (1.15.4) globalid (0.5.2) activesupport (>= 5.0) @@ -92,9 +96,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) @@ -153,6 +161,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) @@ -199,12 +208,14 @@ GEM PLATFORMS arm64-darwin-20 + ruby DEPENDENCIES bootsnap (>= 1.1.0) byebug capybara coffee-rails (~> 4.2) + faraday jbuilder (~> 2.5) listen (>= 3.0.5, < 3.2) pg (>= 0.18, < 2.0) diff --git a/app/controllers/merchants_controller.rb b/app/controllers/merchants_controller.rb new file mode 100644 index 00000000..c3d30cba --- /dev/null +++ b/app/controllers/merchants_controller.rb @@ -0,0 +1,5 @@ +class MerchantsController < ApplicationController + def index + @merchants = MerchantsFacade.get_merchants + end +end \ No newline at end of file diff --git a/app/views/merchants/index.html.erb b/app/views/merchants/index.html.erb new file mode 100644 index 00000000..eae3b0e3 --- /dev/null +++ b/app/views/merchants/index.html.erb @@ -0,0 +1,5 @@ +
    + <% @merchants.each do |merchant| %> +
  • <%= merchant.name %>
  • + <% end %> +
diff --git a/config/routes.rb b/config/routes.rb index 787824f8..7526865c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ 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] end From 26a1fdb5fa4b2519571e2f9a7c4c4a73ba6e22ad Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:22:43 -0600 Subject: [PATCH 06/12] test: merchants name is link to show page --- spec/features/merchants/index_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/features/merchants/index_spec.rb b/spec/features/merchants/index_spec.rb index 136df6e9..d69ab875 100644 --- a/spec/features/merchants/index_spec.rb +++ b/spec/features/merchants/index_spec.rb @@ -13,4 +13,16 @@ expect(page).to have_content(merchant.name) end end + + it 'each merchant name is a link to their show page' do + visit merchants_path + + @merchants.each do |merchant| + expect(page).to have_link(merchant.name) + end + + click_link @merchants.first.name + + expect(current_path).to eq(merchant_path(@merchants.first.id)) + end end \ No newline at end of file From 41ed1c3cff9828e591eaa11e2efcd50bd5907bd8 Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:24:31 -0600 Subject: [PATCH 07/12] feat: merchants index links to show page --- app/controllers/merchants_controller.rb | 4 ++++ app/views/merchants/index.html.erb | 2 +- app/views/merchants/show.html.erb | 0 config/routes.rb | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 app/views/merchants/show.html.erb diff --git a/app/controllers/merchants_controller.rb b/app/controllers/merchants_controller.rb index c3d30cba..c106d58d 100644 --- a/app/controllers/merchants_controller.rb +++ b/app/controllers/merchants_controller.rb @@ -2,4 +2,8 @@ class MerchantsController < ApplicationController def index @merchants = MerchantsFacade.get_merchants end + + def show + + end end \ No newline at end of file diff --git a/app/views/merchants/index.html.erb b/app/views/merchants/index.html.erb index eae3b0e3..890c5386 100644 --- a/app/views/merchants/index.html.erb +++ b/app/views/merchants/index.html.erb @@ -1,5 +1,5 @@
    <% @merchants.each do |merchant| %> -
  • <%= merchant.name %>
  • +
  • <%= link_to merchant.name, merchant_path(merchant.id) %>
  • <% end %>
diff --git a/app/views/merchants/show.html.erb b/app/views/merchants/show.html.erb new file mode 100644 index 00000000..e69de29b diff --git a/config/routes.rb b/config/routes.rb index 7526865c..3d28e9da 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ 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] + resources :merchants, only: [:index, :show] end From 3b7034378f0ff57f50bc40fde64588822594355e Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:32:30 -0600 Subject: [PATCH 08/12] add single merchant to service and facade --- app/facades/merchants_facade.rb | 5 +++++ app/services/merchants_service.rb | 2 +- spec/features/merchants/show_spec.rb | 6 ++++++ spec/services/merchants_service_spec.rb | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/features/merchants/show_spec.rb diff --git a/app/facades/merchants_facade.rb b/app/facades/merchants_facade.rb index 63eec291..e354b2d2 100644 --- a/app/facades/merchants_facade.rb +++ b/app/facades/merchants_facade.rb @@ -5,4 +5,9 @@ def self.get_merchants Merchant.new(merchant_data) end end + + def self.get_merchant(merchant_id) + merchant_data = MerchantsService.get_merchant(merchant_id)[:data] + Merchant.new(merchant_data) + end end \ No newline at end of file diff --git a/app/services/merchants_service.rb b/app/services/merchants_service.rb index d6050839..aac2b005 100644 --- a/app/services/merchants_service.rb +++ b/app/services/merchants_service.rb @@ -6,7 +6,7 @@ def self.get_merchants end def self.get_merchant(merchant_id) - response = conn.get("/merchants/#{merchant_id}") + response = conn.get("/api/v1/merchants/#{merchant_id}") JSON.parse(response.body, symbolize_names: true) end diff --git a/spec/features/merchants/show_spec.rb b/spec/features/merchants/show_spec.rb new file mode 100644 index 00000000..3ef4928a --- /dev/null +++ b/spec/features/merchants/show_spec.rb @@ -0,0 +1,6 @@ +require 'rails_helper' + +RSpec.describe "merchants show page" do + + before :each do +end \ No newline at end of file diff --git a/spec/services/merchants_service_spec.rb b/spec/services/merchants_service_spec.rb index 78ce0eea..642dd541 100644 --- a/spec/services/merchants_service_spec.rb +++ b/spec/services/merchants_service_spec.rb @@ -16,4 +16,19 @@ expect(merchant[:attributes][:name]).to be_a(String) end end + + it 'returns a single merchant' do + first_merchant_id = MerchantsService.get_merchants[:data].first[:id] + + first_merchant = MerchantsService.get_merchant(first_merchant_id) + + expect(first_merchant).to be_a(Hash) + expect(first_merchant).to have_key(:data) + expect(first_merchant[:data]).to be_a(Hash) + expect(first_merchant[:data][:id]).to eq(first_merchant_id) + expect(first_merchant[:data][:type]).to eq("merchant") + expect(first_merchant[:data][:attributes]).to be_a(Hash) + expect(first_merchant[:data][:attributes]).to have_key(:name) + expect(first_merchant[:data][:attributes][:name]).to be_a(String) + end end \ No newline at end of file From 979d3342b73ac3d112c625cf49cf35e4190cebcf Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:37:47 -0600 Subject: [PATCH 09/12] feat: merchants show page displays merchant name --- app/controllers/merchants_controller.rb | 2 +- app/views/merchants/show.html.erb | 1 + db/schema.rb | 18 ++++++++++++++++++ spec/features/merchants/show_spec.rb | 9 ++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 db/schema.rb diff --git a/app/controllers/merchants_controller.rb b/app/controllers/merchants_controller.rb index c106d58d..fc11654f 100644 --- a/app/controllers/merchants_controller.rb +++ b/app/controllers/merchants_controller.rb @@ -4,6 +4,6 @@ def index end def show - + @merchant = MerchantsFacade.get_merchant(params[:id]) end end \ No newline at end of file diff --git a/app/views/merchants/show.html.erb b/app/views/merchants/show.html.erb index e69de29b..71e652b6 100644 --- a/app/views/merchants/show.html.erb +++ b/app/views/merchants/show.html.erb @@ -0,0 +1 @@ +

<%= @merchant.name %>

\ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..2611543b --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,18 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 0) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + +end diff --git a/spec/features/merchants/show_spec.rb b/spec/features/merchants/show_spec.rb index 3ef4928a..a566f619 100644 --- a/spec/features/merchants/show_spec.rb +++ b/spec/features/merchants/show_spec.rb @@ -1,6 +1,13 @@ require 'rails_helper' RSpec.describe "merchants show page" do - + before :each do + @merchant = MerchantsFacade.get_merchants.first + visit merchant_path(@merchant.id) + end + + it 'shows the merchants name' do + expect(page).to have_content(@merchant.name) + end end \ No newline at end of file From 9e37c6c824a030c9e3fb66bfbbdae3f549e664ad Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 10:39:52 -0600 Subject: [PATCH 10/12] test: merchants show page has merchant items --- spec/features/merchants/show_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/features/merchants/show_spec.rb b/spec/features/merchants/show_spec.rb index a566f619..94f5e278 100644 --- a/spec/features/merchants/show_spec.rb +++ b/spec/features/merchants/show_spec.rb @@ -4,10 +4,17 @@ before :each do @merchant = MerchantsFacade.get_merchants.first + @merchant_items = ItemsFacade.get_merchant_items(@merchant.id) visit merchant_path(@merchant.id) end it 'shows the merchants name' do expect(page).to have_content(@merchant.name) end + + it 'shows the merchant items' do + @merchant_items.each do |item| + expect(page).to have_content(item.name) + end + end end \ No newline at end of file From 684fa3dbc356eb20c6acbfc2f0974944f1f8bd0f Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 11:43:14 -0600 Subject: [PATCH 11/12] feat: merchant items listed on show page --- app/controllers/items_controller.rb | 9 +++++++++ app/facades/items_facade.rb | 20 ++++++++++++++++++++ app/poros/item.rb | 10 ++++++++++ app/poros/merchant.rb | 6 +++++- app/services/items_service.rb | 22 ++++++++++++++++++++++ app/views/items/index.html.erb | 0 app/views/items/show.html.erb | 0 app/views/merchants/show.html.erb | 7 ++++++- config/routes.rb | 1 + spec/features/merchants/show_spec.rb | 10 ++++++---- spec/poros/item_spec.rb | 7 +++++++ 11 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 app/controllers/items_controller.rb create mode 100644 app/facades/items_facade.rb create mode 100644 app/poros/item.rb create mode 100644 app/services/items_service.rb create mode 100644 app/views/items/index.html.erb create mode 100644 app/views/items/show.html.erb create mode 100644 spec/poros/item_spec.rb diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb new file mode 100644 index 00000000..976da56a --- /dev/null +++ b/app/controllers/items_controller.rb @@ -0,0 +1,9 @@ +class ItemsController < ApplicationController + def index + @items = ItemsFacade.get_items + end + + def show + @item = ItemsFacade.get_item(params[:id]) + end +end \ No newline at end of file diff --git a/app/facades/items_facade.rb b/app/facades/items_facade.rb new file mode 100644 index 00000000..93c5417b --- /dev/null +++ b/app/facades/items_facade.rb @@ -0,0 +1,20 @@ +class ItemsFacade + def self.get_merchant_items(merchant_id) + items_data = ItemsService.get_merchant_items(merchant_id)[:data] + items_data.map do |item_data| + Item.new(item_data) + end + end + + def self.get_item(item_id) + item_data = ItemsService.get_item(item_id)[:data] + Item.new(item_data) + end + + def self.get_items + items_data = ItemsSerice.get_items[:data] + items_data.map do |item_data| + Item.new(item_data) + end + end +end \ No newline at end of file diff --git a/app/poros/item.rb b/app/poros/item.rb new file mode 100644 index 00000000..e691e1a5 --- /dev/null +++ b/app/poros/item.rb @@ -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 \ No newline at end of file diff --git a/app/poros/merchant.rb b/app/poros/merchant.rb index 32f0c15b..ef50912e 100644 --- a/app/poros/merchant.rb +++ b/app/poros/merchant.rb @@ -1,8 +1,12 @@ class Merchant - attr_reader :id, :name + attr_reader :id, :name, :items def initialize(merchant_data) @id = merchant_data[:id].to_i @name = merchant_data[:attributes][:name] end + + def items + ItemsFacade.get_merchant_items(@id) + end end \ No newline at end of file diff --git a/app/services/items_service.rb b/app/services/items_service.rb new file mode 100644 index 00000000..640a2963 --- /dev/null +++ b/app/services/items_service.rb @@ -0,0 +1,22 @@ +class ItemsService + def self.get_merchant_items(merchant_id) + response = conn.get("/api/v1/merchants/#{merchant_id}/items") + JSON.parse(response.body, symbolize_names: true) + end + + def self.get_items + response = conn.get("/api/v1/items") + JSON.parse(response.body, symbolize_names: true) + end + + def self.get_item(item_id) + response = conn.get("/api/v1/items/#{item_id}") + JSON.parse(response.body, symbolize_names: true) + end + + private + + def self.conn + Faraday.new(url: "http://localhost:3000") + end +end \ No newline at end of file diff --git a/app/views/items/index.html.erb b/app/views/items/index.html.erb new file mode 100644 index 00000000..e69de29b diff --git a/app/views/items/show.html.erb b/app/views/items/show.html.erb new file mode 100644 index 00000000..e69de29b diff --git a/app/views/merchants/show.html.erb b/app/views/merchants/show.html.erb index 71e652b6..a54f3fe6 100644 --- a/app/views/merchants/show.html.erb +++ b/app/views/merchants/show.html.erb @@ -1 +1,6 @@ -

<%= @merchant.name %>

\ No newline at end of file +

<%= @merchant.name %>

+
    + <% @merchant.items.each do |item| %> +
  • <%= link_to item.name, item_path(item.id) %>
  • + <% end %> +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 3d28e9da..92725056 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ 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 diff --git a/spec/features/merchants/show_spec.rb b/spec/features/merchants/show_spec.rb index 94f5e278..2ff525f6 100644 --- a/spec/features/merchants/show_spec.rb +++ b/spec/features/merchants/show_spec.rb @@ -4,7 +4,6 @@ before :each do @merchant = MerchantsFacade.get_merchants.first - @merchant_items = ItemsFacade.get_merchant_items(@merchant.id) visit merchant_path(@merchant.id) end @@ -12,9 +11,12 @@ expect(page).to have_content(@merchant.name) end - it 'shows the merchant items' do - @merchant_items.each do |item| - expect(page).to have_content(item.name) + it 'shows the merchant items as links to their show pages' do + @merchant.items.each do |item| + expect(page).to have_link(item.name) end + first_item = @merchant.items.first + click_link first_item.name + expect(current_path).to eq(item_path(first_item.id)) end end \ No newline at end of file diff --git a/spec/poros/item_spec.rb b/spec/poros/item_spec.rb new file mode 100644 index 00000000..711587a6 --- /dev/null +++ b/spec/poros/item_spec.rb @@ -0,0 +1,7 @@ +require 'rails_helper' + +RSpec.describe Item do + before :each do + + end +end \ No newline at end of file From 032fbc346af241136f274a9af46bedfb03dfda1b Mon Sep 17 00:00:00 2001 From: Thomas Musselman Date: Thu, 27 Oct 2022 12:03:59 -0600 Subject: [PATCH 12/12] feat: add merchant relationship for item --- app/poros/item.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/poros/item.rb b/app/poros/item.rb index e691e1a5..167f463d 100644 --- a/app/poros/item.rb +++ b/app/poros/item.rb @@ -6,5 +6,10 @@ def initialize(item_data) @name = item_data[:attributes][:name] @description = item_data[:attributes][:description] @unit_price = item_data[:attributes][:unit_price] + @merchant_id = item_data[:attributes][:merchant_id] + end + + def merchant + MerchantsFacade.get_merchant(@merchant_id) end end \ No newline at end of file