-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.rb
More file actions
97 lines (79 loc) · 2.42 KB
/
run.rb
File metadata and controls
97 lines (79 loc) · 2.42 KB
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
require 'sinatra'
require 'erb'
require 'pg'
require 'json'
set :bind, '0.0.0.0'
get '/' do
erb :index
end
post '/login' do
conn = PGconn.connect("localhost", 5432, '', '', "test", "postgres", "admin")
res = conn.exec('SELECT * from "users" where user_name = \'' + params[:username] + '\' and user_password= \'' + params[:password] + '\' ')
if res.count > 0 then
puts res[0]['user_status']
if (res[0]['user_status'] == 1)
redirect '/blocked'
else
redirect '/menu'
end
else
redirect '/unknown'
end
end
post '/form-submit' do
@form_details = Hash.new()
@form_details['forename'] = params[:forename]
@form_details['surname'] = params[:surname]
@form_details['house_number'] = params[:house_number]
@form_details['street'] = params[:street]
@form_details['city'] = params[:city]
@form_details['postcode'] = params[:postcode]
@form_details['sex'] = params[:sex]
@form_details['interest'] = Hash.new()
@form_details['interest']['walking'] = params[:walking]
@form_details['interest']['cycling'] = params[:cycling]
@form_details['interest']['swimming'] = params[:swimming]
@form_details['cars'] = params[:cars]
erb :form_output
end
get '/unknown' do
@message = 'Unknown User'
erb :message
end
get '/blocked' do
@message = 'User Blocked'
erb :message
end
get '/menu' do
@message = 'Logged in'
erb :menu
end
get '/form-details' do
erb :form
end
get '/get_user/:user_id' do
conn = PGconn.connect("localhost", 5432, '', '', "test", "postgres", "admin")
res = conn.exec('SELECT * from "users" where user_id = \'' + params['user_id'] + '\' ')
result = {}
if res.count > 0 then
result['user_name'] = res[0]['user_name']
result['user_id'] = res[0]['user_id']
result['user_status'] = res[0]['user_status']
else
result['status'] = 'No User'
end
return result.to_json
end
post '/add_user' do
conn = PGconn.connect("localhost", 5432, '', '', "test", "postgres", "admin")
res = conn.exec('SELECT * from "users" where user_name = \'' + params['user_name'] + '\' ')
result = {}
if res.count > 0 then
result['status'] = 'User Already Exists'
else
conn = PGconn.connect("localhost", 5432, '', '', "test", "postgres", "admin")
res = conn.exec('insert into users (user_name, user_password) Values (\'' + params['user_name'] + '\', \'' + params['user_password'] + '\') ')
result['status'] = 'User Added'
end
return result.to_json
end