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,5 +1,5 @@
FarmBot::Application.routes.draw do
namespace :api, defaults: {format: :json}, constraints: { format: "json" } 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"
post "/rmq/resource" => "rmq_utils#resource_action", as: "rmq_resource"
@ -8,31 +8,32 @@ FarmBot::Application.routes.draw do
# Standard API Resources:
{
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],
sensors: [: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],
farmware_envs: [:create, :destroy, :index, :show, :update],
plant_templates: [:create, :destroy, :index, :update],
pin_bindings: [: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,],
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}
}.to_a.map { |(name, only)| resource name, only: only }
get "/corpus" => "corpuses#show", as: :api_corpus
resources(:points, except: []) { post :search, on: :collection }
@ -52,7 +53,7 @@ FarmBot::Application.routes.draw do
post :control_certificate, on: :collection
end
resources :saved_gardens, except: [ :show ] do
resources :saved_gardens, except: [:show] do
post :snapshot, on: :collection
post :apply, on: :member
patch :apply, on: :member

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