diff --git a/lib/songify.rb b/lib/songify.rb index 90548c82..5a9a2002 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -2,14 +2,15 @@ module Songify def self.create_db_connection(dbname) - PG.connect(host: 'localhost', dbname: dbname) + PG.connect(host: 'localhost', dbname: dbname, user: "ruby", password: "rubyRailsJS") end def self.clear_db(db) db.exec <<-SQL - DELETE FROM albums; - DELETE FROM songs; - DELETE FROM genres; + DELETE FROM albumgenres CASCADE; + DELETE FROM genres CASCADE; + DELETE FROM songs CASCADE; + DELETE FROM albums CASCADE; /* TODO: Clear rest of the tables (books, etc.) */ SQL end @@ -22,22 +23,27 @@ 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( id SERIAL PRIMARY KEY, name VARCHAR ); - /* TODO: Create song_genres table */ + CREATE TABLE albumgenres( + id SERIAL PRIMARY KEY, + album_id integer REFERENCES albums (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; + DROP TABLE albumgenres CASCADE; + DROP TABLE albums CASCADE; + DROP TABLE songs CASCADE; + DROP TABLE genres CASCADE; /* TODO: Drop song_genres table */ SQL end diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..99f735ac 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -4,11 +4,50 @@ 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 + r1 = db.exec("SELECT * FROM albums").to_a + q = <<-SQL + SELECT + ag.album_id, + g.name + FROM genres g + JOIN albumgenres ag + ON g.id = ag.genre_id + SQL + + genres = db.exec(q).to_a + genres.each do |genre| + r1.each do |album| + if(album['id'] == genre['album_id']) + album['genres']||album['genres'] = [] + album['genres'].push(genre['name']) + end + end + end + return r1 end def self.find(db, album_id) - db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + r1 = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + + q = <<-SQL + SELECT + ag.album_id, + g.name + FROM genres g + JOIN albumgenres ag + ON g.id = ag.genre_id + WHERE ag.album_id=$1 + SQL + + r2 = db.exec(q, [album_id]).to_a + if(r2 != []) + r1['genres'] = [] + + r2.each do |genre| + r1['genres'].push(genre) + end + end + return r1 end def self.save(db, album_data) @@ -17,9 +56,17 @@ def self.save(db, album_data) 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']]) + + result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING *", [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 albumgenres (album_id, genre_id) VALUES ($1, $2)", [album_data["id"], genre_id]) + end + + end + return album_data end end diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index a9fc10c0..06cb26de 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -22,11 +22,21 @@ 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 + return song_data + end + def self.all_with_album(db) + r = db.exec <<-SQL + SELECT s.id, + s.title AS song_title, + a.title AS album_title + FROM songs s + JOIN albums a + on s.album_id = a.id + SQL + r.to_a end - end end diff --git a/server.rb b/server.rb index 361bacc7..4aaf768e 100644 --- a/server.rb +++ b/server.rb @@ -1,5 +1,6 @@ require 'sinatra' require './lib/songify.rb' +require 'json' # set :bind, '0.0.0.0' # This is needed for Vagrant @@ -10,22 +11,43 @@ get '/albums' do db = Songify.create_db_connection('songify_dev') @albums = Songify::AlbumRepo.all(db) + @genres = JSON.generate(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 get '/songs' do + db = Songify.create_db_connection('songify_dev') + @songs = Songify::SongRepo.all_with_album(db) erb :"songs/index" end +get '/songs/add' do + db = Songify.create_db_connection('songify_dev') + @albums = Songify::AlbumRepo.all(db) + @genres = JSON.generate(Songify::GenreRepo.all(db)) + + erb :"songs/add" +end +post '/songs/add' do + db = Songify.create_db_connection('songify_dev') + data = {} + data["title"] = params["title"] + data["album_id"] = params["album"] + Songify::SongRepo.save(db, data) + + 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..2c698d3c 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -45,11 +45,10 @@ 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']) diff --git a/spec/repos/song_repo_spec.rb b/spec/repos/song_repo_spec.rb index d9a823e7..44807053 100644 --- a/spec/repos/song_repo_spec.rb +++ b/spec/repos/song_repo_spec.rb @@ -72,6 +72,7 @@ def song_count it "updates songs" do song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song1['id'] song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) expect(song2['id']).to eq(song1['id']) expect(song2['title']).to eq "Alicia" diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..12dd2b34 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -9,7 +9,46 @@
+ \ No newline at end of file