refactored missing link scenario to make more sense

+ 404 code, stay on the current page, and load the form
+ also simplified the logic for the create action if the link doesn't
  already exist
pull/18/head
Adam Townsend 2023-10-07 21:11:12 -07:00
parent c7fee27623
commit 5bce31baf2
1 changed files with 5 additions and 6 deletions

11
app.rb
View File

@ -19,11 +19,10 @@ class App < Roda
r.get String do | url_code |
link = links.filter(:code => url_code)
if link.first.nil?
flash['message'] = "Link #{url_code} doesn't exist"
r.redirect '/'
end
r.redirect link.first[:url]
r.redirect link.first[:url] unless link.first.nil?
@message = "Link #{url_code} doesn't exist"
response.status = 404
view :home
end
r.post "create" do
@ -32,7 +31,7 @@ class App < Roda
flash['message'] = "Please enter a valid URL";
r.redirect '/'
end
if nil == links.filter(:url => url).first
if links.filter(:url => url).first.nil?
code = SecureRandom.urlsafe_base64 4
links.insert(url: url, code: code)
@message = "Link created"