-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
199 lines (168 loc) · 4.7 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
# frozen_string_literal: true
require "sinatra/base"
require "sinatra/reloader"
require_relative "lib/space"
require_relative "lib/space_repository"
require_relative "lib/database_connection"
require_relative "lib/booking"
require_relative "lib/booking_repository"
require_relative "lib/user_repository"
DatabaseConnection.connect("makersbnb_test")
class Application < Sinatra::Base
configure :development do
register Sinatra::Reloader
enable :sessions
also_reload 'lib/space_repository'
also_reload "lib/space"
end
post "/spaces/book/:id" do
@message = "Booking confirmed"
repo = SpaceRepository.new
@space = repo.is_available?(params[:id])
if @space.dates.include?(params[:dates])
@message = "Unavailable on this date"
else
new_booking = Booking.new
new_booking.space_id = params[:id]
new_booking.user_id = session[:user_id]
new_booking.booking_date = params[:dates]
new_booking.confirmation = false
new_booking.total_cost = @space.price
booking_repo = BookingRepository.new
booking_repo.create(new_booking)
end
#get book required to return post book
return erb(:booking_confirmed)
end
get "/" do
if session[:session_email]
user = UserRepository.new
@user = user.find(session[:session_email])
end
repo = SpaceRepository.new
@all_spaces = repo.all
return erb(:index)
end
get '/registrations/signup' do
erb :'registrations/signup'
end
post '/adduser' do
@user_repository = UserRepository.new
new_user = User.new
new_user.name = params[:name]
new_user.username = params[:username]
new_user.email = params[:email]
new_user.password = params[:password]
@user_repository.create(new_user)
session[:user_id] = new_user.id
redirect '/'
end
# post "/" do
# repo = SpaceRepository.new
# index = 1
# while index <= repo.all.length do
# @all_spaces = repo.all
# space = repo.is_available?(index)
# if space.dates.include?(params[:dates])
# @all_spaces.delete(space)
# end
# index += 1
# end
# redirect "/"
# end
###james
get "/spaces/new" do
return erb(:new_space)
end
post "/spaces/new" do
name = params[:name]
description = params[:description]
price = params[:price]
user_id = 1 ##TESTTT
# if user_id.nil?
# status 400
# return 'ERROR: Please log in to add a new listing'
# end
new_space = Space.new
new_space.name = name
new_space.description = description
new_space.price = price
new_space.user_id = user_id
SpaceRepository.new.create(new_space)
redirect "/"
end
get "/spaces/book/:id" do
repo = SpaceRepository.new
@space = repo.find(params[:id])
if (params[:id]).to_i > repo.all.length
redirect "/"
elsif session[:session_user_id] == nil
redirect '/sessions/login'
else
return erb(:book)
end
end
get "/spaces/:id" do
repo = SpaceRepository.new
if (params[:id]).to_i > repo.all.length
redirect "/"
else
@space = repo.find(params[:id])
return erb(:spaces)
end
end
get '/mybookings/:id' do
#@session_id = 1 #sessions[:id]
#if sessions[:id] == params[:id]
#flash error message
#redirect "/"
#else
repo = BookingRepository.new
@renter_bookings = repo.find_by_renter(params[:id])
new_repo = BookingRepository.new
@host_bookings = new_repo.bookings_by_owner(params[:id])
return erb(:mybookings)
end
post "/mybookings/success/:id" do
repo = BookingRepository.new
params = [params[:booking_id]]
query = "UPDATE SET confirmation = true WHERE id = $1"
DatabaseConnection.exec_params(query, params)
return erb(:booking_confirmed)
end
get "/mybookings/success/:id" do
return erb(:booking_confirmed)
end
#get '/requests/:id' do
post "/sessions" do
repo = UserRepository.new
user = repo.find(params[:email])
users = repo.all
email = params[:email]
password = params[:password]
return erb :'sessions/login' if users.any? { |user| user.email == email } == false
login = repo.sign_in(email, password)
# if user == "incorrect"
# return erb :"sessions/login"
# end
if login == "correct"
session[:session_user_id] = user.id
session[:session_email] = user.email
p session
redirect "/"
else
# @message = incorrect login details
return erb :'sessions/login'
end
end
get "/sessions/login" do
erb :"sessions/login"
end
post "/logout" do
session[:session_user_id] = nil
session[:session_email] = nil
repo = SpaceRepository.new
@all_spaces = repo.all
erb :index
end
end