-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
138 lines (116 loc) · 3.13 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
# in app.rb
require "sinatra/base"
# an extra gem to require
require "sinatra/activerecord"
require "sinatra/reloader"
require_relative "./lib/user"
require_relative "./lib/space"
require_relative "./lib/booking"
# BCrypt for encrypting the passwords
require "bcrypt"
class Application < Sinatra::Base
enable :sessions
configure :development do
register Sinatra::ActiveRecordExtension
register Sinatra::Reloader
end
get '/search' do
return erb(:search)
end
post '/search' do
@space = Space.where("spaces.date_from >= ? AND spaces.date_to <= ? AND spaces.available = ?", params[:date_from], params[:date_to], true)
return erb(:viewspaces)
end
def username_exists(username)
User.find_by(user_name: username)
end
def email_exists(email)
User.find_by(email: email)
end
get '/' do
@user = User.all
return erb(:index)
end
get '/login' do
return erb(:login)
end
get '/book' do
return erb(:book)
end
get '/signup' do
return erb(:signup)
end
get '/login' do
return erb(:login)
end
get '/spaces' do
return erb(:listspace)
end
get '/viewspaces' do
@space = Space.all
return erb(:viewspaces)
end
get '/viewspace/:id' do
repo = Space.all
@space = repo.find(params[:id])
return erb(:viewspace)
end
get '/booking' do
return erb(:bookings)
end
post '/booking' do
space = Space.find_by(id: params[:space_id])
return erb(:booking_error) if !space.available
Booking.create(user_id: params[:user_id], space_id: params[:space_id], date: params[:date])
return erb(:booking_created)
end
get '/confirm' do
return erb(:confirm)
end
post '/confirm' do
booking = Booking.find_by(space_id: params[:space_id])
return erb(:confirm_error) if !booking.available
Booking.update(user_id: params[:user_id], space_id: params[:space_id], date: params[:date], available: false)
return erb(:confirmed)
end
get '/delete_bookings' do
return erb(:delete)
end
post '/delete_bookings' do
Booking.destroy_by(space_id: params[:space_id])
if Booking.all.length == 0
return erb(:deleted)
else Booking.all.length >= 1
return erb(:deleted_error)
end
end
post '/spaces' do
Space.create(name: params[:name], description: params[:description], price_per_night: params[:price_per_night], date_from: params[:date_from], date_to: params[:date_to], user_id: session[:user_id])
return erb(:viewspaces)
end
post '/signup' do
return erb(:signup_error) if username_exists(params[:user_name]) || email_exists(params[:email])
User.create(user_name: params[:user_name], email: params[:email], password: params[:password])
erb(:user_added)
end
post '/login' do
username = params[:user_name]
password = params[:password]
if user = User.find_by(user_name: username)
if user.password == password
session[:user_id] = user.id
return erb(:loggedin)
else
return erb(:login_error)
end
else
return erb(:login_error)
end
end
post '/logout' do
session.clear
return erb(:logout)
end
2012-01-01
2012-02-03
end