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 pathadmin.rb
150 lines (124 loc) · 4.24 KB
/
admin.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
require 'sqlite3'
require 'sinatra'
require 'erb'
require_relative 'customer'
before do
@db = SQLite3::Database.new './curry_house.sqlite'
end
get '/admin' do
redirect '/' if !session[:admin]
@page_header = 'Dashboard'
erb :admin_panel
end
get '/admin_edit_menu' do
redirect '/' if !session[:admin]
query = %{SELECT id, itemName, description, unitPrice, vegetarian, glutenFree, atSheff, atBirm
FROM menu
WHERE category = ? }
@starterResults = @db.execute query, 'starter'
@mildResults = @db.execute query, 'mild'
@hotResults = @db.execute query, 'hot'
@riceResults = @db.execute query, 'rice'
@sidesResults = @db.execute query, 'side'
Array @m = [
["Starters", @starterResults],
["Mild Dishes", @mildResults],
["Hot Dishes", @hotResults],
["Rice Dishes", @riceResults],
["Sides", @sidesResults]
]
if !params[:errorAdding].nil?
@displayError = "Oops... There was an error procesing your request and the item couldn´t be added tho the menu."
end
erb :dashboard_edit_menu
end
post '/delete_menu' do
menuID = params[:menuID]
if !menuID
redirect '/admin'
else
executeDeleteQuery = @db.execute('DELETE FROM menu WHERE id = ?',[menuID])
redirect '/admin_edit_menu'
end
end
post '/add_menu' do
@dishType = params[:dishType].strip
@dishName = params[:dishName].strip
@dishCost = params[:dishCost].strip
@dishDesc = params[:dishDesc].strip
@dishExtra = params[:dishExtraInfo].strip
@dishAvailability = params[:dishAvailability].strip
@confirmation = params[:dishConfirmation]
dishType_ok = [email protected]?
dishName_ok = [email protected]? && @dishName != ""
dishCost_ok = [email protected]? && @dishCost != ""
dishDesc_ok = [email protected]? && @dishDesc != ""
dishExtra_ok = [email protected]? && @dishExtra != ""
dishAvail_ok = [email protected]? && @dishAvailability != ""
confirmation_ok = [email protected]?
all_ok = dishType_ok && dishName_ok && dishCost_ok && dishDesc_ok && confirmation_ok && dishExtra_ok && dishAvail_ok
if all_ok
case @dishExtra
when "v"
vegetarian = 1
glutenFree = 0
when "gf"
vegetarian = 0
glutenFree = 1
else
vegetarian = 1
glutenFree = 1
end
case @dishAvailability
when "sh"
atSheff = 1
atBirm = 0
when "bi"
atSheff = 0
atBirm = 1
else
atSheff = 1
atBirm = 1
end
executeAddQuery = @db.execute('INSERT INTO menu (itemName, unitPrice, description, category, vegetarian, glutenFree, atBirm, atSheff)
VALUES (?,?,?,?,?,?,?,?)',[@dishName,@dishCost,@dishDesc,@dishType,vegetarian,glutenFree,atSheff,atBirm])
else
redirect '/admin_edit_menu?errorAdding=1'
end
redirect '/admin_edit_menu'
end
post '/update_menu' do
@dishID = params[:dishID]
@dishName = params[:dishName].strip
@dishCost = params[:dishCost].strip
@dishDesc = params[:dishDesc].strip
@gFree = params[:gFree]
@dishVeggie = params[:dishVeggie]
@atSheff = params[:atSheff]
@atBirm = params[:atBirm]
dishName_ok = [email protected]? && @dishName != ""
dishCost_ok = [email protected]? && @dishCost != ""
dishDesc_ok = [email protected]? && @dishDesc != ""
gFree_ok = @gFree_ok == "1"|| @gFree_ok.nil?
dishVeggie_ok = @dishVeggie == "1" || @dishVeggie.nil?
atSheff_ok = @atSheff == "1" || @atSheff.nil?
atBirm_ok = @atBirm == "1" || @atBirm.nil?
if @gFree.nil?
@gFree == "0"
end
if @dishVeggie.nil?
@dishVeggie == "0"
end
if @atSheff.nil?
@atSheff == "0"
end
if @atBirm.nil?
@atBirm == "0"
end
all_ok = dishName_ok && dishCost_ok && dishDesc_ok && gFree_ok && dishVeggie_ok && atSheff_ok && atBirm_ok
if all_ok
exe = @db.execute('UPDATE menu SET itemName = ?, unitPrice = ?, description= ?, vegetarian = ?, glutenFree = ?, atBirm = ?, atSheff = ?
WHERE id = ?',[@dishName,@dishCost,@dishDesc,@dishVeggie,@gFree,@atBirm,@atSheff,@dishID])
redirect '/admin_edit_menu'
end
end