-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
158 lines (136 loc) · 4.82 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
# frozen_string_literal: true
require 'sinatra/base'
require 'sinatra/flash'
require 'sinatra/reloader'
require 'sass'
require './lib/user'
require './lib/availability'
require './lib/booking'
require './lib/database_connection_setup'
require './lib/property'
class InnCognito < Sinatra::Base
register Sinatra::Reloader
register Sinatra::Flash
enable :sessions
disable :strict_paths
set :views, 'public/assets/views/'
set :scss, 'public/assets/sass/'
def location(*args)
session[:location] = []
args.each do |a|
session[:location] << { name: a, link: request.path_info }
end
end
get '/' do
location 'home'
@current_user = User.find(id: session[:user_id])
@properties = Property.all
@properties = Property.sort_by_recent if params[:sort] == 'recent'
@properties = Property.sort_by_low_cpn if params[:sort] == 'lowprice'
@properties = Property.sort_by_high_cpn if params[:sort] == 'highprice'
erb :index
end
get '/sign-up' do
location 'sign up'
session[:location] = [{ name: 'sign up', link: request.path_info }]
erb :'users/sign_up'
end
post '/sign-up' do
@current_user = User.create(name: params[:name], email: params[:email], password: params[:password])
session[:user_id] = @current_user.id unless @current_user.nil?
redirect '/'
end
get '/sign-in' do
location 'sign in'
erb :'users/sign_in'
end
post '/sign-in' do
@current_user = User.authenticate(email: params[:email], password: params[:password])
if @current_user
session[:user_id] = @current_user.id
redirect '/'
else
flash[:notice] = 'Please check your email or password.'
redirect '/sign-in'
end
end
get '/book/:id' do
location 'new booking'
session[:place_id] = params[:id]
@current_user = User.find(id: session[:user_id])
@property = Property.find(id: session[:place_id])
@availability = Availability.find(property_id: session[:place_id])
erb :'account/create_booking_request'
end
post '/book/success' do
@duration = (Date.parse(params[:end_date]) - Date.parse(params[:start_date])).to_i
@property = Property.find(id: session[:place_id])
begin
@booking = Booking.create(user_id: params[:user_id], property_id: params[:property_id], start_date: params[:start_date], end_date: params[:end_date], owner_id: params[:owner_id])
rescue StandardError => e
@error = e
end
erb :'account/booking_request_confirmation'
end
get '/account/add-property' do
location 'new property'
erb :'account/add_property'
end
post '/account/add-property' do
@current_user = User.find(id: session[:user_id])
@property = Property.create(name: params[:name], description: params[:description], cpn: params[:cpn], user_id: @current_user.id)
@availability = Availability.create(property_id: @property.id, start_date: params[:start_date], end_date: params[:end_date])
path = "./public/images/properties/#{@property.id}"
if params[:file]
Dir.mkdir(path) unless Dir[path].any?
@filename = params[:file][:filename]
file = params[:file][:tempfile]
File.open("#{path}/#{(Dir[path + '/*'].length + 1)}_#{@filename}", 'wb') do |f|
f.write(file.read)
end
end
redirect '/'
end
get '/account/manage-bookings' do
location 'manage bookings'
@current_user = User.find(id: session[:user_id])
@properties = Property.where(user_id: @current_user.id)
@bookings = Booking.find_by_owner_id(@current_user.id)
@requests = Booking.find_by_user_id(@current_user.id)
@bookings.each_with_index do |b, i|
@bookings.delete_at(i) if b.approval != Booking::PENDING
end
erb :'account/manage_booking_requests'
end
get '/account/manage-bookings/:id' do
session[:place_id] = params[:id]
@current_user = User.find(id: session[:user_id])
@property = Property.find(id: session[:place_id])
@availability = Availability.find(property_id: session[:place_id])
erb :'account/edit_property'
end
post '/account/manage-bookings/:id' do
@property = Property.edit(id: session[:place_id], name: params[:name], description: params[:description], cpn: params[:cpn])
@availability = Availability.edit(property_id: session[:place_id], start_date: params[:start_date], end_date: params[:end_date])
redirect 'account/manage-bookings'
end
post '/account/approve-booking' do
@bookings = Booking.set_approval(params[:booking_id], Booking::APPROVED)
redirect '/account/manage-bookings'
end
post '/account/reject-booking' do
@bookings = Booking.set_approval(params[:booking_id_2], Booking::REJECTED)
redirect '/account/manage-bookings'
end
post '/sign-out' do
session.clear
flash[:notice] = 'You have signed out.'
redirect('/')
end
not_found do
erb :'errors/404'
end
error do
erb :'errors/500'
end
end