Skip to content

Commit

Permalink
Added RESTful resourse CRUD + list
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony Ryabov committed Nov 24, 2015
1 parent 8cc1a84 commit c4d1cdf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
52 changes: 52 additions & 0 deletions app/api/notes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion app/models/note.rb
Original file line number Diff line number Diff line change
@@ -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.