-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.rb
176 lines (141 loc) · 4.12 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
# frozen_string_literal: true
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/flash'
require 'bcrypt'
require 'sinatra/content_for'
require_relative 'lib/database_connection'
require_relative 'lib/space_repository'
require_relative 'lib/space'
require_relative 'lib/user_repository'
require_relative 'lib/user'
require_relative 'lib/booking_repository'
DatabaseConnection.connect('rubnb') if ENV['ENV'] != 'test'
class Application < Sinatra::Base
enable :sessions
helpers Sinatra::ContentFor
configure :development do
register Sinatra::Reloader
register Sinatra::Flash
also_reload 'lib/space_repository'
also_reload 'lib/user_repository'
end
get '/' do
repo = SpaceRepository.new
@top_spaces = repo.find_top_spaces
@user_not_logged_in = session[:user].nil?
return erb(:index)
end
get '/spaces' do
repo = SpaceRepository.new
@spaces = repo.all
return erb(:spaces)
end
get '/my_spaces' do
redirect '/login' if session[:user].nil?
repo = SpaceRepository.new
@spaces_by_user = repo.all_by_user(session[:user].id)
return erb(:userpage)
end
get '/signup' do
return erb(:signup)
end
post '/signup' do
repo = UserRepository.new
user = User.new
username_valid = repo.is_username_unique?(params[:username])
email_valid = repo.is_email_unique?(params[:email])
if username_valid && email_valid
user.id = params[:id].to_i
user.name = params[:name]
user.username = params[:username]
user.email = params[:email]
user.password = params[:password]
repo.create(user)
session[:user] = user
redirect '/my_spaces'
else
flash[:username] = 'Username already in use' unless username_valid
flash[:email] = 'Email already in use' unless email_valid
redirect back
end
end
get '/login' do
erb(:login)
end
post '/login' do
repo = UserRepository.new
email = params[:email]
password = params[:password]
session[:user] = repo.log_in(email, password)
if session[:user].nil?
flash[:error] = 'email/password incorrect'
redirect back
else
redirect '/my_spaces'
end
end
get '/logout' do
session[:user] = nil
redirect '/'
end
post '/logout' do
session[:user] = nil
redirect '/'
end
get '/spaces/new' do
redirect '/login' if session[:user].nil?
@current_date = Date.today.strftime('%Y-%m-%d')
return erb(:spaces_new)
end
post '/spaces/new' do
redirect '/login' if session[:user].nil?
repo = SpaceRepository.new
space = Space.new
dates_valid = (Date.parse(params[:available_to]) >= Date.parse(params[:available_from]))
if dates_valid
space.name = params[:name]
space.description = params[:description]
space.price_per_night = params[:price_per_night]
space.available_from = params[:available_from]
space.available_to = params[:available_to]
space.user_id = session[:user].id
repo.create(space)
redirect('/my_spaces')
else
flash[:dates_valid] = 'Your space should be available for at least one night.'
redirect back
end
end
get '/spaces/:id' do
repo = SpaceRepository.new
@space = repo.find_by_id(params[:id])
@dates = repo.availability_status(params[:id])
@user_id = session[:user].id unless session[:user].nil?
return erb(:space_view)
end
post '/book' do
redirect '/login' if session[:user].nil?
repo = BookingRepository.new
repo.create(params[:date], params[:user_id], params[:space_id])
redirect '/bookings'
end
get '/bookings' do
redirect '/login' if session[:user].nil?
booking_repo = BookingRepository.new
@space_repo = SpaceRepository.new
@user_bookings = booking_repo.find_by_user(session[:user].id)
@space_bookings = booking_repo.find_for_user(session[:user].id)
return erb(:bookings)
end
post '/confirm' do
booking_repo = BookingRepository.new
booking_repo.confirm(params[:id])
redirect '/bookings'
end
post '/deny' do
booking_repo = BookingRepository.new
booking_repo.deny(params[:id])
redirect '/bookings'
end
end