forked from monenavarro/makers_bnb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
127 lines (109 loc) · 3.37 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
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/flash'
require_relative './lib/users'
require_relative './lib/space'
require_relative './lib/database_connection'
require './database_connection_setup'
require_relative './lib/availability'
class MakersBnB < Sinatra::Base
enable :sessions
register Sinatra::Flash
configure :development do
register Sinatra::Reloader
end
get '/' do
erb(:index)
end
get '/spaces' do
@user = User.find(session[:user_id])
if @user
@spaces = Space.spaces_listed(session[:user_id])
end
@all_spaces = Space.all
erb(:'spaces/index')
end
post '/spaces/new' do
name = params[:name]
price = params[:price]
description = params[:description]
user_id = session[:user_id]
Space.create(name: name, price: price, description: description, user_id: user_id)
redirect '/spaces'
end
get '/users/new' do
erb(:'users/new')
end
post '/users' do
@user = User.create(email: params[:email], password: params[:password])
if @user
session[:user_id] = @user.id
redirect '/spaces'
else
flash[:notice] = "Email already in use"
redirect '/users/new'
end
end
post '/sessions/destroy' do
session.clear
flash[:notice] = "You have signed out."
redirect '/'
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] = 'Please check your email or password.'
redirect '/sessions/new'
end
end
get '/spaces/availability' do
@user = User.find(session[:user_id])
if @user
@spaces = Space.spaces_listed(session[:user_id])
else
@spaces = []
end
erb :"spaces/availability/new"
end
post '/spaces/availability/new' do
space = Space.find_by_name(name: params[:space]) # return the id of the space with that name
Availability.create_period(start_date: params[:start_date], end_date: params[:end_date], space_id: space.id)
flash[:notice] = "Availability for #{params[:space]} has been added for #{params[:start_date]} to #{params[:end_date]}"
redirect '/spaces/availability'
end
get '/spaces/:id' do
@space = DatabaseConnection.query(
"SELECT * FROM spaces WHERE id = $1;", [params[:id]]
)
@user = User.find(session[:user_id])
availability = DatabaseConnection.query(
"SELECT * FROM availability WHERE space_id = $1;", [params[:id]]
)
@availability = availability.sort_by { |hash| hash['date'] }
erb(:"spaces/show")
end
post '/spaces/availability/:id' do # availability (date) id (space_id)
availability = Availability.find(params['availability'], params['id'])
Availability.remove(id: availability[0]['id'])
flash[:notice] = "You have deleted the availability on '#{params[:availability]}'"
redirect "/spaces/#{params[:id]}"
end
post '/spaces/availability/request/:id' do
space = DatabaseConnection.query(
"SELECT * FROM spaces WHERE id = $1;", [params[:id]]
)
host_id = space[0]['user_id']
host = DatabaseConnection.query(
"SELECT * FROM users WHERE id = $1;", [host_id]
)
flash[:notice] = "Please email the host at #{host[0]['email']}."
redirect "/spaces/#{params[:id]}"
end
run! if app_file == $0
end