Skip to content

Commit

Permalink
FlowchartNode model and controller (#24)
Browse files Browse the repository at this point in the history
* [WIP]: show, update, and swap functions

* [WIP]: basic delete functionality and return json objects

* add soft delete in node, run rubocop

* flowchart node model tests

* Finish testing for show and update

* Updates to swap/delete and unit tests

* linting

* [WIP] FlowchartNode create controller method, some unit tests

* Tests for flowchartnode create controller method

* linting

* Fix duplicate primary key issue in tests

* Add comment around primary key sequence correction

* remove as_json from render in flowchartnode controller

* JSON parse body in flowchart node update

* Add validation and testing for flowchart node create

* Remove rescue blocks from flowchart node controller

* remove request body access

* add delete method on flowchart node model

* Add swap method to flowchart model

* lint
  • Loading branch information
danielpeng2 authored Dec 4, 2019
1 parent 43afebb commit 4f5d996
Show file tree
Hide file tree
Showing 5 changed files with 610 additions and 0 deletions.
52 changes: 52 additions & 0 deletions app/controllers/flowchart_node_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

class FlowchartNodeController < ApplicationController
def create
prev_id = params[:prev_id]
is_child = params[:is_child]

prev_node = FlowchartNode.find(prev_id)

new_node = FlowchartNode.new(params[:node].permit(:text, :header, :button_text, :next_question))
new_node.is_root = false
new_node.flowchart_id = prev_node.flowchart_id

ActiveRecord::Base.transaction do
new_node.save!
if is_child == 'true'
new_node.child_id = prev_node.child_id
prev_node.child_id = new_node.id
else
new_node.sibling_id = prev_node.sibling_id
prev_node.sibling_id = new_node.id
end
new_node.save!
prev_node.save!
end
render json: new_node
end

def show
node = FlowchartNode.find(params[:id])
render json: node
end

def update
flowchart_node = FlowchartNode.find(params[:id])
flowchart_node.update!(params[:node].permit(:text, :header, :button_text, :next_question))
render json: flowchart_node
end

def swap
node_a = FlowchartNode.find(params[:id_a])
res = node_a&.swap(params[:id_b])

render json: { new_a: res[:new_a], new_b: res[:new_b] }
end

def delete
delete_node = FlowchartNode.find(params[:id])
delete_node&.delete
render json: delete_node
end
end
62 changes: 62 additions & 0 deletions app/models/flowchart_node.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,66 @@
# frozen_string_literal: true

class FlowchartNode < ApplicationRecord
belongs_to :flowchart
validates :text, presence: true
validates :header, presence: true
validates :button_text, exclusion: { in: [''] }
validates :next_question, exclusion: { in: [''] }
validates :child_id, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true
validates :sibling_id, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, allow_nil: true
validates :is_root, null: false
validates :flowchart_id, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, null: false
validates :deleted, inclusion: { in: [true, false] }

def swap(swap_id)
node_b = FlowchartNode.find(swap_id)

self.text, node_b.text = node_b.text, text
self.header, node_b.header = node_b.header, header
self.button_text, node_b.button_text = node_b.button_text, button_text
self.next_question, node_b.next_question = node_b.next_question, next_question
self.is_root, node_b.is_root = node_b.is_root, is_root
self.child_id, node_b.child_id = node_b.child_id, child_id
ActiveRecord::Base.transaction do
save!
node_b.save!
end

{ new_a: self, new_b: node_b }
end

def delete
self.deleted = true

parent_node = FlowchartNode.find_by(child_id: id)
left_node = FlowchartNode.find_by(sibling_id: id)
child_node = FlowchartNode.find_by(id: child_id)

if !parent_node && !left_node
save!
elsif !parent_node
left_node.sibling_id = sibling_id
ActiveRecord::Base.transaction do
left_node.save!
save!
end
else
parent_node.child_id = sibling_id
ActiveRecord::Base.transaction do
parent_node.save!
save!
end
end
child_node&.child_delete
self
end

def child_delete
self.deleted = true
save!
sibling = FlowchartNode.find_by(id: sibling_id)
child = FlowchartNode.find_by(id: child_id)
sibling&.child_delete
child&.child_delete
end
end
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
Rails.application.routes.draw do
devise_for :users
get '/questions', to: 'questions#index'

get '/flowchart/:id', to: 'flowchart#serialized_flowchart_by_id'
get '/flowcharts', to: 'flowchart#all_flowcharts'
post '/flowchart', to: 'flowchart#create'
put '/flowchart/:id', to: 'flowchart#update'
delete '/flowchart/:id', to: 'flowchart#delete'

get '/flowchart_node/:id', to: 'flowchart_node#show'
post '/flowchart_node', to: 'flowchart_node#create'
put '/flowchart_node/swap', to: 'flowchart_node#swap'
put '/flowchart_node/:id', to: 'flowchart_node#update'
delete '/flowchart_node/:id', to: 'flowchart_node#delete'
end
Loading

0 comments on commit 4f5d996

Please sign in to comment.