forked from simon-2357/chuckle_brothers_bnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
163 lines (138 loc) · 3.76 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
require 'sinatra/base'
require 'sinatra/flash'
require_relative './lib/space'
require_relative './lib/user'
require_relative './lib/bookingrequest'
require_relative './lib/database_connection'
class ChuckleHotel < Sinatra::Base
enable :sessions
register Sinatra::Flash
get '/' do
erb :index
end
get '/spaces' do
@spaces = Space.all
erb :'spaces/index'
end
get '/spaces/new' do
if session[:user_id]
@user = User.find(id: session[:user_id])
erb :'spaces/new'
else
flash[:notice] = 'You must be logged in to create a space'
redirect '/'
end
end
post '/spaces' do
Space.create(
name: params[:name],
description: params[:description],
price: params[:price],
user_id: params[:user_id],
available_from: params[:available_from],
available_to: params[:available_to]
)
redirect '/spaces/confirmation'
end
get '/spaces/confirmation' do
@space = Space.all.last
erb :'spaces/confirmation'
end
get '/users/new' do
erb :'users/new'
end
post '/users' do
user = User.create(
username: params[:username],
email: params[:email],
password: params[:password]
)
session[:user_id] = user.id
flash[:notice] = 'Thanks for signing up to Chuckle Hotel'
redirect '/spaces'
end
get '/users/confirmation' do
@user = User.all.last
erb :'users/confirmation'
end
get '/spaces/:id' do
if session[:user_id]
@space = Space.find(id: params[:id])
@guest_id = session[:user_id]
erb :'spaces/space'
else
flash[:notice] = 'You must be logged in to view a space'
redirect '/'
end
end
post '/booking' do
booking_request = BookingRequest.create(
date: params[:date],
space_id: params[:space_id],
guest_id: session[:user_id]
)
if booking_request.class == BookingRequest
redirect "/booking/#{booking_request.id}/confirmation"
else
flash[:notice] = "Sorry the space is not available on that day, please select a different date"
redirect "/spaces/#{booking_request}"
end
end
get '/booking/:id/confirmation' do
booking_request = BookingRequest.find(id: params[:id])
@space = Space.find(id: booking_request.space_id)
erb :'booking/confirmation'
end
get '/sessions/new' do
erb :'sessions/new'
end
post '/sessions' do
user = User.authenticate(email: params[:email], password: params[:password])
if user
session[:user_id] = user.id
redirect '/spaces'
else
flash[:notice] = 'Email or password incorrect'
redirect '/sessions/new'
end
end
post '/sessions/destroy' do
session.clear
flash[:notice] = 'You have logged out successfully'
redirect '/'
end
get '/host/requests' do
@booking_requests = BookingRequest.all_for_user(id: session[:user_id])
erb :'host/requests'
end
get '/host/spaces' do
erb :'host/index'
end
post '/requests/approve' do
request = BookingRequest.approve(id: params[:request_option])
session[:last_request] = params[:request_option]
redirect '/host/approved'
end
post '/requests/reject' do
request = BookingRequest.reject(id: params[:request_option])
session[:last_request] = params[:request_option]
redirect '/host/rejected'
end
get '/host/approved' do
@space = Space.find(id: BookingRequest.find(id: session[:last_request]).space_id)
erb :'host/approved'
end
get '/host/rejected' do
@space = Space.find(id: BookingRequest.find(id: session[:last_request]).space_id)
erb :'host/rejected'
end
get '/host' do
if session[:user_id]
erb :'host/index'
else
flash[:notice] = 'You must be logged in to be a host'
redirect '/'
end
end
run! if app_file == $0
end