This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
forked from ntijoh/slutprojektWSP21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
362 lines (322 loc) · 11 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require 'sinatra'
require 'slim'
require 'sqlite3'
require 'bcrypt'
require './model.rb'
require 'byebug'
enable :sessions
# include Model # why you not work huh
# First login:
first_login = true
# Error messages:
not_logged_in_error = "You need to be logged in to see this."
wrong_password = "Wrong username or password. >:("
not_matching = "Passwords didn't match!"
data_error = "You've not entered the correct data, check if:\nYou've entered data in every field.\nEntered correct data type."
not_enough_money = "You don't have that amount of money on your account."
business_not_found = "There's no business with this name."
user_not_found = "There's no user with that name."
already_admin = "The user is already an Admin."
no_access = "You don't have access to this data."
cooldown_error = "The cooldown login cooldown is not ready."
before do
if request.path_info != "/" && request.path_info != "/register" && request.path_info != "/login" && request.path_info != "/browse/" && request.path_info != "/error" && session[:id] == nil
session[:error] = not_logged_in_error
redirect("/error")
end
# Försöker förhindra att man kan gå in på någon annans account när man väl är inloggad:
# if params[:id] != session[:id] && params[:id] != nil
# session[:error] = no_access
# redirect("/error")
# end
end
# Displays the error
#
# @session [String] error, the error message that should be displayed
get("/error") do
error = session[:error]
slim(:error, locals:{error:error})
end
# Display the home page to log in or register
#
get('/') do
slim(:index)
end
# Display the log in page
#
get('/login') do
slim(:login)
end
# Display the home page to log in or register and redirects to the browse page
#
# @param [String] username, the username of the user
# @param [String] password, the non-decrypted password of the user
# @see Model#logged_in?
post('/login') do
username = params[:username]
password = params[:password]
if first_login == true
session[:cooldown] = nil
end
login_response = logged_in?(username, password, first_login, session[:cooldown])
if login_response[:success] == true
if first_login == true
first_login = false
end
session[:cooldown] = login_response[:cooldown_timer]
redirect('/browse/')
elsif login_response[:success] == "cooldown"
if first_login == true
first_login = false
end
session[:cooldown] = login_response[:cooldown_timer]
session[:error] = cooldown_error
redirect("/error")
else
if first_login == true
first_login = false
end
session[:cooldown] = login_response[:cooldown_timer]
session[:error] = wrong_password
redirect("/error")
end
end
# Display the register a new user page
#
get('/register') do
slim(:register)
end
# Registering a new user and inserting the data into the database and redirects to the browse page
#
# @param [String] username, the username of the user
# @param [String] password, the non-decrypted password of the user
# @param [String] password_confirm, a confirmation string that should be equal to "password" for the registration to ge through
# @see Model#register_user
post('/register') do
username = params[:username]
password = params[:password]
password_confirm = params[:password_confirm]
if password == password_confirm
register_user(username, password) # Missade felhantering här, enkel if-statement
redirect("/browse/")
else
session[:error] = not_matching
redirect("/error")
end
end
# Displays the posts browsing page
#
get('/browse/') do
slim(:"posts/index")
end
# Displays the businesses data of the current user
#
# @param [Integer] :id, the id of the current user
# @see Model#get_businesses_from_user
get('/business/:id') do # if the user have 0 businesses, make it show another page
id = params[:id].to_i
business = get_businesses_from_user(id)
slim(:"businesses/show", locals:{business:business})
end
# Displays the post creating page
#
# @param [Integer] :id, the id of the user
# @params [Integer] business_id, the id of the business that wants to create a new post
get('/create_post/:id/new') do
id = params[id].to_i
business_id = params[:business_selected].to_i
slim(:"posts/new", locals:{business_id:business_id})
end
# Creates a new invention post with the following information and then redirects to the the users businesses tab:
#
# @param [Integer] :id, the id of the business creating the post
# @param [String] title, the title of the post
# @param [String] picture, a picture of the invention # might not be a string depending on how I make it work lol
# @param [String] body, the description of the invention
# @param [Integer] money_offer, the amount of money the user wants
# @param [Integer] percentage_offer, the percentage the user gives to the buyer for that money
# @see Model#create_post
post('/create_post/:id/update') do # fix how picture works
business_id = params[:id].to_i
user_id = session[:id]
title = params[:title]
picture = params[:picture]
body = params[:body]
money_offer = params[:money_offer].to_i
percent_offer = params[:percent_offer].to_i
if business_id == nil || title == nil || picture == nil || body == nil || money_offer == nil || percent_offer == nil || percent_offer == 0 || money_offer == 0
session[:error] = data_error
redirect("/error")
end
create_post(business_id, title, picture, body, money_offer, percent_offer)
redirect("/business/#{user_id}")
end
# Displays the current users profile
#
# @param [Integer] :id, the id of the current user which will be used to get the users business data
# @see Model#get_user_business_data
get('/account/:id') do
id = params[:id].to_i
business = get_user_business_data(id)
slim(:"user/show", locals:{business:business})
end
# Logs the current user out by setting the session to nil and redirects to the home page
#
get('/logout') do
session[:id] = nil
session[:admin] = nil
redirect('/')
end
# Deletes the current user out of the database and redirects back to the home screen
#
# @param [Integer] :id, id of the current user
# @see Model#delete_user
post('/user/:id/delete') do
id = params[:id].to_i
delete_user(id)
redirect('/')
end
# Gives other admins the opertunity to make someone else also an admin
#
# @session [Integer] :id, the id of the user
# @param [String] username, the name of the new admin
# @see Model#make_admin
post('/make_admin') do
id = session[:id].to_i
username = params[:username]
if make_admin(username) == "already_exists"
session[:error] = already_admin
redirect("/error")
elsif make_admin(username) == "not_found"
session[:error] = user_not_found
redirect("/error")
else
redirect("/account/#{id}")
end
end
# Allows the user to change its username and redirects back to the account tab
#
# @param [Integer] :id, id of the current user
# @param [String] username, the new username of the user
# @see Model#change_username
post('/change_username/:id/update') do
id = session[:id].to_i
new_username = params[:username]
change_username(id, new_username)
redirect("/account/#{id}")
end
# Allows the user to change its password and redirects back to the account tab
#
# @param [Integer] :id, id of the current user
# @param [String] password, the new non-decrypted password of the user
# @param [String] password_confirm, the new password confirmation of the user
# @param [String] old_password, the old password entered by the user
# @see Model#password_check
# @see Model#change_password
post('/change_password/:id/update') do
id = params[:id].to_i
password = params[:password]
password_confirm = params[:password_confirm]
old_password = params[:old_password]
result = password_check(id, old_password)
if result[:error] == true
session[:error] = result[:message]
redirect("/error")
end
if password == password_confirm
change_password(id, password)
redirect("/account/#{id}")
else
session[:error] = not_matching
redirect("/error")
end
end
# Adds money to the current users account and redirects back to the account tab
#
# @param [Integer] :id, id of the current user
# @param [Integer] money_to_add, the amount of money the user wants to add
# @see Model#add_account_money
post('/add_account_money/:id/update') do
id = params[:id].to_i
money_to_add = params[:money_to_add].to_i
if money_to_add <= 0 || money_to_add == nil
session[:error] = data_error
redirect("/error")
end
add_account_money(id, money_to_add)
redirect("/account/#{id}")
end
# Adds money to a selected business of the current user (while subtracting that amount from the users account balance) and redirects back to the account tab
#
# @param [Integer] :id, id of the business
# @session [Integer] :id, the id of the current user
# @param [Integer] money_to_add, the amount of money the user wants to add
# @see Model#account_money
# @see Model#add_account_money
post('/add_money/:id/update') do
id = params[:id].to_i
user_id = session[:id]
user_money = account_money(id)
money_to_add = params[:money_to_add].to_i
if money_to_add <= 0 || money_to_add == nil
session[:error] = data_error
redirect("/error")
elsif user_money > money_to_add
add_account_money(id, money_to_add)
redirect("/account/#{id}")
else
session[:error] = not_enough_money
redirect("/error")
end
end
# Makes the user "leave" a business and redirects them back to their account
#
# @param [Integer] :id, id of the business
# @session [Integer] :id, the id of the current user
# @see Model#leave_business
post('/leave/:id/delete') do
id = params[:id].to_i
user_id = session[:id]
leave_business(user_id, id)
redirect("/account/#{id}")
end
# Lets the user type in the name of a business to join that business and then redirects them back to their account
#
# @param [Integer] :id, the id of the current user
# @param [String] business_name, the name of the business the user wants to join
# @see Model#join_business
post('/join_business/:id/update') do
id = params[:id]
business_name = params[:business_name]
result = join_business(id, business_name)
if result[:error] == false
redirect("/account/#{id}")
else
session[:error] = result[:message]
redirect("/error")
end
end
# Let the current user create a business and then redirects them back to their account
#
# @param [Integer] :id, the id of the current user
# @param [String] business_name, the name of the business the user wants to create
# @param [Integer] startingh_budget, the starting budget of the business
# @see Model#account_money
# @see Model#create_business
post('/create_business/:id/update') do
id = params[:id]
business_name = params[:business_name]
if params[:starting_budget] == nil
starting_budget = 0
else
starting_budget = params[:starting_budget].to_i
end
user_money = account_money(id)
if user_money > starting_budget
create_business(id, user_money, business_name, starting_budget)
redirect("/account/#{id}")
else
session[:error] = not_enough_money
redirect("/error")
end
end