Skip to content

Commit d0d2093

Browse files
committed
Make record exists before call methods on it.
1 parent 7a0e24e commit d0d2093

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

server.gb

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,47 @@ server.put('/items/{id:[0-9]+}') do |req, res|
5757
if params.nil? || params[:title].nil?
5858
res.status = 400
5959
else
60-
title = params[:title]
61-
ListItem.find(req.params[:id]).update_title(title)
62-
res.status = 200
60+
record = ListItem.find(req.params[:id])
61+
if record
62+
record.update_title(params[:title])
63+
res.status = 200
64+
else
65+
res.status = 404
66+
end
6367
end
6468
end
6569

6670
# PUT Check Action
6771
server.put('/items/{id:[0-9]+}/check') do |req, res|
68-
ListItem.find(req.params[:id]).check
69-
res.status = 200
72+
record = ListItem.find(req.params["id"])
73+
if record
74+
record.check
75+
res.status = 200
76+
else
77+
res.status = 404
78+
end
7079
end
7180

7281
# PUT Check Action
7382
server.put('/items/{id:[0-9]+}/uncheck') do |req, res|
74-
ListItem.find(req.params[:id]).uncheck
75-
res.status = 200
83+
record = ListItem.find(req.params["id"])
84+
if record
85+
record.uncheck
86+
res.status = 200
87+
else
88+
res.status = 404
89+
end
7690
end
7791

7892
# DELETE Delete Action
7993
server.delete('/items/{id:[0-9]+}') do |req, res|
80-
ListItem.find(req.params["id"]).destroy
81-
res.status = 200
94+
record = ListItem.find(req.params["id"])
95+
if record
96+
record.destroy
97+
res.status = 200
98+
else
99+
res.status = 404
100+
end
82101
end
83102

84103
server.start

0 commit comments

Comments
 (0)