-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
236 lines (160 loc) · 3.85 KB
/
app.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
#Require Dependencies
require 'bundler/setup'
require 'pry'
require "sinatra"
require "sinatra/activerecord"
require "sinatra/flash"
require "will_paginate"
require 'will_paginate/active_record'
require "will_paginate-bootstrap"
require './lib/auth'
#Require Models
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file }
#Configure Sinatra
set :database, {adapter: "sqlite3", database: "./db/db.sqlite3" , encoding: "utf8"}
set :session_secret, 'b51bffb91a03fa8a07bf9158e367bfef'
enable :sessions
#Controllers
get '/' do
protected!
#Get the current search
search = params["search"]
status = params["status"]
#Get the list of todos
@todos = Todo.paginate( :page => params[:page] , :per_page => 10 )
#Filter by logged in user
@todos = @todos.where( user: authenticated_user() )
#Search todos
if search && !search.empty?
@todos = @todos.where( "content LIKE :search" , search: "%#{search}%" )
end
#Filter by status
if status && !status.empty?
@todos = @todos.where( "status = :status" , status: status )
end
#Order Todos
@todos = @todos.order( "status ASC , priority DESC" )
#Render the view
erb :list
end
post '/new' do
protected!
#Save new todo item
begin
#Save new todo item
todo = Todo.new
todo.content = params["content"]
todo.priority = params["priority"]
todo.status = 0
todo.user = authenticated_user
todo.save!
#Save success message to the session
flash[:success] = "Successfully created new Todo Item"
rescue ActiveRecord::RecordInvalid
#Save success message to the session
flash[:danger] = "There is an error with your data"
end
#Redirect to list
redirect to("/")
end
get '/finish/:id' do
protected!
#Get the id from the request
id = params["id"]
#Find the todo item and change status
todo = Todo.find id
#Check that the todo belongs to the user
ensure_user todo.user
#Save
todo.status = 1
todo.save!
#Redirect to list
redirect to("/")
end
get '/unfinish/:id' do
protected!
#Get the id from the request
id = params["id"]
#Find the todo item and change status
todo = Todo.find id
#Check that the todo belongs to the user
ensure_user todo.user
#Save
todo.status = 0
todo.save!
#Redirect to list
redirect to("/")
end
get '/delete/:id' do
protected!
#Get the id from the request
id = params["id"]
#Find the todo item
todo = Todo.find id
#Make sure the todo belongs to the user
ensure_user todo.user
#Delete the todo
todo.delete
#Save success message to the session
flash[:success] = "Successfully deleted item"
#Redirect to list
redirect to("/")
end
get '/edit/:id' do
protected!
#Get the id from the request
id = params["id"]
#Find the todo item
@todo = Todo.find id
#Check that the todo belongs to the user
ensure_user @todo.user
#Render the view
erb :edit
end
post '/edit/:id' do
protected!
#Get the id from the request
id = params["id"]
#Find the todo item
@todo = Todo.find id
#Check that the todo belongs to the user
ensure_user @todo.user
#Update its content
begin
#Update its content
@todo.content = params["content"]
@todo.priority = params["priority"]
@todo.save!
#Redirect to list
redirect "/"
rescue ActiveRecord::RecordInvalid
#Save success message to the session
flash[:danger] = "There is an error with your data"
#Redirect to list
redirect to("/edit/" + id)
end
end
get '/login' do
erb :login
end
post '/login' do
if authenticate( params['username'] , params['password'] )
redirect to("/")
else
@error = "Invalid Username/Password"
end
erb :login
end
get '/logout' do
logout!
end
#Error Handling
not_found do
'Not Found'
end
error ActiveRecord::RecordNotFound do
"Not Found"
end
error 403 do
'Access forbidden'
end