-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.rb
241 lines (199 loc) · 5.52 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
require 'sinatra'
require 'sinatra/content_for'
require 'tilt/erubis'
require_relative "database_persistence"
configure do
enable :sessions
set :session_secret, 'secret'
set :erb, :escape_html => true
end
configure(:development) do
require 'sinatra/reloader'
also_reload "database_persistence.rb"
end
before do
@storage = DatabasePersistence.new(logger)
end
after do
@storage.disconnect
end
helpers do
def completed?(list)
list[:todos_count] >= 1 && list[:todos_remaining_count] == 0
end
def list_class(list)
'complete' if completed?(list)
end
def no_todos(todos)
'complete' if todos.count == 0
end
def sort_lists(lists, &block)
incomplete = {}
complete = {}
lists.each do |list|
if completed?(list)
complete[list] = list[:id]
else
incomplete[list] = list[:id]
end
end
incomplete.each(&block)
complete.each(&block)
end
def sort_todos(todos, &block)
incomplete = {}
complete = {}
todos.each do |todo|
if todo[:completed]
complete[todo] = todo[:id]
else
incomplete[todo] = todo[:id]
end
end
incomplete.each(&block)
complete.each(&block)
end
end
# Return an error if list name is invalid. Return nil if valid.
def check_for_error(list_name)
error = if !(1..100).cover?(list_name.size)
'The list name must be between 1 and 100 characters.'
elsif @storage.all_lists.any? { |list| list[:name] == list_name }
'The list name must be unique.'
end
error
end
# Return an error if todo name is invalid. Return nil if valid.
def check_todo_error(todo)
unless (1..100).cover?(todo.length)
'Todo must be between 1 and 100 characters.'
end
end
get '/' do
redirect '/lists'
end
# Verifies a valid list id is given
def load_list(id)
list = @storage.find_list(id)
return list if list
session[:error] = "The specified list was not found."
redirect '/lists'
end
# GET /lists -> view all lists
# GET /lists/new -> new list form
# POST /lists -> create new list
# GET /lists/1 -> view a single list
# GET /lists/1/edit -> edit existing todo list name
# POST /lists/1 -> rename an existing list
# POST /lists/1/destroy -> delete an existing list
# POST /lists/1/todos -> add a todo to a list
# POST /lists/1/todos/1/destroy -> delete a todo from a list
# POST /lists/1/todos/1 -> update status of todo
# POST /lists/1/complete_all -> complete all todos in a list
# View all the lists
get '/lists' do
@lists = @storage.all_lists
erb :lists, layout: :layout
end
# Render the new list form
get '/lists/new' do
erb :new_list, layout: :layout
end
# Create a new list
post '/lists' do
list_name = params[:list_name].strip
error = check_for_error(list_name)
if error
session[:error] = error
erb :new_list, layout: :layout
else
@storage.create_new_list(list_name)
session[:success] = 'The list has been successfully created.'
redirect '/lists'
end
end
# View a single list
get '/lists/:number' do
@num = params[:number].to_i
@list = load_list(@num)
@todos = @storage.find_todos_for_list(@num)
erb :list, layout: :layout
end
# Edit existing todo list name
get '/lists/:number/edit' do
@num = params[:number].to_i
@list = load_list(@num)
erb :edit_list
end
# Rename an existing list
post '/lists/:number' do
list_name = params[:list_name].strip
@num = params[:number].to_i
@list = load_list(@num)
error = check_for_error(list_name)
if error
session[:error] = error
erb :edit_list, layout: :layout
else
@storage.update_list_name(@num, list_name)
session[:success] = 'The list has been updated.'
redirect "/lists/#{@num}"
end
end
# Delete an existing list
post '/lists/:number/destory' do
num = params[:number].to_i
@storage.delete_list(num)
if env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
'/lists'
else
session[:success] = "The list has been deleted."
redirect "/lists"
end
end
# Add a todo to a list
post '/lists/:list_num/todos' do
@list_id = params[:list_num].to_i
todo_name = params[:todo].strip
@list = load_list(@list_id)
@todos = @list[:todos]
error = check_todo_error(todo_name)
if error
session[:error] = error
erb :list, layout: :layout
else
@storage.create_new_todo(@list_id, todo_name)
session[:success] = 'The todo was added.'
redirect "/lists/#{@list_id}"
end
end
# Delete a todo from a list
post '/lists/:list_num/todos/:todo_num/destroy' do
todo_num = params[:todo_num].to_i
list_num = params[:list_num].to_i
list = load_list(list_num)
@storage.delete_todo_from_list(list_num, todo_num)
if env['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
status 204
else
session[:success] = 'The todo has been deleted.'
redirect "/lists/#{list_num}"
end
end
# Update status of todo
post '/lists/:list_num/todos/:todo_num' do
list_num = params[:list_num].to_i
todo_num = params[:todo_num].to_i
list = load_list(list_num)
is_completed = params[:completed] == 'true'
@storage.update_todo_status(list_num, todo_num, is_completed)
session[:success] = 'The todo has been updated.'
redirect "/lists/#{list_num}"
end
# Mark all todos on a list complete
post '/lists/:num/complete_all' do
list_id = params[:num].to_i
@storage.mark_all_todos_complete(list_id)
session[:success] = 'All todos have been completed.'
redirect "/lists/#{list_id}"
end