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..3bb50987 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -8,7 +8,21 @@ 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.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) @@ -20,7 +34,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 +51,5 @@ def self.destroy(db, album_id) end end + + 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..901e51e2 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 @@ -9,23 +9,38 @@ 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 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 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..6f19e259 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -45,15 +45,22 @@ 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_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..e502a4f0 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,9 +1,13 @@ + + <- Back to Everything

All Albums

@@ -11,5 +15,43 @@

New Album

+
+
+ +
+
+ + Remove +
+
+ Add Genre +
+ + + + + + + + \ No newline at end of file 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