From 181ba98e1ab733d272f75feb574e354a557da6e2 Mon Sep 17 00:00:00 2001 From: David Coleman Date: Mon, 8 Dec 2014 15:49:45 -0600 Subject: [PATCH 1/6] add songs page and add a song --- lib/songify.rb | 2 +- lib/songify/album_repo.rb | 4 ++++ lib/songify/song_repo.rb | 6 +++++- server.rb | 17 ++++++++++++++++- views/index.erb | 1 + 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/songify.rb b/lib/songify.rb index 90548c82..63a55233 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -22,7 +22,7 @@ def self.create_tables(db) ); CREATE TABLE songs( id SERIAL PRIMARY KEY, - album_id integer REFERENCES genres (id), + album_id integer REFERENCES albums (id), title VARCHAR ); CREATE TABLE genres( diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..cdde0129 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -11,6 +11,10 @@ def self.find(db, album_id) db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first end + def self.find_id(db, album_title) + db.exec("SELECT * FROM albums WHERE title=$1", [album_title]).first + end + def self.save(db, album_data) if album_data['id'] result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index a9fc10c0..0e138660 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -11,6 +11,10 @@ def self.find(db, song_id) db.exec("SELECT * FROM songs WHERE id=$1", [song_id]).first end + def self.find_id(db, song_title) + db.exec("SELECT * FROM songs WHERE title=$1", [song_title]).first + end + def self.save(db, song_data) if song_data['id'] result = db.exec("UPDATE songs SET title = $2 WHERE id = $1", [song_data['id'], song_data['title']]) @@ -22,7 +26,7 @@ def self.save(db, song_data) album = AlbumRepo.find(db, song_data['album_id']) raise "A valid album_id is required." if album.nil? - result = db.exec("INSERT INTO songs (title) VALUES ($1) RETURNING id", [song_data['title']]) + result = db.exec("INSERT INTO songs (title, album_id) VALUES ($1, $2) RETURNING id", [song_data['title'], album['id']]) song_data['id'] = result.entries.first['id'] song_data end diff --git a/server.rb b/server.rb index 361bacc7..345aac08 100644 --- a/server.rb +++ b/server.rb @@ -1,7 +1,7 @@ require 'sinatra' require './lib/songify.rb' -# set :bind, '0.0.0.0' # This is needed for Vagrant +set :bind, '0.0.0.0' # This is needed for Vagrant get '/' do erb :index @@ -23,9 +23,24 @@ get '/songs' do + db = Songify.create_db_connection('songify_dev') + @albums = Songify::AlbumRepo.all(db) + @songs = Songify::SongRepo.all(db) erb :"songs/index" end +post '/songs' do + db = Songify.create_db_connection('songify_dev') + + album_title = params[:album_title] + album_id = Songify::AlbumRepo.find_id(db, album_title) + song = Songify::SongRepo.save(db, { + 'title' => params[:title], + 'album_id' => album_id['id'] + }) + redirect to '/songs' +end + get '/genres' do db = Songify.create_db_connection('songify_dev') diff --git a/views/index.erb b/views/index.erb index 8f52ed98..686e6a56 100644 --- a/views/index.erb +++ b/views/index.erb @@ -5,4 +5,5 @@ From b471df05e094287de6c8db3720fd21e5437e95c9 Mon Sep 17 00:00:00 2001 From: David Coleman Date: Mon, 8 Dec 2014 16:08:45 -0600 Subject: [PATCH 2/6] add a song total to the albums page --- lib/songify/song_repo.rb | 8 ++++++++ server.rb | 3 +++ views/albums/index.erb | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index 0e138660..bc356d34 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -32,5 +32,13 @@ def self.save(db, song_data) end end + def self.songs_per_album(db, album_id) + db.exec("SELECT * FROM songs WHERE album_id=$1", [album_id]) + end + + + + + end end diff --git a/server.rb b/server.rb index 345aac08..cb44e5cb 100644 --- a/server.rb +++ b/server.rb @@ -10,6 +10,9 @@ get '/albums' do db = Songify.create_db_connection('songify_dev') @albums = Songify::AlbumRepo.all(db) + @albums.each do |x| + x['count'] = Songify::SongRepo.songs_per_album(db, x['id']).count + end erb :"albums/index" end diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..21efe646 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -2,11 +2,15 @@

All Albums

+ +

New Album

From 08af40b796daae446d051ea1ba0687f8a5e2ffa1 Mon Sep 17 00:00:00 2001 From: David Coleman Date: Mon, 8 Dec 2014 16:26:21 -0600 Subject: [PATCH 3/6] add album show page with song list --- lib/songify.rb | 2 +- server.rb | 7 +++++++ views/albums/index.erb | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/songify.rb b/lib/songify.rb index 63a55233..9245c400 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -7,8 +7,8 @@ def self.create_db_connection(dbname) def self.clear_db(db) db.exec <<-SQL - DELETE FROM albums; DELETE FROM songs; + DELETE FROM albums; DELETE FROM genres; /* TODO: Clear rest of the tables (books, etc.) */ SQL diff --git a/server.rb b/server.rb index cb44e5cb..82ceaf18 100644 --- a/server.rb +++ b/server.rb @@ -24,6 +24,13 @@ redirect to '/albums' end +get '/albums/:id' do + db = Songify.create_db_connection('songify_dev') + @album = Songify::AlbumRepo.find(db, params[:id]) + @songs = Songify::SongRepo.songs_per_album(db, params[:id]).to_a + erb :"albums/show" +end + get '/songs' do db = Songify.create_db_connection('songify_dev') diff --git a/views/albums/index.erb b/views/albums/index.erb index 21efe646..be594be0 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -4,7 +4,8 @@
  • Album Title : song count
  • <% @albums.each do |album| %> -
  • <%= album['title'] %> : <%= album['count'] %>
  • + <% path = "/albums/#{album['id']}" %> +
  • ><%= album['title'] %> : <%= album['count'] %>
  • <% end %>
  • <%= @songs %>
From a3bf0f438bb042ee0e9191459f910825bbb2e911 Mon Sep 17 00:00:00 2001 From: David Coleman Date: Mon, 8 Dec 2014 21:15:56 -0600 Subject: [PATCH 4/6] add table for albums_genres and update save and find methods for albumrepo --- lib/songify.rb | 12 +++++++++--- lib/songify/album_repo.rb | 20 +++++++++++++++++++- server.rb | 4 +++- spec/repos/album_repo_spec.rb | 2 +- views/albums/index.erb | 6 ++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/songify.rb b/lib/songify.rb index 9245c400..b746c5dd 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -8,6 +8,7 @@ def self.create_db_connection(dbname) def self.clear_db(db) db.exec <<-SQL DELETE FROM songs; + DELETE FROM albums_genres; DELETE FROM albums; DELETE FROM genres; /* TODO: Clear rest of the tables (books, etc.) */ @@ -16,19 +17,24 @@ def self.clear_db(db) def self.create_tables(db) db.exec <<-SQL - CREATE TABLE albums( + CREATE TABLE if not exists albums( id SERIAL PRIMARY KEY, title VARCHAR ); - CREATE TABLE songs( + CREATE TABLE if not exists songs( id SERIAL PRIMARY KEY, album_id integer REFERENCES albums (id), title VARCHAR ); - CREATE TABLE genres( + CREATE TABLE if not exists genres( id SERIAL PRIMARY KEY, name VARCHAR ); + CREATE TABLE if not exists albums_genres( + id SERIAL PRIMARY KEY, + album_id integer REFERENCES albums (id), + genre_id integer REFERENCES genres (id) + ); /* TODO: Create song_genres table */ SQL end diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index cdde0129..977a06e4 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -8,7 +8,19 @@ def self.all(db) end def self.find(db, album_id) - db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + response = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + # result = db.exec("SELECT * FROM albums_genres WHERE album_id = $1", [album_id]) + # final = {} + result = db.exec("SELECT g.name FROM albums_genres x JOIN genres g ON x.genre_id = g.id JOIN albums a ON x.album_id = a.id WHERE x.album_id = $1", [album_id]).to_a + if result[0] + final_genre = [] + result.each do |x| + final_genre << x + end + response['genres'] = final_genre + return response + end + response end def self.find_id(db, album_title) @@ -24,7 +36,13 @@ def self.save(db, album_data) result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) album_data['id'] = result.entries.first['id'] album_data + if album_data['genre_ids'] + album_data['genre_ids'].each do |genre| + db.exec("INSERT INTO albums_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], genre]) + end + end end + album_data end def self.destroy(db, album_id) diff --git a/server.rb b/server.rb index 82ceaf18..4fb7efb8 100644 --- a/server.rb +++ b/server.rb @@ -13,13 +13,15 @@ @albums.each do |x| x['count'] = Songify::SongRepo.songs_per_album(db, x['id']).count end + @genres = Songify::GenreRepo.all(db) erb :"albums/index" end post '/albums' do db = Songify.create_db_connection('songify_dev') album = Songify::AlbumRepo.save(db, { - 'title' => params[:title] + 'title' => params[:title], + 'genres' => params[:genre_ids] }) redirect to '/albums' end diff --git a/spec/repos/album_repo_spec.rb b/spec/repos/album_repo_spec.rb index 3f080f05..931ebde1 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -45,7 +45,7 @@ def album_count } end - xit "can be assigned genres" do + it "can be assigned genres" do gid_1 = Songify::GenreRepo.save(db, { 'name' => 'rock' }) gid_2 = Songify::GenreRepo.save(db, { 'name' => 'avant-garde' }) gid_3 = Songify::GenreRepo.save(db, { 'name' => 'jazz' }) diff --git a/views/albums/index.erb b/views/albums/index.erb index be594be0..f85a7fee 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -16,5 +16,11 @@

New Album

+ + add another From f38e8d4219c137692f0d53c3be8f572e7bc431c5 Mon Sep 17 00:00:00 2001 From: David Coleman Date: Tue, 9 Dec 2014 12:26:21 -0600 Subject: [PATCH 5/6] add jquery to add multiple genres to an alubm --- views/albums/index.erb | 48 ++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/views/albums/index.erb b/views/albums/index.erb index f85a7fee..c4e02f23 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -7,20 +7,42 @@ <% path = "/albums/#{album['id']}" %>
  • ><%= album['title'] %> : <%= album['count'] %>
  • <% end %> -
  • <%= @songs %>
  • + -
    -

    New Album

    - - - - add another - -
    +
    +

    New Album

    + + + + + + add genre + + +
    + + + + + + From 50d02407b8824aea7ce73a5d2ed49e187381510d Mon Sep 17 00:00:00 2001 From: David Coleman Date: Tue, 9 Dec 2014 14:43:34 -0600 Subject: [PATCH 6/6] change divs in album page for easier access, add remove jquery --- views/albums/index.erb | 46 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/views/albums/index.erb b/views/albums/index.erb index c4e02f23..3cff9280 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -11,22 +11,27 @@ - +

    New Album

    +
    - + +
    add genre - + +
    +
    +
    @@ -34,15 +39,38 @@ $(document).on('click', 'a.add-genre', function(e) { e.preventDefault() - $select = $("[name='genre_ids[]'").last() - $placement = $('a.add-genre') - // $genre = $("[name='genre']") - // $select.clone().before($placement) - // $placement. - $placement.before($select.clone()) + $select = $("div.genre").last() + $br = $('
    ') + $remove = $('').attr('href', '#').addClass('remove-genre').text('remove genre') + $remove.append($br) + $div = $('div.genre').last() + $placement = $('a.add-genre') + if ($('a.remove-genre').length === 0) { + $div.append($remove) + + // $genre = $("[name='genre']") + // $select.clone().before($placement) + // $placement. + $placement.before($div.clone()) + } else { + $placement.before($div.clone()) + } }) +$(document).on('click', 'a.remove-genre', function(e) { + e.preventDefault() + $remove = $("div.genre").last() + // this.remove() + // this.remove() + $remove.remove() + this.remove() + if ($('a.remove-genre').length === 1) { + $rere = $('a.remove-genre') + $rere.remove() + } +}) +