Added table jobs and table devices
This commit is contained in:
51
app/controllers/devices_controller.rb
Normal file
51
app/controllers/devices_controller.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
class DevicesController < ApplicationController
|
||||
before_action :set_device, only: [:show, :update, :destroy]
|
||||
|
||||
# GET /devices
|
||||
def index
|
||||
@devices = Device.all
|
||||
|
||||
render json: @devices
|
||||
end
|
||||
|
||||
# GET /devices/1
|
||||
def show
|
||||
render json: @device
|
||||
end
|
||||
|
||||
# POST /devices
|
||||
def create
|
||||
@device = Device.new(device_params)
|
||||
|
||||
if @device.save
|
||||
render json: @device, status: :created, location: @device
|
||||
else
|
||||
render json: @device.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /devices/1
|
||||
def update
|
||||
if @device.update(device_params)
|
||||
render json: @device
|
||||
else
|
||||
render json: @device.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /devices/1
|
||||
def destroy
|
||||
@device.destroy
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_device
|
||||
@device = Device.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def device_params
|
||||
params.require(:device).permit(:id, :device_id)
|
||||
end
|
||||
end
|
||||
51
app/controllers/jobs_controller.rb
Normal file
51
app/controllers/jobs_controller.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
class JobsController < ApplicationController
|
||||
before_action :set_job, only: [:show, :update, :destroy]
|
||||
|
||||
# GET /jobs
|
||||
def index
|
||||
@jobs = Job.all
|
||||
|
||||
render json: @jobs
|
||||
end
|
||||
|
||||
# GET /jobs/1
|
||||
def show
|
||||
render json: @job
|
||||
end
|
||||
|
||||
# POST /jobs
|
||||
def create
|
||||
@job = Job.new(job_params)
|
||||
|
||||
if @job.save
|
||||
render json: @job, status: :created, location: @job
|
||||
else
|
||||
render json: @job.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
# PATCH/PUT /jobs/1
|
||||
def update
|
||||
if @job.update(job_params)
|
||||
render json: @job
|
||||
else
|
||||
render json: @job.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /jobs/1
|
||||
def destroy
|
||||
@job.destroy
|
||||
end
|
||||
|
||||
private
|
||||
# Use callbacks to share common setup or constraints between actions.
|
||||
def set_job
|
||||
@job = Job.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
def job_params
|
||||
params.require(:job).permit(:job_id, :params, :type)
|
||||
end
|
||||
end
|
||||
2
app/models/device.rb
Normal file
2
app/models/device.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class Device < ApplicationRecord
|
||||
end
|
||||
2
app/models/job.rb
Normal file
2
app/models/job.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class Job < ApplicationRecord
|
||||
end
|
||||
@@ -1,3 +1,5 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :devices
|
||||
resources :jobs
|
||||
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
|
||||
end
|
||||
|
||||
11
db/migrate/20211019152106_create_jobs.rb
Normal file
11
db/migrate/20211019152106_create_jobs.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class CreateJobs < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :jobs do |t|
|
||||
t.primary_key :job_id
|
||||
t.json :params
|
||||
t.string :type
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
10
db/migrate/20211019152158_create_devices.rb
Normal file
10
db/migrate/20211019152158_create_devices.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
class CreateDevices < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :devices do |t|
|
||||
t.primary_key :id
|
||||
t.string :device_id
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
38
test/controllers/devices_controller_test.rb
Normal file
38
test/controllers/devices_controller_test.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
require "test_helper"
|
||||
|
||||
class DevicesControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@device = devices(:one)
|
||||
end
|
||||
|
||||
test "should get index" do
|
||||
get devices_url, as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create device" do
|
||||
assert_difference('Device.count') do
|
||||
post devices_url, params: { device: { device_id: @device.device_id, id: @device.id } }, as: :json
|
||||
end
|
||||
|
||||
assert_response 201
|
||||
end
|
||||
|
||||
test "should show device" do
|
||||
get device_url(@device), as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should update device" do
|
||||
patch device_url(@device), params: { device: { device_id: @device.device_id, id: @device.id } }, as: :json
|
||||
assert_response 200
|
||||
end
|
||||
|
||||
test "should destroy device" do
|
||||
assert_difference('Device.count', -1) do
|
||||
delete device_url(@device), as: :json
|
||||
end
|
||||
|
||||
assert_response 204
|
||||
end
|
||||
end
|
||||
38
test/controllers/jobs_controller_test.rb
Normal file
38
test/controllers/jobs_controller_test.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
require "test_helper"
|
||||
|
||||
class JobsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@job = jobs(:one)
|
||||
end
|
||||
|
||||
test "should get index" do
|
||||
get jobs_url, as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should create job" do
|
||||
assert_difference('Job.count') do
|
||||
post jobs_url, params: { job: { job_id: @job.job_id, params: @job.params, type: @job.type } }, as: :json
|
||||
end
|
||||
|
||||
assert_response 201
|
||||
end
|
||||
|
||||
test "should show job" do
|
||||
get job_url(@job), as: :json
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "should update job" do
|
||||
patch job_url(@job), params: { job: { job_id: @job.job_id, params: @job.params, type: @job.type } }, as: :json
|
||||
assert_response 200
|
||||
end
|
||||
|
||||
test "should destroy job" do
|
||||
assert_difference('Job.count', -1) do
|
||||
delete job_url(@job), as: :json
|
||||
end
|
||||
|
||||
assert_response 204
|
||||
end
|
||||
end
|
||||
9
test/fixtures/devices.yml
vendored
Normal file
9
test/fixtures/devices.yml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
id:
|
||||
device_id: MyString
|
||||
|
||||
two:
|
||||
id:
|
||||
device_id: MyString
|
||||
11
test/fixtures/jobs.yml
vendored
Normal file
11
test/fixtures/jobs.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
job_id:
|
||||
params:
|
||||
type:
|
||||
|
||||
two:
|
||||
job_id:
|
||||
params:
|
||||
type:
|
||||
7
test/models/device_test.rb
Normal file
7
test/models/device_test.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class DeviceTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
7
test/models/job_test.rb
Normal file
7
test/models/job_test.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class JobTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
Reference in New Issue
Block a user