-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
179 lines (132 loc) · 3.87 KB
/
main.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
require 'sinatra'
require 'sinatra/reloader' if development?
require 'pg'
require 'pry' if development?
# if development?
# require sinatra/reloader
# require pry
# end
require_relative 'models/defect'
require_relative 'models/engineer'
require_relative 'models/reports'
require_relative 'lib'
enable :sessions # plural - this is a sinatra feature
get '/' do
# how about show current defect summary at the start. The users to login to see more detail
defect_summary = defect_summary()
#puts defect_summary.to_a
erb(:index, locals: {
defect_summary: defect_summary
})
end
get '/defects' do
# Want to show all defects once logged in
#1. route to login if the user is not logged in
redirect "/login" unless logged_in?
#2 if login is okay, obtain all defects
all_defects = all_defects()
erb(:show, locals: {
all_defects: all_defects
})
end
get '/login' do
erb(:login)
end
post '/login' do
# After a user submits their login details, we need to verify them.
# 1. obtain the current engineer's email address who is attempting to login
engineer = find_one_engineer_by_email( params[:email] )
puts(engineer)
#2
if engineer && BCrypt::Password.new(engineer["password_digest"]) == params[:password]
session[:eng_id] = engineer['eng_id'] # write down that this user is logged in
redirect "/defects"
else
erb :login
end
end
# This allows an engineer to utilise a form to submit a new defect
get '/defects/new' do
erb(:new)
end
# here, we are going to insert a new defects
post '/defects' do
# guard condition
redirect "/login" unless logged_in?
params[:status] = 'New'
create_new_defect(params[:defect_title], params[:description], params[:status], current_engineer['eng_id'])
redirect "/defects"
end
get '/defects/:defect_id' do
defect = find_one_defect_by_id(params[:defect_id])
engineer = find_one_engineer_by_id(defect["eng_id"])
erb(:showdefect, locals: { defect: defect, engineer: engineer} )
end
get '/defects/:defect_id/edit' do
defect = find_one_defect_by_id(params[:defect_id])
engineer = find_one_engineer_by_id(defect["eng_id"])
erb(:edit, locals: { defect: defect, engineer: engineer })
end
# here, we are updating defect itself
patch '/defects' do
update_defect(
params[:defect_id],
params[:defect_title],
params[:description],
params[:status],
session['eng_id']
)
redirect "/defects/#{params[:defect_id]}"
end
delete '/defects' do
delete_defect(params[:defect_id])
redirect "/defects"
end
delete '/logout' do
# 1 its crud - destroy - what?
session[:eng_id] = nil
# 2 not a safe method so redirect to a get
# redirect to where???
# login page may be????
redirect "/login"
end
get '/engineers' do
#1. route to login if the user is not logged in
redirect "/login" unless logged_in?
engineers = all_engineers()
erb(:'/engineers/show', locals: {engineers: engineers})
end
# this is the route to the engineers sign up page
get '/engineers/new' do
erb(:'/engineers/new')
end
post '/engineers' do
create_engineer(params[:name], params[:email], params[:password], params[:role])
redirect "/engineers"
end
get '/engineers/:eng_id' do
#1. route to login if the user is not logged in
# redirect "/login" unless logged_in?
engineer = find_one_engineer_by_id(params[:eng_id])
erb(:'/engineers/showEngineer', locals: { engineer: engineer} )
end
get '/engineers/:eng_id/edit' do
engineer = find_one_engineer_by_id(params[:eng_id])
erb(:'/engineers/edit', locals: { engineer: engineer })
end
patch '/engineers' do
update_engineer(
params[:eng_id],
params[:name],
params[:email],
params[:role]
#session['eng_id']
)
redirect "/engineers/#{params[:eng_id]}"
end
get '/reports' do
#1. route to login if the user is not logged in
redirect "/login" unless logged_in?
velocity_chart = defect_summary()
erb(:'/reports/show')
end