Refactor & stuff

master
E. Almqvist 3 years ago
parent 0834876425
commit de8c08353d
  1. 35
      wesweb01/crud-music-2020/app.rb
  2. BIN
      wesweb01/crud-music-2020/db/chinook-crud.db
  3. 0
      wesweb01/crud-music-2020/db/chinook.db
  4. 4
      wesweb01/crud-music-2020/views/albums/index.slim
  5. 4
      wesweb01/crud-music-2020/views/albums/new.slim
  6. 4
      wesweb01/crud-music-2020/views/albums/show.slim
  7. 3
      wesweb01/crud-music-2020/views/layout.slim

@ -17,24 +17,51 @@ require "sqlite3"
#6. Skapa funktionalitet för att uppdatera artistinformation
db = SQLite3::Database.new("db/chinook-crud.db")
db.results_as_hash = true
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")
p result
slim(:"albums/index", locals: {albums: result})
end
get "/albums/new" do
slim(:"albums/new")
end
post "/albums/new" do
db = SQLite3::Database.new("db/chinook-crud.db")
db.results_as_hash = true
title, art_id = params[:title], params[:artist_id].to_i
db.execute("INSERT INTO albums (Title, ArtistId) VALUES (?,?)", title, art_id)
redirect "/albums"
end
post "/albums/:id/delete" do
db = SQLite3::Database.new("db/chinook-crud.db")
db.results_as_hash = true
id = params[:id].to_i
db.execute("DELETE FROM albums WHERE AlbumId = ?", id)
redirect "/albums"
end
get "/albums/:id" do
db = SQLite3::Database.new("db/chinook-crud.db")
db.results_as_hash = true
id = params[:id].to_i
result = db.execute("SELECT * FROM albums WHERE ArtistId = ?", id).first
artist = db.execute("SELECT Name FROM artists WHERE ArtistId IN (SELECT ArtistId FROM Albums WHERE AlbumId = ?)", id).first
result = db.execute("SELECT * FROM albums WHERE AlbumId = ?", id).first
artist = db.execute("SELECT Name FROM artists WHERE ArtistId IN (SELECT ArtistId FROM albums WHERE AlbumId = ?)", id).first
slim(:"albums/show", locals: {result: result, artist: artist})
end

@ -5,6 +5,8 @@ ol
- albums.each do |album|
li
a href="albums/#{album['AlbumId']}" #{album["Title"]}
form action="albums/#{album['AlbumId']}/delete" method="post"
input type="submit" value="Remove"

@ -0,0 +1,4 @@
form action="/albums/new" method="post"
input type="text" name="title" placeholder="Album title"
input type="number" name="artist_id" placeholder="Artist ID"
input type="submit" value="Create album"

@ -1,5 +1,5 @@
h1 = result
h1 = result or ""
p Albumet som du har sökt efter heter #{result["Title"]}.
p Artisten som du har sökt efter heter #{result["ArtistId"]}.
// p Artisten som du har sökt efter heter #{result["ArtistId"]}.
p Artisten som du har sökt efter heter #{artist["Name"]}.

@ -12,5 +12,6 @@ html lang="en"
nav
a href="/" Start it
a href="/albums" Visa alla album
a href="/albums/new" New album
==yield

Loading…
Cancel
Save