diff --git a/lib/songify.rb b/lib/songify.rb index 63a55233..371caa61 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -7,9 +7,9 @@ def self.create_db_connection(dbname) def self.clear_db(db) db.exec <<-SQL - DELETE FROM albums; - DELETE FROM songs; DELETE FROM genres; + DELETE FROM songs; + DELETE FROM albums; /* TODO: Clear rest of the tables (books, etc.) */ SQL end diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..c3dc7abe 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -4,7 +4,7 @@ class AlbumRepo def self.all(db) # Other code should not have to deal with the PG:Result. # Therefore, convert the results into a plain array. - db.exec("SELECT * FROM albums").to_a + db.exec("SELECT albums.title, albums.id, count(songs.album_id) FROM albums LEFT OUTER JOIN songs on ( albums.id = songs.album_id) Group By albums.id").to_a end def self.find(db, album_id) @@ -25,8 +25,8 @@ def self.save(db, album_data) def self.destroy(db, album_id) # TODO: Delete SQL statement - # ALSO DELETE SONGS - # ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS ALBUM AND ITS GENRES + resultSongs = db.exec("delete from songs where album_id = $1", album_id) + resultsAlbum = db.exec("delete from songs where album_id = $1", album_id) end end diff --git a/lib/songify/genre_repo.rb b/lib/songify/genre_repo.rb index 558261b5..c3475d9a 100644 --- a/lib/songify/genre_repo.rb +++ b/lib/songify/genre_repo.rb @@ -13,12 +13,18 @@ def self.find(db, genre_id) def self.save(db, genre_data) if genre_data['id'] - result = db.exec("UPDATE genres SET name = $2 WHERE id = $1", [genre_data['id'], genre_data['name']]) + #gmh modified next line + #genre_data['name'].each { + result = db.exec("UPDATE genres SET name = $2 WHERE id = $1", [genre_data['id'], genre_data['name']]) + #} + self.find(db, genre_data['id']) else raise "name is required." if genre_data['name'].nil? || genre_data['name'] == '' - - result = db.exec("INSERT INTO genres (name) VALUES ($1) RETURNING id", [genre_data['name']]) + #gmh modified next line + #genre_data['name'].each { + result = db.exec("INSERT INTO genres (name) VALUES ($1) RETURNING id", [genre_data['name']]) + #} genre_data['id'] = result.entries.first['id'] genre_data 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..99c79980 100644 --- a/server.rb +++ b/server.rb @@ -1,7 +1,9 @@ +require 'rubygems' +#require 'pry-byebug' 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 @@ -10,6 +12,7 @@ get '/albums' do db = Songify.create_db_connection('songify_dev') @albums = Songify::AlbumRepo.all(db) + @album = Songify::GenreRepo.all(db) erb :"albums/index" end @@ -23,10 +26,29 @@ 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') + song = Songify::SongRepo.save(db, { + 'title' => params[:title], 'album_id' => params[:selectedSong] + }) + redirect to '/songs' +end + +post '/songsadd' do + db = Songify.create_db_connection('songify_dev') + song = Songify::SongRepo.save(db, { + 'title' => params[:title], 'album_id' => params[:selectedAlbum] + }) + redirect to '/songs' +end + + get '/genres' do db = Songify.create_db_connection('songify_dev') @genres = Songify::GenreRepo.all(db) diff --git a/spec/repos/album_repo_spec.rb b/spec/repos/album_repo_spec.rb index 3f080f05..37873364 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -45,13 +45,14 @@ 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']] }) + 'genre_id' => [gid_1['id'], gid_2['id'], gid_3['id']] }) +#gmh some type of forEach statement to look album = repo.find(db, album['id']) expect(album['genres'].count).to eq 3 diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..532babe0 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,9 +1,13 @@ -<- Back to Everything + + +<- Back to...