-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
292 lines (223 loc) · 7.26 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/flash'
require_relative 'lib/listing_repository'
require_relative 'lib/booking_repository'
require_relative 'lib/user_repository'
require_relative 'lib/database_connection'
DatabaseConnection.connect('bnb_database_test')
class Application < Sinatra::Base
enable :sessions
register Sinatra::Flash
configure :development do
register Sinatra::Reloader
end
get '/' do
return erb :index, layout: nil
end
get '/listings' do
if session[:user_id] == nil
return redirect '/'
else
user_repo = UserRepository.new
@user = user_repo.find(session[:user_id])
repo = ListingRepository.new
@listings = repo.all
return erb(:listings)
end
end
get '/listings/new' do
if session[:user_id] == nil
return redirect '/'
else
user_repo = UserRepository.new
@user = user_repo.find(session[:user_id])
return erb(:new_listing)
end
end
get '/listings/:id' do
if session[:user_id] == nil
return redirect '/'
else
user_repo = UserRepository.new
@user = user_repo.find(session[:user_id])
repo = ListingRepository.new
@listing = repo.find(params[:id])
listing_start_date = @listing.start_date
@first_available_day = compare_today_to(listing_start_date)
return erb(:listing)
end
end
post '/listings/:id/booking' do
if session[:user_id] == nil
return redirect '/'
else
if invalid_request_parameters(params[:date])
flash[:error] = "Please enter a valid date"
redirect "/listings/#{params[:id]}"
end
repo = BookingRepository.new
# checks the existing bookings and if a confirmed booking is found on the same date, it fails to book and flashes a message.
existing_bookings = repo.find_by_listing(params[:id])
existing_bookings.each do |booking|
if booking.date == params[:date] && booking.confirmed == true
flash[:error] = "That date is already booked. Please select another"
redirect "/listings/#{params[:id]}"
end
end
end
@new_booking = Booking.new
@new_booking.date = params[:date]
@new_booking.confirmed = false
@new_booking.listing_id = params[:id]
@new_booking.user_id = session[:user_id]
repo.create(@new_booking)
user_repo = UserRepository.new
@user = user_repo.find(session[:user_id])
@listing_repo = ListingRepository.new
@bookings = repo.find_by_user_id(session[:user_id])
return erb(:your_bookings)
end
get '/your_bookings' do
repo = BookingRepository.new
user_repo = UserRepository.new
@user = user_repo.find(session[:user_id])
@listing_repo = ListingRepository.new
@bookings = repo.find_by_user_id(session[:user_id])
return erb(:your_bookings)
end
post '/listings/new' do
params_array = [params[:name], params[:price], params[:description]]
params_array.each do |param|
if invalid_new_listing_parameters(param)
flash[:error] = "Please fill all fields with valid inputs"
redirect "/listings/new"
end
end
repo = ListingRepository.new
new_listing = Listing.new
new_listing.name = params[:name]
new_listing.price = params[:price]
new_listing.description = params[:description]
new_listing.start_date = params[:start_date]
new_listing.end_date = params[:end_date]
new_listing.user_id = session[:user_id]
if dates_checker(new_listing)
status 400
flash[:date_error] = 'The end date must be after the start date.'
return redirect '/listings/new'
end
repo.create(new_listing)
status 200
flash[:listing_success] = 'Listing successfully created!'
return redirect '/listings'
end
get '/requests' do
if session[:user_id] == nil
return redirect '/'
else
user_repo = UserRepository.new
@user = user_repo.find(session[:user_id])
booking_repo = BookingRepository.new
@user_repo = UserRepository.new
@listing_repo = ListingRepository.new
# we're creating a bookings array. This is storing a
# booking based on the session id of the current user
listings = @listing_repo.find_by_user_id(session[:user_id])
bookings = []
listings.each do |listing|
bookings += booking_repo.find_by_listing(listing.id)
end
@unconfirmed_bookings = bookings.select do |booking|
!booking.confirmed
end
@confirmed_bookings = bookings.select do |booking|
booking.confirmed
end
erb(:requests)
end
end
post '/requests/confirm/:id' do
repo = BookingRepository.new
booking = repo.find(params[:id])
repo.confirm_booking(booking)
redirect('/requests')
end
post '/signup' do
listing_repo = ListingRepository.new
@listings = listing_repo.all
@user = User.new
@user.email = params[:email]
@user.password = params[:password]
repo = UserRepository.new
if @user.email.empty? || @user.password.empty?
flash[:signup_error] = "Please enter valid email and password"
redirect "/"
elsif @user.email.empty? && @user.password.empty?
flash[:signup_error] = "Please enter valid email and password"
redirect "/"
end
repo.create(@user)
flash[:success] = "Sign up success!"
return redirect "/"
end
post '/login' do
listing_repo = ListingRepository.new
@listings = listing_repo.all
email = params[:email]
password = params[:password]
if email.empty? || password.empty?
flash[:login_error] = "Please enter valid email and password"
redirect "/"
elsif email.empty? && password.empty?
flash[:login_error] = "Please enter valid email and password"
redirect "/"
end
repo = UserRepository.new
user = repo.find_by_email(email)
sign_in_status = repo.password_correct?(user.password, password)
if sign_in_status == true
session[:user_id] = user.id
@user = repo.find(user.id)
return erb(:listings)
else
return erb :signup_fail, layout: nil
end
end
post '/logout' do
session.clear
redirect '/'
end
private
def invalid_request_parameters(params)
# Are the params nil?
return true if params == nil
# Are they empty strings?
return true if params == ""
return true if params.include? '<script>'
return true if Date.parse(params) == false
return false
end
def invalid_new_listing_parameters(params)
# Are the params nil?
return true if params == nil
# Are they empty strings?
return true if params == ""
return true if params.include? '<script>'
return true if params.match /^\A\d{4}-\d{2}-\d{2}\z/
return false
end
def compare_today_to(listing_start_date)
today = Time.now.to_date.to_s
listing_start_date < today ? today : listing_start_date
end
def dates_checker(new_listing)
start_date_parts = new_listing.start_date.split('-')
start_date_to_check = Time.new(*start_date_parts)
end_date_parts = new_listing.end_date.split('-')
end_date_to_check = Time.new(*end_date_parts)
if start_date_to_check > end_date_to_check
return true
end
end
end