forked from saasbook/sinatra_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.rb
70 lines (63 loc) · 1.79 KB
/
final.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
=begin
Created on Feb 2nd, 2016
A toy Sinatra app to demostrate the basic concept of MVC, RESTful Routes and CRUD.
Run ``bundle install`` to make sure you have necessary gems installed.
TO run the script, type ``ruby final.rb`` in command line.
@author: hezheng.yin
=end
# load libraries we need
require 'sinatra'
require 'json'
require './todo'
# display all todos
get '/todos' do
content_type :json
Todo.all.to_json
end
# show a specific todo
get '/todos/:id' do
content_type :json
todo = Todo.find_by_id(params[:id])
if todo
return {description: todo.description}.to_json
else
return {msg: "error: specified todo not found"}.to_json
end
end
# create a new todo
# goal: if we receive non-empty description, render json with msg set to "create success"
# otherwise render json with msg set to "error: description can't be blank"
post '/todos' do
content_type :json
todo = Todo.new(description: params[:description])
if todo.save
return {msg: "create success"}.to_json
else
return {msg: todo.errors}.to_json
end
end
# update a todo
# return: if todo with specified id exist and description non-empty, render json with msg set to "update success"
# otherwise render json with msg set to "upate failure"
put '/todos/:id' do
content_type :json
todo = Todo.find(params[:id])
if todo.update_attribute(:description, params[:description])
return {msg: "update success"}.to_json
else
return {msg: todo.errors}.to_json
end
end
# delete a todo
# return: if todo with specified id exist, render json with msg set to "delete success"
# otherwise render json with msg set to "delete failure"
delete '/todos/:id' do
content_type :json
todo = Todo.find(params[:id])
if todo
todo.destroy
return {msg: "delete success"}.to_json
else
return {msg: "delete failure"}.to_json
end
end