Skip to content

Commit

Permalink
Create routes for remaining relationships
Browse files Browse the repository at this point in the history
Create tests for merchant/invoice relationship
Create controller for merchants/invoices
  • Loading branch information
epintozzi committed Nov 29, 2016
1 parent b791cd2 commit 49bfb95
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--color
--format=documentation
--require spec_helper
5 changes: 5 additions & 0 deletions app/controllers/api/v1/merchants/invoices_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Api::V1::Merchants::InvoicesController < ApplicationController
def index
render json: Merchant.find(params[:merchant_id]).invoices
end
end
45 changes: 37 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
Rails.application.routes.draw do



namespace :api do
namespace :v1 do
# resources :merchants, only: [:index]
resources :merchants, only: [:index, :show] do
scope module: "merchants" do
resources :items, only: [:index]
resources :invoices, only: [:index]
end
end
resources :customers, only: [:index, :show]
resources :invoices, only: [:index, :show]
resources :items, only: [:index, :show]
resources :invoice_items, only: [:index, :show]
resources :transactions, only: [:index, :show]

resources :customers, only: [:index, :show] do
scope module: "customers" do
resources :invoices, only: [:index]
resources :transactions, only: [:index]
end
end

resources :invoices, only: [:index, :show] do
scope module: "invoices" do
resources :transactions, only: [:index]
resources :invoice_items, only: [:index]
resources :items, only: [:index]
resources :customers, only: [:index]
resources :merchants, only: [:index]
end
end

resources :items, only: [:index, :show] do
scope module: "items" do
resources :invoice_items, only: [:index]
resources :merchants, only: [:index]
end
end

resources :invoice_items, only: [:index, :show] do
scope module: "invoice_items" do
resources :invoices, only: [:index]
resources :items, only: [:index]
end
end

resources :transactions, only: [:index, :show] do
scope module: "transactions" do
resources :invoices, only: [:index]
end
end
end
end
end
1 change: 1 addition & 0 deletions spec/factories/merchants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
factory :merchant do
name "Merchant Name"
end

end
20 changes: 20 additions & 0 deletions spec/requests/api/v1/merchants/invoices_requests_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails_helper'

describe "merchant/invoices relationship endpoints" do
context "get /api/v1/merchants/:merchant_id/invoices" do
it "returns a collection of invoices associated with that merchant" do
merchant = create(:merchant)
merchant_2 = create(:merchant)
create_list(:invoice, 3, merchant_id: merchant.id)
create(:invoice, merchant_id: merchant_2.id)

get "/api/v1/merchants/#{merchant.id}/invoices"


invoices = JSON.parse(response.body)

expect(response).to be_success
expect(invoices.count).to eq(3)
end
end
end
1 change: 1 addition & 0 deletions spec/requests/api/v1/merchants_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
expect(merchant["id"]).to eq(id)
end
end

end

0 comments on commit 49bfb95

Please sign in to comment.