Added table jobs and devices

This commit is contained in:
2021-10-20 12:17:31 +02:00
parent 799f55335f
commit 842a55be5f
14 changed files with 266 additions and 0 deletions

View 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(:device_id)
end
end

View 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
View File

@@ -0,0 +1,2 @@
class Device < ApplicationRecord
end

2
app/models/job.rb Normal file
View File

@@ -0,0 +1,2 @@
class Job < ApplicationRecord
end

View File

@@ -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

View File

@@ -0,0 +1,10 @@
class CreateJobs < ActiveRecord::Migration[6.1]
def change
create_table :jobs do |t|
t.json :params
t.string :type
t.timestamps
end
end
end

View File

@@ -0,0 +1,9 @@
class CreateDevices < ActiveRecord::Migration[6.1]
def change
create_table :devices do |t|
t.string :device_id
t.timestamps
end
end
end

31
db/schema.rb generated Normal file
View File

@@ -0,0 +1,31 @@
# 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.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_10_20_101118) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "devices", force: :cascade do |t|
t.string "device_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "jobs", force: :cascade do |t|
t.json "params"
t.string "type"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end

View 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 } }, 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 } }, 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

View 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

7
test/fixtures/devices.yml vendored Normal file
View File

@@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
device_id: MyString
two:
device_id: MyString

11
test/fixtures/jobs.yml vendored Normal file
View 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:

View 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
View File

@@ -0,0 +1,7 @@
require "test_helper"
class JobTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end