-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
131 lines (102 loc) · 3.02 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
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/session'
require 'bcrypt'
require_relative 'lib/database_connection'
require_relative 'lib/space_repository'
require_relative 'lib/space'
require_relative 'lib/account_repository'
require_relative 'lib/account'
DatabaseConnection.connect('makersBnB')
class Application < Sinatra::Base
# This allows the app code to refresh
# without having to restart the server.
configure :development do
register Sinatra::Reloader
register Sinatra::Session
also_reload 'lib/space_repository.rb'
get '/' do
return erb(:index) # links to an index file with the html content
end
get '/all_spaces' do
# if session[:user_id] == nil
# # No user id in the session
# # so the user is not logged in.
# return redirect('/sign_in')
# else
# # The user is logged in, display
# # their account page.
# return erb(:account)
# end
repo = SpaceRepository.new
@spaces = repo.all
return erb(:all_spaces) # links to an index file with the html content
end
get '/all_spaces/new_space' do
if session? == false
redirect('/sign_in')
else
return erb(:new_space_form)
end
end
get '/all_spaces/:id' do
repo = SpaceRepository.new
# space_repo = ArtistRepository.new
@space = repo.find(params[:id])
return erb(:space)
end
post '/all_spaces' do
if invalid_request_parameters?
status 400
return ''
end
repo = SpaceRepository.new
new_space = Space.new
new_space.name = params[:name]
new_space.price = params[:price]
new_space.description = params[:description]
new_space.availability = [:availability]
repo.create(new_space)
return erb(:space_added)
end
get '/sign_up' do
return erb(:sign_up)
end
post '/sign_up' do
repo = AccountRepository.new
@new_account = Account.new
@new_account.name = params[:name]
@new_account.password = params[:password]
@new_account.email = params[:email]
@new_account.phone = params[:phone]
repo.create(@new_account)
session_start!
session[:user_id] = @new_account.id
return erb(:post_sign_up)
end
get '/sign_in' do
return erb(:sign_in)
end
get '/user_homepage' do
return erb(:user_homepage)
end
post '/sign_in' do
repo = AccountRepository.new
@account = repo.find_by_email(params[:email])
if BCrypt::Password.new(@account.password) == params[:password]
session_start!
session[:user_id] = @account.id
return erb(:user_homepage)
else
return erb(:sign_in)
end
end
get '/sign_out' do
session_end!
return erb(:sign_out)
end
def invalid_request_parameters?
return params[:name]==nil || params[:price]==nil || params[:description]==nil || params[:availability]==nil
end
end
end