From 05807cc520923a50ef92f6673c771d3973a64b6f Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 8 Dec 2021 13:18:48 +0100 Subject: [PATCH] Opti & reformat --- wesweb01/crud-music-2020/app.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/wesweb01/crud-music-2020/app.rb b/wesweb01/crud-music-2020/app.rb index 5d75b9e..e5ddb81 100755 --- a/wesweb01/crud-music-2020/app.rb +++ b/wesweb01/crud-music-2020/app.rb @@ -17,25 +17,24 @@ require "sqlite3" #6. Skapa funktionalitet för att uppdatera artistinformation +db_cache = SQLite3::Database.new("db/chinook-crud.db") # behöver inte skapa nya handlers varje gång vi ska "titta" i databasen +db_cache.results_as_hash = true +# Har därmed en "cache" get "/" do slim(:start) end get "/albums" do - db = SQLite3::Database.new("db/chinook-crud.db") - db.results_as_hash = true - result = db.execute("SELECT * FROM albums") + result = db_cache.execute("SELECT * FROM albums") p result slim( :"albums/index", locals: { albums: result } ) end get "/albums/:id" do id = params[:id].to_i - db = SQLite3::Database.new("db/chinook-crud.db") - db.results_as_hash = true - result = db.execute("SELECT * FROM albums WHERE ArtistId = ?",id).first - slim(:"albums/show",locals:{result:result}) + result = db_cache.execute("SELECT * FROM albums WHERE ArtistId = ?", id).first + slim( :"albums/show", locals: { result: result} ) end