forked from daz4126/Just-Do-It
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
57 lines (45 loc) · 1.1 KB
/
main.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
require 'sinatra'
require 'slim'
require 'data_mapper'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
class Task
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
property :completed_at, DateTime
belongs_to :list
end
class List
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
has n, :tasks, :constraint => :destroy
end
DataMapper.finalize
get('/styles.css'){ content_type 'text/css', :charset => 'utf-8' ; scss :styles }
get '/' do
@lists = List.all(:order => [:name])
slim :index
end
post '/:id' do
List.get(params[:id]).tasks.create params['task']
redirect '/'
end
delete '/task/:id' do
Task.get(params[:id]).destroy
redirect '/'
end
put '/task/:id' do
task = Task.get params[:id]
task.completed_at = task.completed_at.nil? ? Time.now : nil
task.save
redirect '/'
end
post '/new/list' do
List.create params['list']
redirect '/'
end
delete '/list/:id' do
List.get(params[:id]).destroy
redirect '/'
end