-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.rb
190 lines (163 loc) · 3.94 KB
/
todo.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env ruby
=begin
Ok, I have a version that does Sinatra and datamapper. Can I keep using Sinatra? I
like how lightweight it was
=end
require 'sequel'
require 'haml'
require 'active_support/all'
require 'sinatra'
$curday = Date.today
duration = {}
DB = Sequel.connect('sqlite://recall.db')
unless DB.table_exists?(:notes)
DB.create_table :notes do
primary_key :id
String :content, null: false
String :comment
String :status, default: 'new'
Date :created_at
Date :task_date
DateTime :updated_at
DateTime :completed_at
Integer :duration, default: 0
TrueClass :priority, default: false
TrueClass :complete, default: false
TrueClass :active, default: false
TrueClass :repeater, default: false
TrueClass :longterm, default: false
end
end
#testing better format
class Note < Sequel::Model
end
def task_create(content, repeater, longterm=false)
Note.create(content: params[content],
repeater: params[repeater],
longterm: longterm,
created_at: $curday)
end
def edit(id,field)
@note = Note.where(id: params[:id]).first
@title = "Edit note ##{params[id]}"
erb :edit, :locals => {:field => field}
end
def save(id, field)
n = Note.where(id: params[:id]).first
if field == 'comment'
n.comment = params[:comment]
else
n.content = params[field]
end
n.updated_at = Time.now
n.save
redirect '/'
end
def check_repeaters()
if Date.today.day == 10
Note.find_or_create(:content=>"Rotate Mattress",:created_at=>Date.today)
end
if Date.today.month % 2 == 0 && Date.today.day == 6
Note.find_or_create(:content=>"Water snake plant", :created_at=>Date.today)
end
repeaters = Note.where(repeater: true).each do |rep|
if rep.created_at.cwday == Date.today.cwday and rep.complete == true and rep.created_at != Date.today
# make new note, same content
rep.repeater = false
rep.save
Note.create(repeater: true, complete: false, status: 'new', created_at: Date.today, content: rep.content)
end
end
end
get '/' do
check_repeaters()
notes = DB[:notes]
@notes = notes.all
@title = ' - CRC - '
erb :home, locals: {curday: $curday}
end
get '/:id/comment' do
edit(:id, 'comment')
end
put '/:id/content' do
save(:id, 'content')
end
put '/:id/comment' do
save(:id, 'comment')
end
get '/present' do
$curday = Date.today
redirect '/'
end
get '/nextday' do
$curday = $curday + 1.day
redirect '/'
end
get '/prevday' do
$curday = $curday - 1.day
redirect '/'
end
get '/longterm' do
@notes = DB[:notes].where(longterm: true).all
@title = ' - CRC - '
erb :longterm, locals: {curday: $curday}
end
get '/:id' do
edit(:id, 'content')
end
post '/datejump' do
if params[:newdate] == ''
else
$curday = Date.strptime("#{params[:newdate]}", '%m/%d/%Y') #fix date format
redirect '/'
end
end
post '/' do
task_create(:content, :repeater)
redirect '/'
end
post '/longterm' do
task_create(:content, false, true)
redirect '/longterm'
end
get '/:id/complete' do
n = Note.where(id: params[:id]).first
if n[:status] == 'new' || n[:status] == 'slack' || n[:active] == true
n[:status] = 'done'
n[:complete] = true
n[:active] = false
elsif n[:complete] == true
n[:status] = 'new'
n[:complete] = false
duration["#{n}"] = 0
end
n[:updated_at] = Time.now
# kludge to get completed notes to show on current day
n[:created_at] = Date.today unless n[:repeater]
n.save
redirect '/'
end
get '/:id/delete' do
Note.where(id: params[:id]).delete
redirect '/'
end
get '/:id/activate' do
n = Note.where(id: params[:id]).first
if (n.active == true || n.status == 'doing')
n.status = 'new'
n.active = false
elsif (n.status == 'new' || n.status == 'overdue') && $curday == Date.today
n.status = 'doing'
n.active = true
end
n.updated_at = Time.now
n.save
redirect '/'
end
get '/:id/slack' do
n = Note.where(id: params[:id]).first
n.status = :slack
n.updated_at = Time.now
n.save
edit(:id, 'comment')
end