-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
135 lines (115 loc) · 3 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
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/activerecord'
require 'bcrypt'
require_relative 'lib/space'
require_relative 'lib/user'
require_relative 'lib/availability'
require_relative 'lib/request'
class Application < Sinatra::Base
register Sinatra::ActiveRecordExtension
enable :sessions
configure :development do
register Sinatra::Reloader
end
get '/' do
render_erb(:index)
end
post '/register' do
# add user credentials to the database
user = User.new(
username: params[:username],
firstname: params[:firstname],
lastname: params[:lastname],
email: params[:email],
password: params[:password]
)
# add user to db
user.save ? (redirect '/login') : 'Failed to add user!'
end
get '/login' do
render_erb(:login)
end
post '/login' do
user = User.find_by(username: params[:username])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect '/spaces'
else
render_erb(:login)
end
end
get '/logout' do
# kill the session and redirect to the homepage
session.clear
redirect '/'
end
get '/spaces' do
redirect_if_not_logged_in
@spaces = Space.all
render_erb(:spaces)
end
get '/create-space' do
redirect_if_not_logged_in
render_erb(:create_space)
end
post '/createspace' do
user_id = session[:user_id]
space = Space.new(
name: params[:name],
description: params[:description],
price: params[:price],
address: params[:address]
)
space.save!
book_in = params[:date_from]
book_out = params[:date_to]
availability = Availability.create(
space_id: space.id,
book_in: book_in,
book_out: book_out
)
redirect '/spaces'
end
get '/requests' do
redirect_if_not_logged_in
# find spaces owned by user logged in
spaces = Space.where("user_id = #{session[:user_id]}").ids
# find the requests on those spaces:
@requests_on_my_spaces = Request.where('space_id IN (?)', spaces)
# users requests
@users_requests = Request.where("user_id = #{session[:user_id]}")
render_erb(:requests)
end
post '/request' do
user_id = session[:user_id]
space_id = session[:space_id]
request = Request.create(
space_id: space_id,
user_id: user_id,
book_in: params[:book_in],
book_out: params[:book_out]
)
redirect '/spaces'
end
get '/book/:id' do
redirect_if_not_logged_in
space_id = params[:id]
session[:space_id] = space_id # a bit of a hack
@space = Space.find(space_id)
@availabilities = Availability.where("space_id = #{space_id}")
render_erb(:book)
end
helpers do
def logged_in?
!!session[:user_id]
end
def redirect_if_not_logged_in
redirect '/login' unless logged_in?
end
def render_erb(template)
@username = User.find(session[:user_id]).username unless !logged_in?
erb template, :layout => !request.xhr?
end
end
end