-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
78 lines (60 loc) · 1.3 KB
/
Copy pathapp.rb
File metadata and controls
78 lines (60 loc) · 1.3 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
# encoding: utf-8
require 'rubygems'
require 'sinatra'
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/activerecord'
require 'rackup'
configure :development do
# set :port, 5433
set :database, { adapter: 'postgresql', encoding: 'unicode', database: 'pizzaShop', pool: 2, username: 'postgres', password: 'root', port: '5433' }
end
class Order < ActiveRecord::Base
end
class Product < ActiveRecord::Base
end
get '/' do
@products = Product.all
erb :index
end
get '/about' do
erb :about
end
get '/admin' do
@orders = Order.order('created_at DESC')
erb :admin
end
post '/cart' do
@orders_input = params[:orders_input]
@items = parse_orders_input @orders_input
if @items.length == 0
return erb :cart_is_empty
end
@items.each do |item|
item[0] = Product.find(item[0])
end
erb :cart
end
post '/place_order' do
@order = Order.create params[:order]
erb :order_placed
# if @order.save
# erb "<h2>Thank you! You order has benn placed</h2>"
# else
# @error = @order.errors.filter.first
# erb "Error"
# end
end
def parse_orders_input (orders_input)
s1 = orders_input.split(/,/)
arr = []
s1.each do |x|
s2 = x.split(/=/)
s3 = s2[0].split(/_/)
id = s3[1]
cnt = s2[1]
arr2 = [id, cnt]
arr.push arr2
end
return arr
end