-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
105 lines (87 loc) · 2.16 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
require("bundler/setup")
Bundler.require(:default)
also_reload("lib/**/*.rb")
Dir[File.dirname(__FILE__) + '/lib/*.rb'].each { |file| require file } #loading individual files when required
#loads first web page 'index'
get("/") do
erb(:index)
end
get("/top") do
@brands = Brand.all()
@stores = Store.all()
erb(:top)
end
get("/brands") do
@brands = Brand.all()
erb(:brands)
end
get("/stores") do
@stores = Store.all()
erb(:stores)
end
get("/stores/new") do
erb(:stores_form)
end
get("/brands/new") do
erb(:brand_form)
end
get('/stores/:id') do
@brands = Brand.all()
@store = Store.find(params.fetch("id").to_i())
erb(:store_details)
end
get('/brands/:id') do
@stores = Store.all()
@brand= Brand.find(params.fetch("id").to_i())
erb(:brand_details)
end
get("/stores/:id/edit") do
@store = Store.find(params.fetch("id").to_i())
erb(:store_update)
end
get("/brands/:id/edit") do
@brand = Brand.find(params.fetch("id").to_i())
erb(:brand_update)
end
post("/stores") do
name = params.fetch("name")
store= Store.new({:name => name, :id => nil})
if store.save()
redirect("/stores/".concat(store.id().to_s())) #implemented redirecting functionality
else
erb(:errors) #catching errors
end
end
post("/brands") do
name = params.fetch("name")
brand = Brand.new({:name => name, :id => nil})
if brand.save()
redirect("/brands/".concat(brand.id().to_s()))
else
erb(:errors) #catching errors
end
end
patch("/stores/:id") do
store_id = params.fetch("id").to_i()
@store = Store.find(store_id)
brand_id = params.fetch("brand_id")
@store.update({:brand_ids => brand_id})
@brands = Brand.all()
redirect("/stores")
end
delete("/stores/:id") do
@store = Store.find(params.fetch("id").to_i())
if @store.destroy()
redirect("/stores")
else
erb(:errors)
end
end
delete("/brands/:id") do
@brand = Brand.find(params.fetch("id").to_i())
if @brand.destroy()
redirect("/brands")
else
erb(:errors)
end
end