Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions app/controllers/api/v1/benefits_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Api
module V1
class BenefitsController < ApplicationController

def index
@benefits = Benefit.all
end
end
end
end
13 changes: 13 additions & 0 deletions app/models/benefit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# == Schema Information
#
# Table name: benefits
#
# id :bigint not null, primary key
# name :string not null
# price :decimal(, ) not null
# created_at :datetime not null
# updated_at :datetime not null
#
class Benefit < ApplicationRecord
validates :name, presence: true, uniqueness: true
end
7 changes: 7 additions & 0 deletions app/views/api/v1/benefits/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
json.benefits do
json.array! @benefits do |benefit|
json.id benefit.id
json.name benefit.name
json.price benefit.price
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace :v1, defaults: { format: :json } do
resources :reset_passwords, only: :index
resources :topics, only: :index
resources :benefits, only: :index
resources :targets, only: [:index, :create, :destroy]
resources :conversations, only: :index do
resources :messages, only: [:index, :create]
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20210902163709_create_benefits.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateBenefits < ActiveRecord::Migration[6.1]
def change
create_table :benefits do |t|
t.string :name, null: false
t.decimal :price, null: false

t.timestamps
end

add_index :benefits, :name, unique: true
end
end
10 changes: 9 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
topic3 = Topic.create(name: "Travel", image: Faker::LoremFlickr.image(search_terms: ['travel']))
topic4 = Topic.create(name: "Art", image: Faker::LoremFlickr.image(search_terms: ['art']))
topic5 = Topic.create(name: "Dating", image: Faker::LoremFlickr.image(search_terms: ['love']))

benefit1 = Benefit.create(name: "Basic Benefit", price: 7)
benefit2 = Benefit.create(name: "Plus Benefit", price: 15)
benefit3 = Benefit.create(name: "Premium Benefit", price: 22)
18 changes: 18 additions & 0 deletions spec/factories/benefits.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# Table name: benefits
#
# id :bigint not null, primary key
# name :string not null
# price :decimal(, ) not null
# created_at :datetime not null
# updated_at :datetime not null
#
require 'faker'

FactoryBot.define do
factory :benefit do
name { Faker::Lorem.unique.word }
price { Faker::Commerce.price }
end
end
25 changes: 25 additions & 0 deletions spec/requests/api/v1/benefits/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "rails_helper"

describe "GET api/v1/benefits", type: :request do

context 'when the route is valid' do
before do
create_list(:benefit, 10)
get api_v1_benefits_path
end

it 'returns status 200' do
expect(response).to be_successful
end

it 'returns first data row attributes' do
expect(json_response['benefits'][0]['id']).to be_present
expect(json_response['benefits'][0]['name']).to be_present
expect(json_response['benefits'][0]['price']).to be_present
end

it "returns the total data generated" do
expect(json_response['benefits'].length).to eq(10)
end
end
end