-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
251 lines (201 loc) Β· 6.73 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
require 'sinatra/base'
require 'sinatra/reloader'
require_relative 'lib/user_params'
require_relative 'lib/booking_repository'
require_relative 'lib/space_repository'
require_relative 'lib/user_repository'
require_relative 'lib/host_params'
require_relative 'lib/space_params'
require_relative 'lib/host'
require_relative 'lib/host_repository'
require_relative 'lib/host_params'
require_relative 'lib/booking'
require_relative 'lib/database_connection'
DatabaseConnection.connect
class Application < Sinatra::Base
enable :sessions, :method_override
configure :development do
register Sinatra::Reloader
also_reload 'lib/booking_repository'
end
get '/' do
return erb(:index)
end
get '/user' do
return erb(:user_homepage)
end
get '/host' do
return erb(:host_homepage)
end
get '/user/login' do
return erb(:user_login)
end
post '/user/login' do
email = params[:user_email]
password = params[:user_password]
repo = UserRepository.new
status = repo.sign_in(email, password)
if status == nil
erb(:user_not_recognised)
elsif status == false
erb(:user_login_error)
else
@user = repo.find_by_email(email)
return erb(:user_successful_login)
end
end
get '/host/login' do
return erb(:host_login)
end
post '/host/login' do
email = params[:host_email]
password = params[:host_password]
repo = HostRepository.new
status = repo.sign_in(email, password)
if status == nil
erb(:host_not_recognised)
elsif status == false
erb(:host_login_error)
else
@host = repo.find_by_email(email)
return erb(:host_successful_login)
end
end
get '/spaces' do
repo = SpaceRepository.new
@spaces = repo.all
return erb(:view_spaces)
end
get '/space/:host_id' do
repo = HostRepository.new
@host = repo.find(params[:host_id])
return erb(:new_space)
end
post '/space/:hostid' do
@host_id = params[:hostid]
if empty_space_params?
return erb(:empty_space_params)
end
@checking_space_params = SpaceParams.new(params[:space_name], params[:space_description], params[:space_price])
if
@checking_space_params.invaild_space_params?
erb(:failed_space_creation)
else
create_space
return erb(:space_created)
end
end
post '/user/register' do
@checking_params = UserParams.new(params[:new_name], params[:new_username], params[:new_email], params[:new_password])
if empty_user_params?
erb(:empty_user_params)
elsif @checking_params.invaild_user_params?
erb(:failed_user_registration)
else
@new_user = create_user
return erb(:user_created)
end
end
post '/host/register' do
@checking_host_params = HostParams.new(params[:new_host_name], params[:new_host_username], params[:new_host_email], params[:new_host_password])
if empty_host_params?
erb(:empty_host_params)
elsif @checking_host_params.invaild_host_params?
erb(:failed_host_registration)
else
@new_host = create_host
return erb(:host_created)
end
end
get '/booking/:id' do
repo = SpaceRepository.new
@space = repo.find(params[:id])
return erb(:make_booking)
end
post '/booking/:space_id' do
space_repo = SpaceRepository.new
@space = space_repo.find(params[:space_id])
repo = BookingRepository.new
booking = Booking.new
booking.space_id = @space.id
booking.host_id = @space.host_id
booking.user_id = 1 #need to change
booking.start_date = params[:booking_start_date]
booking.end_date = params[:booking_end_date]
booking.confirmed = 'no'
repo.request(booking)
return erb(:booking_requested)
end
get '/bookings/:host' do
repo = HostRepository.new
@host = repo.find(params[:host])
booking_repo = BookingRepository.new
all_bookings = booking_repo.all
@unconfirmed_bookings = all_bookings.select do |booking|
booking.host_id == params[:host] && booking.confirmed == 'no'
end
@confirmed_bookings = all_bookings.select do |booking|
booking.host_id == params[:host] && booking.confirmed == 'yes'
end
return erb(:host_bookings)
end
get '/approve/:booking_id' do
repo = BookingRepository.new
@booking = repo.find(params[:booking_id])
repo.approve(@booking)
return erb(:booking_confirmation)
end
get '/decline/:bookingid' do
repo = BookingRepository.new
@booking = repo.find(params[:bookingid])
repo.delete(@booking.id)
return erb(:booking_deleted)
end
private
def empty_user_params?
params[:new_name] == "" || params[:new_name] == nil || params[:new_username] == "" || params[:new_username] == nil || params[:new_email] == "" || params[:new_email] == nil || params[:new_password] == "" || params[:new_password] == nil
end
def empty_host_params?
params[:new_host_name] == "" || params[:new_host_name] == nil || params[:new_host_username] == "" || params[:new_host_username] == nil || params[:new_host_email] == "" || params[:new_host_email] == nil || params[:new_host_password] == "" || params[:new_host_password] == nil
end
def empty_space_params?
params[:space_name] == "" || params[:space_name] == nil || params[:space_description] == "" || params[:space_description] == nil || params[:space_price] == "" || params[:space_price] == nil
end
def create_user
repo = UserRepository.new
new_user = User.new
new_user.id = (repo.all.length + 1) #look how we can remove this line so it automatically adds id
new_user.name = params[:new_name]
new_user.username = params[:new_username]
new_user.email = params[:new_email]
new_user.password = params[:new_password]
repo.create(new_user)
return new_user
end
def empty_host_params?
params[:new_host_name] == "" || params[:new_host_name] == nil || params[:new_host_username] == "" || params[:new_host_username] == nil || params[:new_host_email] == "" || params[:new_host_email] == nil || params[:new_host_password] == "" || params[:new_host_password] == nil
end
def create_host
repo = HostRepository.new
new_host = Host.new
new_host.id = (repo.all.length + 1)
new_host.name = params[:new_host_name]
new_host.username = params[:new_host_username]
new_host.email = params[:new_host_email]
new_host.password = params[:new_host_password]
repo.create(new_host)
return new_host
end
def create_space
repo = HostRepository.new
@host = repo.find(params[:hostid])
space_repo = SpaceRepository.new
space = Space.new
space.name = params[:space_name]
space.description = params[:space_description]
space.price = params[:space_price]
space.host_id = params[:hostid]
space_repo.create(space)
return space
end
end