This repository has been archived by the owner on Dec 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.rb
93 lines (75 loc) · 2.92 KB
/
account.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
before do
@customer_info = SQLite3::Database.new './curry_house.sqlite'
@redeeming = false
end
def get_info
@results = @customer_info.get_first_row('SELECT * FROM customer WHERE twitterAcc = ?',session[:twitter_acc])
@twitter_acc = @results[0]
@cc_no = @results[1]
@address = @results[2]
@balance = @results[3]
@first_name = @results[5]
@surname = @results[6]
@atSheffield = @customer_info.get_first_value('SELECT city FROM customer WHERE twitterAcc = ?', @twitter_acc).downcase.eql? 'sheffield'
end
end
get '/account' do
if !session[:logged_in]
redirect '/'
end
get_info
@updating = false
@updating_balance = false
erb :account_new
end
post '/update_info' do
@updating = true
@cc_no = params[:cc].strip
@address = params[:address].strip
@first_name = params[:first_name].strip
@surname = params[:surname].strip
@city = params[:city].strip
@cc_no_ok = (@cc_no.nil? || @cc_no=="") || (@cc_no.length==16 && @cc_no.to_i.to_s == @cc_no)
@first_name_ok = !@first_name.nil? && @first_name != ''
@surname_ok = [email protected]? && @surname != ''
@all_ok = @cc_no_ok && @first_name_ok && @surname_ok
if @all_ok
query = 'UPDATE customer SET cc=?, address=?, firstName=?, surname=?, city=? WHERE twitterAcc = ?'
@customer_info.execute(query, [@cc_no,@address,@first_name,@surname,@city,session[:twitter_acc]])
session[:city] = @city
end
get_info
erb :account_new
end
post '/update_balance' do
@updating_balance = true
get_info
@has_cc = @cc_no!='' && !@cc_no.nil?
if @has_cc
old_balance = @customer_info.get_first_value('SELECT balance FROM customer WHERE twitterAcc = ?', session[:twitter_acc])
@added_funds = params[:amount].to_i
new_balance = old_balance.to_i+ @added_funds
@customer_info.execute('UPDATE customer SET balance = ? WHERE twitterAcc = ?',[new_balance,session[:twitter_acc]])
end
get_info
erb :account
end
post '/redeem_voucher' do
@redeeming = true
code = params[:code].strip
@exists = @customer_info.get_first_value('SELECT COUNT(*) FROM competition_winners WHERE coupon_code = ?',code) !=0
@redeemed = (@customer_info.get_first_value('SELECT redeemed FROM competition_winners WHERE coupon_code = ?',code) ==1) if @exists
if @exists && !@redeemed
reward = @customer_info.get_first_value('SELECT cp FROM competition_winners WHERE coupon_code = ?',code)
@added_funds = reward
new_balance = @customer_info.get_first_value('SELECT balance FROM customer WHERE twitterAcc = ?', session[:twitter_acc]) + reward
@customer_info.execute('UPDATE customer SET balance = ? WHERE twitterAcc = ?',[new_balance,session[:twitter_acc]])
@customer_info.execute('UPDATE competition_winners SET redeemed = 1 WHERE coupon_code = ?',code)
@succ_red = true
else
@succ_red = false
end
get_info
erb :account
end