Stub out Enigmas controller

pull/1147/head
Rick Carlino 2019-04-11 16:03:14 -05:00
parent 5319e48d58
commit 133a2a7e78
10 changed files with 205 additions and 58 deletions

View File

@ -0,0 +1,17 @@
module Api
class EnigmasController < Api::AbstractController
def index
render json: current_device.enigmas
end
def destroy
mutate Enigmas::Destroy.run(enigma: enigma)
end
private
def enigma
@enigma ||= current_device.enigmas.find(params[:id])
end
end
end

View File

@ -12,6 +12,7 @@ class Device < ApplicationRecord
"Resuming log storage."
CACHE_KEY = "devices.%s"
has_many :enigmas, dependent: :destroy
has_many :farmware_envs, dependent: :destroy
has_many :farm_events, dependent: :destroy
has_many :farmware_installations, dependent: :destroy

View File

@ -0,0 +1,3 @@
class Enigma < ApplicationRecord
belongs_to :device
end

View File

@ -0,0 +1,11 @@
module Enigmas
class Destroy < Mutations::Command
required do
model :enigma
end
def execute
enigma.destroy!
end
end
end

View File

@ -0,0 +1,7 @@
class EnigmaSerializer < ApplicationSerializer
attributes :created_at, :id, :priority, :problem_tag, :updated_at, :uuid
def created_at
object.created_at.to_i
end
end

View File

@ -1,38 +1,39 @@
FarmBot::Application.routes.draw do
namespace :api, defaults: {format: :json}, constraints: { format: "json" } do
post "/rmq/user" => "rmq_utils#user_action", as: "rmq_user"
post "/rmq/vhost" => "rmq_utils#vhost_action", as: "rmq_vhost"
namespace :api, defaults: { format: :json }, constraints: { format: "json" } do
post "/rmq/user" => "rmq_utils#user_action", as: "rmq_user"
post "/rmq/vhost" => "rmq_utils#vhost_action", as: "rmq_vhost"
post "/rmq/resource" => "rmq_utils#resource_action", as: "rmq_resource"
post "/rmq/topic" => "rmq_utils#topic_action", as: "rmq_topic"
post "/rmq/topic" => "rmq_utils#topic_action", as: "rmq_topic"
# Standard API Resources:
{
diagnostic_dumps: [:create, :destroy, :index],
farm_events: [:create, :destroy, :index, :show, :update],
images: [:create, :destroy, :index, :show],
password_resets: [:create, :update],
peripherals: [:create, :destroy, :index, :show, :update],
sensors: [:create, :destroy, :index, :show, :update],
regimens: [:create, :destroy, :index, :show, :update],
sensor_readings: [:create, :destroy, :index, :show],
sequences: [:create, :destroy, :index, :show, :update],
tools: [:create, :destroy, :index, :show, :update],
webcam_feeds: [:create, :destroy, :index, :show, :update],
farmware_envs: [:create, :destroy, :index, :show, :update],
plant_templates: [:create, :destroy, :index, :update],
pin_bindings: [:create, :destroy, :index, :show, :update]
diagnostic_dumps: [:create, :destroy, :index],
enigmas: [:create, :destroy, :index],
farm_events: [:create, :destroy, :index, :show, :update],
farmware_envs: [:create, :destroy, :index, :show, :update],
images: [:create, :destroy, :index, :show],
password_resets: [:create, :update],
peripherals: [:create, :destroy, :index, :show, :update],
pin_bindings: [:create, :destroy, :index, :show, :update],
plant_templates: [:create, :destroy, :index, :update],
regimens: [:create, :destroy, :index, :show, :update],
sensor_readings: [:create, :destroy, :index, :show],
sensors: [:create, :destroy, :index, :show, :update],
sequences: [:create, :destroy, :index, :show, :update],
tools: [:create, :destroy, :index, :show, :update],
webcam_feeds: [:create, :destroy, :index, :show, :update],
}.to_a.map { |(name, only)| resources name, only: only }
# Singular API Resources:
{
device: [:create, :destroy, :show, :update],
device_cert: [:create],
fbos_config: [:destroy, :show, :update,],
firmware_config: [:destroy, :show, :update,],
public_key: [:show],
tokens: [:create, :show],
web_app_config: [:destroy, :show, :update],
}.to_a.map{|(name, only)| resource name, only: only}
device_cert: [:create],
device: [:create, :destroy, :show, :update],
fbos_config: [:destroy, :show, :update],
firmware_config: [:destroy, :show, :update],
public_key: [:show],
tokens: [:create, :show],
web_app_config: [:destroy, :show, :update],
}.to_a.map { |(name, only)| resource name, only: only }
get "/corpus" => "corpuses#show", as: :api_corpus
resources(:points, except: []) { post :search, on: :collection }
@ -52,46 +53,46 @@ FarmBot::Application.routes.draw do
post :control_certificate, on: :collection
end
resources :saved_gardens, except: [ :show ] do
post :snapshot, on: :collection
post :apply, on: :member
patch :apply, on: :member
resources :saved_gardens, except: [:show] do
post :snapshot, on: :collection
post :apply, on: :member
patch :apply, on: :member
end
get "/global_config" => "global_config#show", as: :global_config
get "/device/sync" => "devices#sync", as: :device_sync
post "/device/seed" => "devices#seed", as: :device_seed
get "/global_config" => "global_config#show", as: :global_config
get "/device/sync" => "devices#sync", as: :device_sync
post "/device/seed" => "devices#seed", as: :device_seed
# Make life easier on API users by not adding special rules for singular
# resources.
# Might be safe to remove now with the advent of TaggedResource.kind
get "/device/:id" => "devices#show", as: :get_device_redirect
get "/device/:id" => "devices#show", as: :get_device_redirect
patch "/device/:id" => "devices#update", as: :patch_device_redirect
put "/device/:id" => "devices#update", as: :put_device_redirect
put "/device/:id" => "devices#update", as: :put_device_redirect
delete "/fbos_config/:id" => "fbos_configs#destroy", as: "delete_fbos_config_redirect"
get "/fbos_config/:id" => "fbos_configs#show", as: "get_fbos_config_redirect"
put "/fbos_config/:id" => "fbos_configs#update", as: "put_fbos_config_redirect"
get "/fbos_config/:id" => "fbos_configs#show", as: "get_fbos_config_redirect"
put "/fbos_config/:id" => "fbos_configs#update", as: "put_fbos_config_redirect"
delete "/firmware_config/:id" => "firmware_configs#destroy", as: "delete_firmware_config_redirect"
get "/firmware_config/:id" => "firmware_configs#show", as: "get_firmware_config_redirect"
patch "/firmware_config/:id" => "firmware_configs#update", as: "patch_firmware_config_redirect"
put "/firmware_config/:id" => "firmware_configs#update", as: "put_firmware_config_redirect"
get "/firmware_config/:id" => "firmware_configs#show", as: "get_firmware_config_redirect"
patch "/firmware_config/:id" => "firmware_configs#update", as: "patch_firmware_config_redirect"
put "/firmware_config/:id" => "firmware_configs#update", as: "put_firmware_config_redirect"
delete "/web_app_config/:id" => "web_app_configs#destroy", as: "delete_web_app_config_redirect"
get "/web_app_config/:id" => "web_app_configs#show", as: "get_web_app_config_redirect"
patch "/web_app_config/:id" => "web_app_configs#update", as: "patch_web_app_config_redirect"
put "/web_app_config/:id" => "web_app_configs#update", as: "put_web_app_config_redirect"
delete "/web_app_config/:id" => "web_app_configs#destroy", as: "delete_web_app_config_redirect"
get "/web_app_config/:id" => "web_app_configs#show", as: "get_web_app_config_redirect"
patch "/web_app_config/:id" => "web_app_configs#update", as: "patch_web_app_config_redirect"
put "/web_app_config/:id" => "web_app_configs#update", as: "put_web_app_config_redirect"
patch "/users/:id" => "users#update", as: :patch_users_redirect
put "/users/:id" => "users#update", as: :put_users_redirect
put "/users/:id" => "users#update", as: :put_users_redirect
patch "/webcam_feed/:id" => "webcam_feeds#update", as: :patch_webcam_feed_redirect
put "/webcam_feed/:id" => "webcam_feeds#update", as: :put_webcam_feed_redirect
put "/webcam_feed/:id" => "webcam_feeds#update", as: :put_webcam_feed_redirect
put "/password_resets" => "password_resets#update", as: :whatever
get "/storage_auth" => "images#storage_auth", as: :storage_auth
post "/export_data" => "devices#dump", as: :dump_device
put "/password_resets" => "password_resets#update", as: :whatever
get "/storage_auth" => "images#storage_auth", as: :storage_auth
post "/export_data" => "devices#dump", as: :dump_device
end
devise_for :users
@ -99,15 +100,15 @@ FarmBot::Application.routes.draw do
# =======================================================================
# NON-API (USER FACING) URLS:
# =======================================================================
get "/" => "dashboard#front_page", as: :front_page
get "/" => "dashboard#front_page", as: :front_page
get "/app" => "dashboard#main_app", as: :dashboard
get "/app/controls" => "dashboard#main_app", as: :app_landing_page
get "/app" => "dashboard#main_app", as: :dashboard
get "/app/controls" => "dashboard#main_app", as: :app_landing_page
match "/app/*path", to: "dashboard#main_app", via: :all, constraints: { format: "html" }
get "/tos_update" => "dashboard#tos_update", as: :tos_update
get "/password_reset/*token" => "dashboard#password_reset", as: :password_reset
get "/verify/:token" => "dashboard#confirmation_page", as: :confirmation_page
post "/csp_reports" => "dashboard#csp_reports", as: :csp_report
post "/direct_upload" => "dashboard#direct_upload", as: :direct_upload
get "/tos_update" => "dashboard#tos_update", as: :tos_update
get "/password_reset/*token" => "dashboard#password_reset", as: :password_reset
get "/verify/:token" => "dashboard#confirmation_page", as: :confirmation_page
post "/csp_reports" => "dashboard#csp_reports", as: :csp_report
post "/direct_upload" => "dashboard#direct_upload", as: :direct_upload
end

View File

@ -0,0 +1,11 @@
class CreateEnigmas < ActiveRecord::Migration[5.2]
def change
create_table :enigmas do |t|
t.string :problem_tag, null: false
t.integer :priority, default: 100, null: false
t.string :uuid, null: false
t.references :device, foreign_key: true, null: false
t.timestamps
end
end
end

View File

@ -272,6 +272,40 @@ CREATE SEQUENCE public.edge_nodes_id_seq
ALTER SEQUENCE public.edge_nodes_id_seq OWNED BY public.edge_nodes.id;
--
-- Name: enigmas; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.enigmas (
id bigint NOT NULL,
problem_tag character varying NOT NULL,
priority integer DEFAULT 100 NOT NULL,
uuid character varying NOT NULL,
device_id bigint NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
--
-- Name: enigmas_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.enigmas_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: enigmas_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.enigmas_id_seq OWNED BY public.enigmas.id;
--
-- Name: farm_events; Type: TABLE; Schema: public; Owner: -
--
@ -1571,6 +1605,13 @@ ALTER TABLE ONLY public.diagnostic_dumps ALTER COLUMN id SET DEFAULT nextval('pu
ALTER TABLE ONLY public.edge_nodes ALTER COLUMN id SET DEFAULT nextval('public.edge_nodes_id_seq'::regclass);
--
-- Name: enigmas id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.enigmas ALTER COLUMN id SET DEFAULT nextval('public.enigmas_id_seq'::regclass);
--
-- Name: farm_events id; Type: DEFAULT; Schema: public; Owner: -
--
@ -1837,6 +1878,14 @@ ALTER TABLE ONLY public.edge_nodes
ADD CONSTRAINT edge_nodes_pkey PRIMARY KEY (id);
--
-- Name: enigmas enigmas_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.enigmas
ADD CONSTRAINT enigmas_pkey PRIMARY KEY (id);
--
-- Name: farm_events farm_events_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@ -2148,6 +2197,13 @@ CREATE INDEX index_edge_nodes_on_primary_node_id ON public.edge_nodes USING btre
CREATE INDEX index_edge_nodes_on_sequence_id ON public.edge_nodes USING btree (sequence_id);
--
-- Name: index_enigmas_on_device_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_enigmas_on_device_id ON public.enigmas USING btree (device_id);
--
-- Name: index_farm_events_on_device_id; Type: INDEX; Schema: public; Owner: -
--
@ -2604,6 +2660,14 @@ ALTER TABLE ONLY public.sensor_readings
ADD CONSTRAINT fk_rails_04297fb1ff FOREIGN KEY (device_id) REFERENCES public.devices(id);
--
-- Name: enigmas fk_rails_10ebd17bff; Type: FK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.enigmas
ADD CONSTRAINT fk_rails_10ebd17bff FOREIGN KEY (device_id) REFERENCES public.devices(id);
--
-- Name: pin_bindings fk_rails_1f1c3b6979; Type: FK CONSTRAINT; Schema: public; Owner: -
--
@ -2821,6 +2885,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190209133811'),
('20190212215842'),
('20190307205648'),
('20190401212119');
('20190401212119'),
('20190411152319');

View File

@ -0,0 +1,23 @@
require "spec_helper"
describe Api::EnigmasController do
include Devise::Test::ControllerHelpers
let(:user) { FactoryBot.create(:user) }
it "lists all Enigmas for a user" do
sign_in user
e = FactoryBot.create(:enigma, device: user.device)
process :index, method: :get
expect(response.status).to eq(200)
expect(json.length).to eq(1)
expect(json.first.fetch(:problem_tag)).to eq(e.problem_tag)
end
it "lists all Enigmas for a user" do
sign_in user
FactoryBot.create(:enigma, device: user.device)
process :destroy, method: :get, params: { id: user.device.enigmas.last.id }
expect(response.status).to eq(200)
expect(user.device.reload.enigmas.count).to eq(0)
end
end

View File

@ -0,0 +1,8 @@
FactoryBot.define do
factory :enigma do
problem_tag { "api.seed_data.missing" }
priority { 100 }
uuid { SecureRandom.uuid }
device
end
end