-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
281 lines (253 loc) · 6.57 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
require 'sinatra'
require 'sinatra/contrib/all'
require 'data_mapper'
require 'sinatra/authorization'
require 'will_paginate'
require 'will_paginate/data_mapper'
require './models'
require './custom_helpers'
Refrigerator.rebuild_options
Washingmachine.rebuild_options
Airconditioner.rebuild_options
Dishwasher.rebuild_options
helpers do
def authorization_realm
settings.authorization_realm
end
def authorize(username, password)
username=="admin" && password=="simbhashinybecky"
end
def get_klass(params)
if params[:item]
klass = singular2class params[:item]
elsif params[:items]
klass = plural2class params[:items]
end
end
end
set :authorization_realm, "Protected zone"
helpers CustomHelpers
enable :sessions
# Get the home page
#
get '/' do
@items = {}
@items['refrigerators'] = Refrigerator.all :conditions => ["topselling=? or valueformoney=? or toprange=?", true, true, true]
@items['washingmachines'] = Washingmachine.all :conditions => ["topselling=? or valueformoney=? or toprange=?", true, true, true]
@items['airconditioners'] = Airconditioner.all :conditions => ["topselling=? or valueformoney=? or toprange=?", true, true, true]
@items['dishwashers'] = Dishwasher.all :conditions => ["topselling=? or valueformoney=? or toprange=?", true, true, true]
@categories = categories
erb :home
end
def get_items_with_filter(params)
klass = get_klass params
options = klass.options
items = klass.all
if params[:property]==:brand
items = items.all(:brand => params[:value])
end
if ! params['property'].nil?
items = klass.all( params[:property] => params[:value] ).paginate(:page=>params[:page], :per_page=>8)
else
items =klass.all.paginate(:page=>params[:page], :per_page=>8)
end
item_category = params[:items]
[options, items, item_category]
end
# Show items on display and individual items
#
get '/showcase/:items' do
@options, @items, @item_category = get_items_with_filter params
if request.xhr?
erb :items , :layout => false
else
erb :products
end
end
get '/showcase/:item/:id' do
klass = get_klass params
@item = klass.get params[:id]
@item_category = params[:item]
erb :item
end
# Admin home (Dashboard) and admin show items list with filter
get '/admin/home' do
login_required
erb :admin_home
end
get '/admin/appliances/:items' do
login_required
@options, @items, @item_category = get_items_with_filter params
print @items.inspect
@admin = true
erb :products
end
# Admin individual items new, create, edit, update, show, delete
#
def extract_item_params(klass, params)
data = {}
klass.properties.each { |p| data[p.name.to_s]=params[p.name.to_s] if params.has_key?( p.name.to_s ) }
data['item'] = singular2class data['item']
data
end
get '/admin/appliances/:item/new' do
login_required
@cat = params[:item]
klass = get_klass params
@item = klass.new
@brands = Brand.all
erb :new_item
end
post '/admin/appliances/:item/create' do
login_required
@cat = params[:item]
klass = get_klass params
@item = klass.create (extract_item_params klass, params)
if @item.saved?
redirect to("/showcase/#{@cat}/#{@item.id}")
else
@brands = Brand.all
erb :new_item
end
end
delete '/admin/appliances/:item/destroy/:id' do
login_required
@cat = params[:item]
klass = get_klass params
item = klass.get params[:id]
item.destroy
json :success => true
end
get '/admin/appliances/:item/edit/:id' do
login_required
@cat = params[:item]
klass = get_klass params
@item = klass.get params[:id]
@brands = Brand.all
erb :edit_item
end
post '/admin/appliances/:item/update/:id' do
login_required
@cat = params[:item]
klass = get_klass params
@item = klass.get params[:id]
@item.attributes = extract_item_params klass, params
@item.save
if @item.saved?
redirect to("/showcase/#{@cat}/#{@item.id}")
else
erb url("/admin/#{@cat}/edit/#{@item.id}")
end
end
# Admin individual brands new, create, edit, update, index, delete
#
get '/admin/new/brand' do
login_required
@brand = Brand.new
erb :new_brand
end
post '/admin/create/brand' do
login_required
@brand = Brand.create params
if @brand.saved?
redirect to('/admin/brands')
else
erb :edit_brand
end
end
delete '/admin/destroy/brand/:id' do
login_required
brand = Brand.get params[:id]
if brand.appliances.count() == 0
brand.destroy
json :success => true
else
json :success => false, :msg => "You still have valid products, remove them first"
end
end
get '/admin/edit/brand/:id' do
login_required
@brand = Brand.get params[:id]
erb :edit_brand
end
post '/admin/update/brand/:id' do
login_required
@brand = Brand.get params[:id]
@brand.attributes = {name:params[:name]}
if @brand.save
redirect to('/admin/brands')
else
erb url("/admin/brand/edit/#{@brand.id}")
end
end
get '/admin/brands' do
login_required
@brands = Brand.all
@brands.each do |brand|
brand.appliances_count = brand.appliances.count()
end
erb :brands
end
# Shopping cart
#
def add_to_cart(params)
@appliance = Appliance.get params[:id]
session[:cart]||={}
if session[:cart].has_key? @appliance.id
new_number = session[:cart][@appliance.id][:numbers]+1
session[:cart][@appliance.id] = {numbers:new_number}.merge( {item_type:(@appliance.item.inspect).downcase, model_name:@appliance.model_number, price:@appliance.price, brand:@appliance.brand.name, appliance_id:@appliance.id, added_at:Time.now.to_s})
else
session[:cart][@appliance.id] = {numbers:1, item_type:(@appliance.item.inspect).downcase, model_name:@appliance.model_number, price:@appliance.price, brand:@appliance.brand.name, appliance_id:@appliance.id, added_at:Time.now.to_s}
end
end
post '/buy_now' do
add_to_cart params
redirect_to '/checkout'
end
get '/cart' do
session[:cart]||={}
cart = session[:cart].values
json cart_items:cart
end
get '/clearcart' do
session[:cart]= {}
json success:true
end
post '/cart/add' do
add_to_cart params
json success:true
end
delete '/cart/remove' do
@appliance = Appliance.get params[:id]
session[:cart]||={}
if session[:cart].has_key? @appliance.id
if session[:cart][@appliance.id][:numbers] > 1
session[:cart][@appliance.id][:numbers] -= 1
else
session[:cart].delete @appliance.id
end
json :success => true
else
json :success => false
end
end
# Checkout
#
get '/checkout' do
erb :checkout
end
get '/delivery_confirmed/:id'do
@delivery = Delivery.get params[:id]
erb :delivery_confirmed, :layout => false
end
post '/checkout' do
cart_items = session[:cart] || []
data = params["delivery"]
@delivery = Delivery.save_delivery(data, cart_items )
if @delivery.saved?
session[:cart] = nil
redirect to( "/delivery_confirmed/#{@delivery.id}")
else
redirect to('/checkout')
end
end