This soon-to-be-depreacted controller exposes a series of functions to operate with different models using JSON.
It was created before proper REST controllers were implemented for the majority of the models and as such it duplicates funcionality that should be implemented in them.
This controller will be removed in Dradis 3.0
- category_create
- category_delete
- category_update
- node_create
- node_delete
- node_update
- nodes
- note_create
- note_delete
- note_update
Create a new Category with the given :name
[ show source ]
# File app/controllers/json_controller.rb, line 103
103: def category_create
104: category = Category.new(:name => params[:name])
105: category.save
106: render :text => category.id
107: end
[ show source ]
# File app/controllers/json_controller.rb, line 122
122: def category_delete
123: begin
124: cat_id = params[:id].to_i
125: p cat_id
126: if (cat_id == 1)
127: render :text => 'Default category can\'t be removed.'
128: return
129: end
130: # TODO: this should be done at model level
131: associated_notes = Category.count_by_sql( "SELECT COUNT(*) FROM notes where \"category_id\" = #{cat_id}" )
132: p associated_notes
133: if (associated_notes > 0)
134: render :text => 'There are notes associated with this category!'
135: return
136: end
137: category = Category.find(cat_id)
138: category.destroy
139: render :text => 'noerror'
140: rescue e
141: p e
142: render :text => 'category not found'
143: return
144: end
145: end
Rename a Category
[ show source ]
# File app/controllers/json_controller.rb, line 110
110: def category_update
111: begin
112: category = Category.find(params[:id].to_i)
113: rescue
114: render :text => 'category not found'
115: return
116: end
117: category.update_attributes({ :name => params[:name] })
118: render :text => 'noerror'
119: end
Create a new Node from its :label and :parent_id
[ show source ]
# File app/controllers/json_controller.rb, line 65
65: def node_create
66: node = Node.new({
67: :label => params[:label],
68: :parent_id => params[:parent_id]
69: })
70: node.save
71: render :text => node.id
72: end
Delete a Node given its :id
[ show source ]
# File app/controllers/json_controller.rb, line 91
91: def node_delete
92: begin
93: node = Node.find(params[:id].to_i)
94: node.destroy
95: render :text => 'noerror'
96: rescue
97: render :text => 'node not found'
98: return
99: end
100: end
Update the attributes of a Node from the values submitted in a non-standard POST request
[ show source ]
# File app/controllers/json_controller.rb, line 76
76: def node_update
77: begin
78: node = Node.find(params[:id].to_i)
79: rescue
80: render :text => 'node not found'
81: return
82: end
83: node.update_attributes({
84: :label => params[:label],
85: :parent_id => params[:parent_id]
86: })
87: render :text => 'noerror'
88: end
Return a list of Node objects in JSON format.
[ show source ]
# File app/controllers/json_controller.rb, line 13
13: def nodes
14: parent_id = params[:node] == 'root-node' ? nil : params[:node].to_i
15: nodes = Node.find(:all, :conditions => {:parent_id => parent_id})
16: render :text => nodes.to_json
17: end
Create a new Note using the parameters submited via a non-standard POST request
[ show source ]
# File app/controllers/json_controller.rb, line 21
21: def note_create
22: # TODO: validation!!
23: note = Note.new(
24: :text => params[:text],
25: :author => params[:author],
26: :node_id => params[:node_id],
27: :category_id => params[:category_id]
28: )
29: note.save
30: render :text => note.id
31: end
Delete a Note given its :id
[ show source ]
# File app/controllers/json_controller.rb, line 52
52: def note_delete
53: begin
54: note = Note.find(params[:id].to_i)
55: note.destroy
56: render :text => 'noerror'
57: rescue
58: render :text => 'note not found'
59: return
60: end
61: end
Update a Note using the parameters submited via a non-standard POST request
[ show source ]
# File app/controllers/json_controller.rb, line 34
34: def note_update
35: begin
36: note = Note.find(params[:id].to_i)
37: rescue
38: render :text => 'note not found'
39: return
40: end
41:
42: note.update_attributes({
43: :text => params[:text],
44: :author => params[:author],
45: :node_id => params[:node_id],
46: :category_id => params[:category_id]
47: })
48: render :text => 'noerror'
49: end