-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.rb
214 lines (172 loc) · 4.58 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
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
helpers do
def list_complete?(list)
todos_count(list) > 0 && todos_remaining_count(list) == 0
end
def list_class(list)
"complete" if list_complete?(list)
end
def todos_count(list)
list[:todos].size
end
def todos_remaining_count(list)
list[:todos].count { |todo| !todo[:completed] }
end
def sort_lists(lists, &block)
complete_lists, incomplete_lists = lists.partition { |list| list_complete?(list) }
incomplete_lists.each(&block)
complete_lists.each(&block)
end
def sort_todos(todos, &block)
complete_todos, incomplete_todos = todos.partition { |todo| todo[:completed] }
incomplete_todos.each(&block)
complete_todos.each(&block)
end
end
def load_list(id)
list = @storage.find_list(id)
return list if list
session[:error] = "The specified list was not found."
redirect "/lists"
end
# Return an error message if the name is invalid. Return nil if name is valid.
def error_for_list_name(name)
if !(1..100).cover? name.size
"List name must be between 1 and 100 characters."
elsif @storage.all_lists.any? { |list| list[:name] == name }
"List name must be unique."
end
end
# Return an error message if the name is invalid. Return nil if name is valid.
def error_for_todo(name)
if !(1..100).cover? name.size
"Todo must be between 1 and 100 characters."
end
end
before do
@storage = DatabasePersistence.new(logger)
end
after do
@storage.disconnect
end
get "/" do
redirect "/lists"
end
# View list of 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 = error_for_list_name(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 created."
redirect "/lists"
end
end
# View a single todo list
get "/lists/:id" do
@list_id = params[:id].to_i
@list = load_list(@list_id)
erb :list, layout: :layout
end
# Edit an existing todo list
get "/lists/:id/edit" do
id = params[:id].to_i
@list = load_list(id)
erb :edit_list, layout: :layout
end
# Update an existing todo list
post "/lists/:id" do
list_name = params[:list_name].strip
id = params[:id].to_i
@list = load_list(id)
error = error_for_list_name(list_name)
if error
session[:error] = error
erb :edit_list, layout: :layout
else
@storage.update_list_name(id, list_name)
session[:success] = "The list has been updated."
redirect "/lists/#{id}"
end
end
# Delete a todo list
post "/lists/:id/destroy" do
id = params[:id].to_i
@storage.delete_list(id)
session[:success] = "The list has been deleted."
if env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
"/lists"
else
redirect "/lists"
end
end
# Add a new todo to a list
post "/lists/:list_id/todos" do
@list_id = params[:list_id].to_i
@list = load_list(@list_id)
text = params[:todo].strip
error = error_for_todo(text)
if error
session[:error] = error
erb :list, layout: :layout
else
@storage.create_new_todo(@list_id, text)
session[:success] = "The todo was added."
redirect "/lists/#{@list_id}"
end
end
# Delete a todo from a list
post "/lists/:list_id/todos/:id/destroy" do
@list_id = params[:list_id].to_i
@list = load_list(@list_id)
todo_id = params[:id].to_i
@storage.delete_todo_from_list(@list_id, todo_id)
if env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
status 204
else
session[:success] = "The todo has been deleted."
redirect "/lists/#{@list_id}"
end
end
# Update the status of a todo
post "/lists/:list_id/todos/:id" do
@list_id = params[:list_id].to_i
@list = load_list(@list_id)
todo_id = params[:id].to_i
is_completed = params[:completed] == "true"
@storage.update_todo_status(@list_id, todo_id, is_completed)
session[:success] = "The todo has been updated."
redirect "/lists/#{@list_id}"
end
# Mark all todos as complete for a list
post "/lists/:id/complete_all" do
@list_id = params[:id].to_i
@list = load_list(@list_id)
@storage.mark_all_todos_as_completed(@list_id)
session[:success] = "All todos have been completed."
redirect "/lists/#{@list_id}"
end