From 34b2e0960aa31196e9a9e46522595e47c98d11df Mon Sep 17 00:00:00 2001 From: Brian Atwood Date: Tue, 9 Dec 2014 13:45:26 -0600 Subject: [PATCH 1/4] Pass all tests --- lib/songify.rb | 21 ++++++++++++++----- lib/songify/album_repo.rb | 21 ++++++++++++++++++- lib/songify/song_repo.rb | 2 +- server.rb | 15 +++++++++++++- spec/repos/album_repo_spec.rb | 2 +- views/index.erb | 1 + views/songs/index.erb | 38 +++++++++++++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 views/songs/index.erb diff --git a/lib/songify.rb b/lib/songify.rb index 63a55233..63ca0d68 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 albums_genres; DELETE FROM songs; DELETE FROM genres; - /* TODO: Clear rest of the tables (books, etc.) */ + DELETE FROM albums; SQL end @@ -29,16 +29,26 @@ def self.create_tables(db) id SERIAL PRIMARY KEY, name VARCHAR ); - /* TODO: Create song_genres table */ + CREATE TABLE albums_genres( + id SERIAL PRIMARY KEY, + genre_id integer REFERENCES genres (id), + album_id integer REFERENCES albums (id) + ); + CREATE TABLE songs_genres( + id SERIAL PRIMARY KEY, + song_id integer REFERENCES songs (id), + genre_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 albums; + DROP TABLE albums_genres; + DROP TABLE song_genres; SQL end end @@ -46,3 +56,4 @@ def self.drop_tables(db) require_relative 'songify/album_repo' require_relative 'songify/genre_repo' require_relative 'songify/song_repo' + diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..e391c1db 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -8,7 +8,17 @@ def self.all(db) end def self.find(db, album_id) - db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + result = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + if result + newresult = db.exec("SELECT albums.title, albums.id, albums_genres.genre_id, genres.name FROM albums, albums_genres, genres WHERE albums.id = albums_genres.album_id AND genres.id = albums_genres.genre_id AND albums.id=$1;", [album_id]).to_a + genres = [] + newresult.each do |entry| + genres << {"name" => entry['name'], "id" => entry["genre_id"]} + end + result['genres'] = genres + end + puts result + result end def self.save(db, album_data) @@ -20,7 +30,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_id| + db.exec("INSERT INTO albums_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], genre_id]) + end + end end + album_data end def self.destroy(db, album_id) @@ -31,3 +47,6 @@ def self.destroy(db, album_id) end end + +#ewoijfweofij +#ewoijfweofijewoijfweofijewoijfweofij diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index a9fc10c0..9865dc00 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, album_id) VALUES ($1, $2) RETURNING id", [song_data['title'], song_data['album_id']]) song_data['id'] = result.entries.first['id'] song_data end diff --git a/server.rb b/server.rb index 361bacc7..4248a3f9 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,22 @@ get '/songs' do + db = Songify.create_db_connection('songify_dev') + @songs = Songify::SongRepo.all(db) + @albums = Songify::AlbumRepo.all(db) + @genres = Songify::GenreRepo.all(db) erb :"songs/index" end +post '/songs' do + db = Songify.create_db_connection('songify_dev') + song = Songify::SongRepo.save(db, { + 'title' => params[:title], + 'album_id' => params[:album] + }) + redirect to '/songs' +end + get '/genres' do db = Songify.create_db_connection('songify_dev') 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/index.erb b/views/index.erb index 8f52ed98..10abf7f6 100644 --- a/views/index.erb +++ b/views/index.erb @@ -5,4 +5,5 @@ diff --git a/views/songs/index.erb b/views/songs/index.erb new file mode 100644 index 00000000..5867c77b --- /dev/null +++ b/views/songs/index.erb @@ -0,0 +1,38 @@ +<- Back to Everything +

All Songs

+ + + +
+

New Song

+ + + + +
+
+ + +
+
+ +
+ + + + + + + \ No newline at end of file From 5d1b62bd12149583e352bdd83511d238feacc787 Mon Sep 17 00:00:00 2001 From: Brian Atwood Date: Tue, 9 Dec 2014 15:38:40 -0600 Subject: [PATCH 2/4] Add HTML form for adding genres to albums --- server.rb | 4 +++- spec/repos/album_repo_spec.rb | 13 ++++++++++--- views/albums/index.erb | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/server.rb b/server.rb index 4248a3f9..fbe992e7 100644 --- a/server.rb +++ b/server.rb @@ -10,13 +10,15 @@ 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[:genre_ids] }) redirect to '/albums' end diff --git a/spec/repos/album_repo_spec.rb b/spec/repos/album_repo_spec.rb index 931ebde1..6f19e259 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -50,10 +50,17 @@ def album_count 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_data = repo.save(db, { + 'title' => 'Suspicious Activity?', + 'genre_ids' => [gid_1['id'], gid_2['id'], gid_3['id']] + }) + album = repo.find(db, album_data['id']) + expect(album['genres'].count).to eq 3 + genre = album['genres'].first + expect(genre).to be_a Hash + expect(genre['id']).to_not be_nil + expect(genre['name']).to_not be_nil names = album['genres'].map {|g| g['name'] } expect(names).to include 'rock', 'avant-garde', 'jazz' diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..e392d762 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,3 +1,7 @@ + + <- Back to Everything

All Albums

@@ -11,5 +15,34 @@

New Album

+
+
+ + + Add Genre + Remove +
+
+ + + + + From 700c31beaaaae762bbe2c1a15cac65bbf9de2163 Mon Sep 17 00:00:00 2001 From: Brian Atwood Date: Tue, 9 Dec 2014 19:13:43 -0600 Subject: [PATCH 3/4] can now remove genres --- views/albums/index.erb | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/views/albums/index.erb b/views/albums/index.erb index e392d762..768a4934 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -18,14 +18,17 @@

- <% @genres.each do |genre| %> <% end %> - Add Genre Remove
+ + Add Genre
@@ -33,16 +36,22 @@ - + + + + \ No newline at end of file From 6e132e07ac2a7b4594465edb3a3023b85a89d453 Mon Sep 17 00:00:00 2001 From: Brian Atwood Date: Tue, 9 Dec 2014 20:46:33 -0600 Subject: [PATCH 4/4] add display song count next to each album on albums index page --- lib/songify/album_repo.rb | 7 +++++-- server.rb | 2 +- views/albums/index.erb | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index e391c1db..3bb50987 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -21,6 +21,10 @@ def self.find(db, album_id) result end + def self.albums_with_songs(db) + db.exec("select count(songs.album_id), albums.title , albums.id FROM songs, albums WHERE songs.album_id = albums.id GROUP BY albums.title, albums.id;") + 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']]) @@ -48,5 +52,4 @@ def self.destroy(db, album_id) end end -#ewoijfweofij -#ewoijfweofijewoijfweofijewoijfweofij + diff --git a/server.rb b/server.rb index fbe992e7..901e51e2 100644 --- a/server.rb +++ b/server.rb @@ -9,7 +9,7 @@ get '/albums' do db = Songify.create_db_connection('songify_dev') - @albums = Songify::AlbumRepo.all(db) + @albums = Songify::AlbumRepo.albums_with_songs(db) @genres = Songify::GenreRepo.all(db) erb :"albums/index" end diff --git a/views/albums/index.erb b/views/albums/index.erb index 768a4934..e502a4f0 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -7,7 +7,7 @@
    <% @albums.each do |album| %> -
  • <%= album['title'] %>
  • +
  • <%= album['title'] %>: <%= album['count'] %> songs
  • <% end %>