/
controllers.rb
executable file
·221 lines (179 loc) · 5.08 KB
/
controllers.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
require 'sinatra'
require 'sinatra/json'
configure do
# load our database schema first
require_relative 'schema.rb'
# load our models
require_relative 'models.rb'
# tutor app relies on use of sessions
enable :sessions
end
# directory we will serve static files from
set :public_folder, Proc.new { File.join(root, "public") }
before do
# if there is an active session then
# load an instance of the current user
if session[:user_id] != nil
@user = User.where(:id => session[:user_id]).first
end
end
get '/' do
erb :index
end
get '/signup' do
@page_title = "Signup | Tutor Scheduler"
erb :signup
end
post '/signup' do
if(params[:type] == "tutor")
user = Tutor.create(
:name => params[:name],
:username => params[:username],
:college_id => params[:college_id],
:email => params[:email],
:created_at => DateTime.now,
:updated_at => DateTime.now
)
elsif (params[:type] == "student")
user = Student.create(
:name => params[:name],
:username => params[:username],
:college_id => params[:college_id],
:email => params[:email],
:created_at => DateTime.now,
:updated_at => DateTime.now
)
end
# update their password and save
user.password = params[:password]
user.save
# login the user
session[:user_id] = user.id
redirect to ('/appointments')
end
get '/login' do
erb :login
end
post '/login' do
# find the user by the email address
user = User.where(:email => params[:email]).first
if user and user.authenticate(params[:password])
# store the users session information
session[:user_id] = user.id
redirect to ('/appointments')
else
redirect to ('/login?incorrect_login')
end
end
get '/logout' do
session[:user_id] = nil
@user = nil
redirect to('/login')
end
get '/appointments/new' do
@page_title = "Create Appointment | Tutor Scheduler"
@tutors = @user.tutors
if @tutors.count == 0
redirect to('/tutors?tutors_needed=true')
end
erb :appointments_new
end
post '/appointments' do
if session[:user_id] and @user
# create a formatted when timestamp
w_date = Date.parse(params[:when_date])
w_time = Time.parse(params[:when_time])
# merge the two time zones together
w_merged = DateTime.new(w_date.year, w_date.month, w_date.day,
w_time.hour, w_time.min, w_time.sec, w_time.zone)
w_merged_to_s = w_merged.strftime("%Y-%m-%d %H:%M:%S")
# check to see if this tutor is
# already booked for this session
has_appointment = false
tutor = Tutor.where(:id => params[:tutor_id]).first
tutor.appointments.each do |appt|
when_to_s = appt.when.strftime("%Y-%m-%d %H:%M:%S")
puts "#{when_to_s} == #{w_merged_to_s}"
if when_to_s == w_merged_to_s
has_appointment = true
break
end
end
if has_appointment
return redirect to ('/appointments/new?schedule_conflict=true')
end
# create the appointment
appointment = Appointment.create(
:student_id => @user.id,
:tutor_id => params[:tutor_id],
:where => params[:where],
:when => w_merged
)
return redirect to ('/appointments')
end
end
get '/appointments' do
# print in reverse order as they were created
@appointments = @user.appointments.reverse
erb :appointments
end
post '/appointments/cancel' do
# delete the appointment
Appointment.where(:id => params[:appt_id]).first.delete
return json [:success => "true", :message => "Canceled this appointment"]
end
post '/appointments/attend' do
# mark the appointment as being attended
appt = Appointment.where(:id => params[:appt_id]).first
appt.student_attended = true
appt.save
return json [:success => "true", :message => "Marked this appointment as attended"]
end
get '/tutors' do
if @user and @user.type == "Student"
@tutors = @user.tutors
erb :tutors
else
redirect to ('/appointments?unauthorized_error=true')
end
end
get '/tutors/search' do
# if the user is authorized to
# access this page
if session[:user_id] and @user
# possible sql injection attack?
# since params[:query] is not parameterized
@tutors = Tutor.where(
Sequel.like(:name, "%#{params[:query]}%") |
Sequel.like(:email, "%#{params[:query]}%") |
Sequel.like(:username, "%#{params[:query]}%")
)
return json @tutors
end
end
post '/tutors/add' do
if session[:user_id] and @user
# find the tutor that is being requested
@tutor = Tutor.where(:id => params[:tutor_id]).first
if @user.type == "Student"
# save the student/tutor relationship
# @user.tutors << @tutor
# @user.save
# since sequel was giving me problems
# saving the relationship through model
# associations, I am directly inserting
# into my join table
users_users = DB[:users_users]
users_users.insert(:student_id => @user.id, :tutor_id => @tutor.id)
return json @user.tutors
end
end
end
get '/students' do
if @user and @user.type == "Tutor"
@students = @user.students
erb :students
else
redirect to ('/appointments?unauthorized_error=true')
end
end