-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
79 lines (65 loc) · 2.02 KB
/
server.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
# Victor Andreoni
# CS4241 - Assignment 4
# Index Ruby file to use with Sinatra as server
$: << File.expand_path(File.dirname(__FILE__) + "/lib") #Add lib folder to path
require 'sinatra'
require 'pgmanager'
enable :sessions
# Serve index of website
get '/' do
erb :index
end
# Serve database panel. If no session exists, return error.
post '/panel' do
if session[:loggeduser] == nil
"You must be logged in to access the database panel!"
else
erb :panel
end
end
# Log user off
get '/logoff' do
session[:loggeduser] = nil
redirect to('/')
end
# Register user while performing some server-side validation - Called with AJAX
post '/register' do
@fullname = params[:fullname].to_s
@username = params[:username].to_s
@password = params[:password].to_s
result = registerUser(ENV['DATABASE_URL'], @fullname, @username, @password)
if result == 1 # Result code for existing username
return 1.to_s # Failure
else
session[:loggeduser] = result # Login the newly registered user
return 0.to_s # Success
end
end
# Login user by checking username and password with database - Called with AJAX
post '/login' do
@username = params[:username].to_s
@password = params[:password].to_s
result = loginUser(ENV['DATABASE_URL'], @username, @password) # Execute login routine
if result == 1 # Result code for login error, established inside pgmanager.rb
return 1.to_s # Failure
else
session[:loggeduser] = result # Create session for logged user
return 0.to_s # Success
end
end
# Query database with provided search parameters
get '/search' do
if session[:loggeduser] == nil
"You must be logged in to access the database panel!"
else
return queryDB(ENV['DATABASE_URL'], params["name"], params["company"], params["year"])
end
end
# Add smartphones to database
post '/add' do
return addToDB(ENV['DATABASE_URL'], params[:code], params[:name], params[:company], params[:year])
end
# Update smartphones in database
post '/update' do
return updateDB(ENV['DATABASE_URL'], params[:code], params[:name], params[:company], params[:year])
end