Permalink
Browse files

Added RESTful resourse CRUD + list

  • Loading branch information...
tonymadbrain committed Nov 24, 2015
1 parent 8cc1a84 commit c4d1cdf30bdcc3d5d67d8d0540d4a6c9542c0555
Showing with 53 additions and 1 deletion.
  1. +52 −0 app/api/notes.rb
  2. +1 −1 app/models/note.rb
View
@@ -5,10 +5,62 @@ class Notes < Grape::API
resource 'notes' do
desc 'Test endpoint'
get '/test' do
{ data: "TEST" }
end
desc 'List all notes.'
get '/' do
Note.all
end
desc 'Create a note.'
params do
requires :author, type: String, desc: 'Author'
requires :title, type: String, desc: 'Title'
requires :content, type: String, desc: 'Body'
optional :summary, type: String, desc: 'Summary'
optional :private, type: Boolean, desc: 'Private'
optional :valuation, type: Integer, desc: 'Valuation'
end
post '/' do
Note.create params
end
desc 'Read a note.'
params do
requires :id, type: String, desc: 'Note id'
end
get '/:id' do
Note.find params[:id]
end
desc 'Update a note.'
params do
requires :id, type: String, desc: 'Note id'
requires :author, type: String, desc: 'Author'
requires :title, type: String, desc: 'Title'
requires :content, type: String, desc: 'Body'
optional :summary, type: String, desc: 'Summary'
optional :private, type: Boolean, desc: 'Private'
optional :valuation, type: Integer, desc: 'Valuation'
end
put '/:id' do
if Note.find(params[:id]).update(params.reject{|k,v| k=='id'})
Note.find params[:id]
else
false
end
end
desc 'Delete a note.'
params do
requires :id, type: String, desc: 'Note id'
end
delete '/:id' do
Note.find(params[:id]).destroy
end
end
add_swagger_documentation \
View
@@ -1,3 +1,3 @@
class Note < ActiveRecord::Base
validates :author, :title, :body, presence: true
validates :author, :title, :content, presence: true
end

0 comments on commit c4d1cdf

Please sign in to comment.