Skip to content

Commit af93f68

Browse files
author
Ika Grabon
committed
get /api/products endpoint
1 parent 86c6485 commit af93f68

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727

2828
# Ignore master key for decrypting credentials and more.
2929
/config/master.key
30+
.byebug_history

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,4 @@ RUBY VERSION
221221
ruby 3.2.2p53
222222

223223
BUNDLED WITH
224-
2.4.10
224+
2.4.19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Api::ProductsController < ApplicationController
2+
def index
3+
@products = Product.all
4+
render json: @products, status: :ok
5+
end
6+
end

config/routes.rb

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33

44
# Defines the root path route ("/")
55
# root "articles#index"
6+
namespace :api do
7+
resources :products, only: [:index]
8+
end
69
end

spec/requests/api/products_spec.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "Api::Products", type: :request do
4+
let!(:products) { FactoryBot.create_list(:product, 2) }
5+
6+
describe 'GET /api/products' do
7+
before do
8+
get '/api/products'
9+
end
10+
11+
it 'returns a list of all existing products with the correct attributes' do
12+
expect(response).to have_http_status(200)
13+
products_response = JSON.parse(response.body)
14+
15+
expect(products_response.size).to eq(2)
16+
products_response.each_with_index do |product_response, index|
17+
product = products[index]
18+
19+
expect(product_response['id']).to eq(product.id)
20+
expect(product_response['code']).to eq(product.code)
21+
expect(product_response['name']).to eq(product.name)
22+
expect(product_response['price']).to eq(product.price.to_s)
23+
24+
expect(product_response['created_at'].to_datetime.to_i).to eq(product.created_at.to_datetime.to_i)
25+
expect(product_response['updated_at'].to_datetime.to_i).to eq(product.updated_at.to_datetime.to_i)
26+
end
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)