Skip to content
Permalink
Browse files

Add Employees: Graphiti API

  • Loading branch information...
Lee Richmond
Lee Richmond committed Sep 3, 2018
1 parent 1dfac6e commit 45c1c92e14fb1c3a47b8ed246ceb2cba50e97c72
@@ -0,0 +1,41 @@
class EmployeesController < ApplicationController
def index
employees = EmployeeResource.all(params)
respond_with(employees)
end

def show
employee = EmployeeResource.find(params)
respond_with(employee)
end

def create
employee = EmployeeResource.build(params)

if employee.save
render jsonapi: employee, status: 201
else
render jsonapi_errors: employee
end
end

def update
employee = EmployeeResource.find(params)

if employee.update_attributes
render jsonapi: employee
else
render jsonapi_errors: employee
end
end

def destroy
employee = EmployeeResource.find(params)

if employee.destroy
render jsonapi: { meta: {} }, status: 200
else
render jsonapi_errors: employee
end
end
end
@@ -0,0 +1,7 @@
class EmployeeResource < ApplicationResource
attribute :first_name, :string
attribute :last_name, :string
attribute :age, :integer
attribute :created_at, :datetime, writable: false
attribute :updated_at, :datetime, writable: false
end
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
scope path: ApplicationResource.endpoint_namespace, defaults: { format: :jsonapi } do
resources :employees
# your routes go here
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
@@ -0,0 +1,23 @@
# 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: 2018_09_03_174755) do

create_table "employees", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.integer "age"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
@@ -0,0 +1,225 @@
{
"resources": [
{
"name": "EmployeeResource",
"type": "employees",
"attributes": {
"id": {
"type": "integer_id",
"readable": true,
"writable": true
},
"first_name": {
"type": "string",
"readable": true,
"writable": true
},
"last_name": {
"type": "string",
"readable": true,
"writable": true
},
"age": {
"type": "integer",
"readable": true,
"writable": true
},
"created_at": {
"type": "datetime",
"readable": true,
"writable": false
},
"updated_at": {
"type": "datetime",
"readable": true,
"writable": false
}
},
"extra_attributes": {
},
"sorts": {
"id": {
},
"first_name": {
},
"last_name": {
},
"age": {
},
"created_at": {
},
"updated_at": {
}
},
"filters": {
"id": {
"type": "integer_id",
"operators": [
"eq",
"not_eq",
"gt",
"gte",
"lt",
"lte"
]
},
"first_name": {
"type": "string",
"operators": [
"eq",
"not_eq",
"eql",
"not_eql",
"prefix",
"not_prefix",
"suffix",
"not_suffix",
"match",
"not_match"
]
},
"last_name": {
"type": "string",
"operators": [
"eq",
"not_eq",
"eql",
"not_eql",
"prefix",
"not_prefix",
"suffix",
"not_suffix",
"match",
"not_match"
]
},
"age": {
"type": "integer",
"operators": [
"eq",
"not_eq",
"gt",
"gte",
"lt",
"lte"
]
},
"created_at": {
"type": "datetime",
"operators": [
"eq",
"not_eq",
"gt",
"gte",
"lt",
"lte"
]
},
"updated_at": {
"type": "datetime",
"operators": [
"eq",
"not_eq",
"gt",
"gte",
"lt",
"lte"
]
}
},
"relationships": {
}
}
],
"endpoints": {
"/api/v1/employees": {
"actions": {
"index": {
"resource": "EmployeeResource"
},
"show": {
"resource": "EmployeeResource"
},
"create": {
"resource": "EmployeeResource"
},
"update": {
"resource": "EmployeeResource"
},
"destroy": {
"resource": "EmployeeResource"
}
}
}
},
"types": {
"integer_id": {
"kind": "scalar",
"description": "Base Type. Query/persist as integer, render as string."
},
"string": {
"kind": "scalar",
"description": "Base Type."
},
"integer": {
"kind": "scalar",
"description": "Base Type."
},
"big_decimal": {
"kind": "scalar",
"description": "Base Type."
},
"float": {
"kind": "scalar",
"description": "Base Type."
},
"boolean": {
"kind": "scalar",
"description": "Base Type."
},
"date": {
"kind": "scalar",
"description": "Base Type."
},
"datetime": {
"kind": "scalar",
"description": "Base Type."
},
"hash": {
"kind": "record",
"description": "Base Type."
},
"array": {
"kind": "array",
"description": "Base Type."
},
"array_of_integer_ids": {
"kind": "array",
"description": "Base Type."
},
"array_of_strings": {
"kind": "array",
"description": "Base Type."
},
"array_of_integers": {
"kind": "array",
"description": "Base Type."
},
"array_of_big_decimals": {
"kind": "array",
"description": "Base Type."
},
"array_of_floats": {
"kind": "array",
"description": "Base Type."
},
"array_of_dates": {
"kind": "array",
"description": "Base Type."
},
"array_of_datetimes": {
"kind": "array",
"description": "Base Type."
}
}
}
@@ -0,0 +1,29 @@
require 'rails_helper'

RSpec.describe "employees#create", type: :request do
subject(:make_request) do
jsonapi_post "/api/v1/employees", payload
end

describe 'basic create' do
let(:payload) do
{
data: {
type: 'employees',
attributes: {
# ... your attrs here
}
}
}
end

it 'works' do
expect(EmployeeResource).to receive(:build).and_call_original
expect {
make_request
}.to change { Employee.count }.by(1)
employee = Employee.last
expect(response.status).to eq(201)
end
end
end
@@ -0,0 +1,20 @@
require 'rails_helper'

RSpec.describe "employees#destroy", type: :request do
subject(:make_request) do
jsonapi_delete "/api/v1/employees/#{employee.id}"
end

describe 'basic destroy' do
let!(:employee) { create(:employee) }

it 'updates the resource' do
expect(EmployeeResource).to receive(:find).and_call_original
expect { make_request }.to change { Employee.count }.by(-1)
expect { employee.reload }
.to raise_error(ActiveRecord::RecordNotFound)
expect(response.status).to eq(200)
expect(json).to eq('meta' => {})
end
end
end
@@ -0,0 +1,22 @@
require 'rails_helper'

RSpec.describe "employees#index", type: :request do
let(:params) { {} }

subject(:make_request) do
jsonapi_get "/api/v1/employees", params: params
end

describe 'basic fetch' do
let!(:employee1) { create(:employee) }
let!(:employee2) { create(:employee) }

it 'works' do
expect(EmployeeResource).to receive(:all).and_call_original
make_request
expect(response.status).to eq(200)
expect(d.map(&:jsonapi_type).uniq).to match_array(['employees'])
expect(d.map(&:id)).to match_array([employee1.id, employee2.id])
end
end
end

0 comments on commit 45c1c92

Please sign in to comment.
You can’t perform that action at this time.