From 5073039d084abea1dcd79a982fae70fafd722007 Mon Sep 17 00:00:00 2001 From: charles milam Date: Mon, 8 Dec 2014 15:46:34 -0600 Subject: [PATCH 01/19] Modify for current version of Ruby. --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index cc9b3f83..1e4d1192 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.0.0' +ruby '2.1.3' gem 'rspec', '~> 2.14.1' gem 'pry-byebug' From cf1a083051bca7b8a92fface5c4f222424941457 Mon Sep 17 00:00:00 2001 From: charles milam Date: Mon, 8 Dec 2014 15:46:51 -0600 Subject: [PATCH 02/19] Modify to include .gems --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e941da68..60f8eafa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .bundle vendor/bundle .DS_Store +.gems From b5629b3d7bc4a508990d5ccc350f1563bef08c0f Mon Sep 17 00:00:00 2001 From: charles milam Date: Mon, 8 Dec 2014 15:56:07 -0600 Subject: [PATCH 03/19] Initial commit. --- lib/songify.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/songify.rb b/lib/songify.rb index 63a55233..5990112b 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -2,7 +2,7 @@ module Songify def self.create_db_connection(dbname) - PG.connect(host: 'localhost', dbname: dbname) + PG.connect(dbname: dbname) end def self.clear_db(db) From f25d33e3e3528597d63d0a9394e5bc1507063d0e Mon Sep 17 00:00:00 2001 From: charles milam Date: Mon, 8 Dec 2014 20:59:39 -0600 Subject: [PATCH 04/19] Add endpoints get and post for /songs. --- server.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server.rb b/server.rb index 361bacc7..64259544 100644 --- a/server.rb +++ b/server.rb @@ -21,11 +21,20 @@ redirect to '/albums' end - get '/songs' do + db = Songify.create_db_connection("songify_dev") + @albums = Songify::AlbumRepo.all(db) + erb :"songs/index" end +post '/songs' do + db = Songify.create_db_connection("songify_dev") + puts params.inspect + Songify::SongRepo.save(db, {"title" => params[:title], "album_id" => params[:album_id]}) + + redirect back +end get '/genres' do db = Songify.create_db_connection('songify_dev') From aae13778181dae73e22ac52f8a94a2c3429b8ed9 Mon Sep 17 00:00:00 2001 From: charles milam Date: Mon, 8 Dec 2014 21:00:09 -0600 Subject: [PATCH 05/19] Initial commit. --- views/layout.erb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 views/layout.erb diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 00000000..fdc92a27 --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,20 @@ + + + + Songify + + + + +<%= yield %> +


+ + \ No newline at end of file From 306a2bc4405dca854661082f8b9c2ddbd4a62f3c Mon Sep 17 00:00:00 2001 From: charles milam Date: Mon, 8 Dec 2014 21:00:32 -0600 Subject: [PATCH 06/19] Initial commit. --- views/songs/index.erb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 views/songs/index.erb diff --git a/views/songs/index.erb b/views/songs/index.erb new file mode 100644 index 00000000..1f0230d8 --- /dev/null +++ b/views/songs/index.erb @@ -0,0 +1,16 @@ +<- Back to Everything +
+
+

Add a Song

+ + + + + + +
+
\ No newline at end of file From a5cc8849244f77972e08817dd230ec55234ecf39 Mon Sep 17 00:00:00 2001 From: charles milam Date: Mon, 8 Dec 2014 21:01:31 -0600 Subject: [PATCH 07/19] Add link to songs page. Modify wordage for other links. --- views/index.erb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/views/index.erb b/views/index.erb index 8f52ed98..80328472 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,8 +1,11 @@ -

The Songify System

+
+

The Songify System

-

You're gonna be big

+

You're gonna be big

- + +
From c64081c8e68c82fdf499ef0066e3e8caf11253a3 Mon Sep 17 00:00:00 2001 From: charles milam Date: Mon, 8 Dec 2014 21:09:56 -0600 Subject: [PATCH 08/19] Modify method clear to clear song_genres table. Modify so all tables cascade their deletes. --- lib/songify.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/songify.rb b/lib/songify.rb index 5990112b..43b56ea1 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -7,10 +7,10 @@ def self.create_db_connection(dbname) def self.clear_db(db) db.exec <<-SQL - DELETE FROM albums; - DELETE FROM songs; - DELETE FROM genres; - /* TODO: Clear rest of the tables (books, etc.) */ + DELETE FROM albums cascade; + DELETE FROM songs cascade; + DELETE FROM genres cascade; + delete from song_genres cascade; SQL end @@ -29,16 +29,20 @@ def self.create_tables(db) id SERIAL PRIMARY KEY, name VARCHAR ); - /* TODO: Create song_genres table */ + create table song_genres( + id serial primary key, + album_id integer references albums (id), + genres_id integer references genres (id) + ) SQL end def self.drop_tables(db) db.exec <<-SQL - DROP TABLE albums; - DROP TABLE songs; - DROP TABLE genres; - /* TODO: Drop song_genres table */ + DROP TABLE if exists albums; + DROP TABLE if exists songs; + DROP TABLE if exists genres; + drop table if exists song_genres; SQL end end From 3e9a9b0c8ce69689f4cebdea7a65d6e1c3a74baf Mon Sep 17 00:00:00 2001 From: charles milam Date: Tue, 9 Dec 2014 00:07:34 -0600 Subject: [PATCH 09/19] Attempt to pass test. --- lib/songify/album_repo.rb | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..581491a9 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -8,17 +8,50 @@ def self.all(db) end def self.find(db, album_id) - db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + db.exec("SELECT * FROM albums WHERE id = $1", [album_id]).first + end + + def self.find_genres(db, album_id) + sql = %Q[ + SELECT + albums.title, + genres.name + FROM + album_genres, + genres, + albums + WHERE + album_genres.genres_id = genres.id AND + album_genres.album_id = $1 + ] + result = db.exec(sql, [album_id]) + result.each {|r| p r} + puts result.flatten end def self.save(db, album_data) + #puts album_data if album_data['id'] result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) self.find(db, album_data['id']) else raise "title is required." if album_data['title'].nil? || album_data['title'] == '' result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) + #puts "album result", result.entries.first album_data['id'] = result.entries.first['id'] + + if album_data["genre_ids"] + sql = %Q[ + insert into album_genres + (album_id, genres_id) + values ($1, $2) + ] + album_data["genre_ids"].each do |g_id| + p album_data["id"].to_i, g_id.to_i + db.exec(sql, [album_data["id"].to_i, g_id.to_i]) + end + end + album_data end end From 9f72ceea9950e03ef42b5f6031da7464bda47d9f Mon Sep 17 00:00:00 2001 From: charles milam Date: Tue, 9 Dec 2014 00:07:47 -0600 Subject: [PATCH 10/19] Testing of genres. --- spec/repos/album_repo_spec.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/repos/album_repo_spec.rb b/spec/repos/album_repo_spec.rb index 3f080f05..2fa60a2a 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -45,14 +45,15 @@ 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' }) album = repo.save(db, { 'title' => 'Suspicious Activity?', 'genre_ids' => [gid_1['id'], gid_2['id'], gid_3['id']] }) - album = repo.find(db, album['id']) + album = repo.find_genres(db, album['id']) + puts album.first expect(album['genres'].count).to eq 3 names = album['genres'].map {|g| g['name'] } From 2b73b54a52e99ce575d8a6fb926798642c9d8c25 Mon Sep 17 00:00:00 2001 From: charles milam Date: Tue, 9 Dec 2014 12:49:13 -0600 Subject: [PATCH 11/19] Modify find to work with albums with genres. --- lib/songify/album_repo.rb | 43 ++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 581491a9..136b4b9a 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -8,25 +8,31 @@ def self.all(db) end def self.find(db, album_id) - db.exec("SELECT * FROM albums WHERE id = $1", [album_id]).first - end + result = db.exec("SELECT * FROM albums WHERE id = $1", [album_id]).first + + if result + genres = [] + genre_result = {} + sql = %Q[ + SELECT + albums.title, + genres.name + FROM + album_genres, + genres, + albums + WHERE + album_genres.genres_id = genres.id AND + album_genres.album_id = $1 + ] + genre_result = db.exec(sql, [album_id]) - def self.find_genres(db, album_id) - sql = %Q[ - SELECT - albums.title, - genres.name - FROM - album_genres, - genres, - albums - WHERE - album_genres.genres_id = genres.id AND - album_genres.album_id = $1 - ] - result = db.exec(sql, [album_id]) - result.each {|r| p r} - puts result.flatten + genre_result.each do |r| + genres << {'id' => r['genre_id'], 'name' => r['name']} + end + result['genres'] = genres + end + result end def self.save(db, album_data) @@ -47,7 +53,6 @@ def self.save(db, album_data) values ($1, $2) ] album_data["genre_ids"].each do |g_id| - p album_data["id"].to_i, g_id.to_i db.exec(sql, [album_data["id"].to_i, g_id.to_i]) end end From 920364dda84201d70f697f926f7a825c8c18a816 Mon Sep 17 00:00:00 2001 From: charles milam Date: Tue, 9 Dec 2014 14:25:34 -0600 Subject: [PATCH 12/19] Add select tag for genre list. --- views/albums/index.erb | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..3f200ad4 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,15 +1,25 @@ <- Back to Everything -

All Albums

+
+

All Albums

-
    - <% @albums.each do |album| %> -
  • <%= album['title'] %>
  • - <% end %> -
+
    + <% @albums.each do |album| %> +
  • <%= album['title'] %>
  • + <% end %> +
-
-

New Album

- - - -
+
+

New Album

+ + + + +    add another genre +
+ +
+
From 009d600bd6627863bb5e4593a7a8b18135e49c15 Mon Sep 17 00:00:00 2001 From: charles milam Date: Tue, 9 Dec 2014 14:39:39 -0600 Subject: [PATCH 13/19] Modify get /albums to include instance variable for genres. --- views/albums/index.erb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/views/albums/index.erb b/views/albums/index.erb index 3f200ad4..731192e9 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -23,3 +23,11 @@ + + From 888fc52e10fdd3c0e72a3751b26721555d8f7eb2 Mon Sep 17 00:00:00 2001 From: charles milam Date: Tue, 9 Dec 2014 15:30:26 -0600 Subject: [PATCH 14/19] Add script to trigger additional genre selectors, anchor on click event. --- views/albums/index.erb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/views/albums/index.erb b/views/albums/index.erb index 731192e9..ca22a068 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -13,7 +13,7 @@ - <% @genres.each do |g| %> <% end %> @@ -24,9 +24,12 @@ - + From 8648eefad462c4ec1a53eb69268c3a3c7d228f39 Mon Sep 17 00:00:00 2001 From: charles milam Date: Tue, 9 Dec 2014 21:39:07 -0600 Subject: [PATCH 18/19] Modify endpoint post /albums to save multiple genres with album. --- server.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server.rb b/server.rb index 64259544..ced49e4f 100644 --- a/server.rb +++ b/server.rb @@ -10,13 +10,16 @@ get '/albums' do db = Songify.create_db_connection('songify_dev') @albums = Songify::AlbumRepo.all(db) + @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], + "genre_ids" => params[:genres] }) redirect to '/albums' end From 7c22372f7dc1ff22522254f5b567a6d45f997ddd Mon Sep 17 00:00:00 2001 From: charles milam Date: Tue, 9 Dec 2014 21:40:41 -0600 Subject: [PATCH 19/19] final for exercise 3 --- lib/songify.rb | 26 ++++++++++++++++++-------- lib/songify/song_repo.rb | 2 +- spec/repos/album_repo_spec.rb | 3 +-- views/genres/index.erb | 26 ++++++++++++++------------ 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/lib/songify.rb b/lib/songify.rb index 43b56ea1..80a13633 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -10,7 +10,7 @@ def self.clear_db(db) DELETE FROM albums cascade; DELETE FROM songs cascade; DELETE FROM genres cascade; - delete from song_genres cascade; + delete from album_genres cascade; SQL end @@ -22,27 +22,33 @@ def self.create_tables(db) ); CREATE TABLE songs( id SERIAL PRIMARY KEY, - album_id integer REFERENCES albums (id), + album_id integer REFERENCES albums (id) + on delete cascade + on update cascade, title VARCHAR ); CREATE TABLE genres( id SERIAL PRIMARY KEY, name VARCHAR ); - create table song_genres( + create table album_genres( id serial primary key, - album_id integer references albums (id), + album_id integer references albums (id) + on delete cascade + on update cascade, genres_id integer references genres (id) + on delete cascade + on update cascade ) SQL end def self.drop_tables(db) db.exec <<-SQL - DROP TABLE if exists albums; - DROP TABLE if exists songs; - DROP TABLE if exists genres; - drop table if exists song_genres; + DROP TABLE if exists albums cascade; + DROP TABLE if exists songs cascade; + DROP TABLE if exists genres cascade; + drop table if exists album_genres cascade; SQL end end @@ -50,3 +56,7 @@ def self.drop_tables(db) require_relative 'songify/album_repo' require_relative 'songify/genre_repo' require_relative 'songify/song_repo' + +# db = Songify.create_db_connection("songify_dev") +# Songify.drop_tables(db) +# Songify.create_tables(db) \ No newline at end of file diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index a9fc10c0..9c89ed0e 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -22,7 +22,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) VALUES ($1) RETURNING id", [song_data["title"]]) song_data['id'] = result.entries.first['id'] song_data end diff --git a/spec/repos/album_repo_spec.rb b/spec/repos/album_repo_spec.rb index 2fa60a2a..931ebde1 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -52,8 +52,7 @@ def album_count album = repo.save(db, { 'title' => 'Suspicious Activity?', 'genre_ids' => [gid_1['id'], gid_2['id'], gid_3['id']] }) - album = repo.find_genres(db, album['id']) - puts album.first + album = repo.find(db, album['id']) expect(album['genres'].count).to eq 3 names = album['genres'].map {|g| g['name'] } diff --git a/views/genres/index.erb b/views/genres/index.erb index e62f9b7d..2e7299d0 100644 --- a/views/genres/index.erb +++ b/views/genres/index.erb @@ -1,15 +1,17 @@ <- Back to Everything -

All Genres

+
+

All Genres

-
    - <% @genres.each do |genre| %> -
  • <%= genre['name'] %>
  • - <% end %> -
+
    + <% @genres.each do |genre| %> +
  • <%= genre['name'] %>
  • + <% end %> +
-
-

New Genre

- - - -
+
+

New Genre

+ + + +
+