-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.rb
54 lines (46 loc) · 1.08 KB
/
web.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
require 'rubygems'
require 'sinatra'
require 'data_mapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/todo_list.db")
class Item
include DataMapper::Resource
property :id, Serial
property :content, Text, :required => true
property :done, Boolean, :required => true, :default => false
property :created, DateTime
end
DataMapper.finalize.auto_upgrade!
get '/?' do
@items = Item.all(:order => :created.desc)
redirect '/new' if @items.empty?
erb :index
end
get '/new/?' do
@title = "Add todo item"
erb :new
end
post '/new/?' do
Item.create(:content => params[:content], :created => Time.now)
redirect '/'
end
post '/done/?' do
item = Item.first(:id => params[:id])
item.done = !item.done
item.save
content_type 'application/json'
value = item.done ? 'done' : 'not done'
{ :id => params[:id], :status => value }.to_json
end
get '/delete/:id/?' do
@item = Item.first(:id => params[:id])
erb :delete
end
post '/delete/:id/?' do
if params.has_key?("ok")
item = Item.first(:id => params[:id])
item.destroy
redirect '/'
else
redirect '/'
end
end