diff --git a/lib/songify.rb b/lib/songify.rb index 63a55233..c43f030b 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -7,38 +7,41 @@ def self.create_db_connection(dbname) def self.clear_db(db) db.exec <<-SQL - DELETE FROM albums; + DELETE FROM song_genres; DELETE FROM songs; + DELETE FROM albums; DELETE FROM genres; - /* TODO: Clear rest of the tables (books, etc.) */ SQL end def self.create_tables(db) db.exec <<-SQL - CREATE TABLE albums( + CREATE TABLE IF NOT EXISTS albums( id SERIAL PRIMARY KEY, title VARCHAR ); - CREATE TABLE songs( + CREATE TABLE IF NOT EXISTS songs( id SERIAL PRIMARY KEY, album_id integer REFERENCES albums (id), title VARCHAR ); - CREATE TABLE genres( + CREATE TABLE IF NOT EXISTS genres( id SERIAL PRIMARY KEY, name VARCHAR ); - /* TODO: Create song_genres table */ + CREATE TABLE IF NOT EXISTS song_genres( + 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 song_genres; DROP TABLE songs; DROP TABLE genres; - /* TODO: Drop song_genres table */ SQL end end diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..03ea077f 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -8,19 +8,42 @@ def self.all(db) end def self.find(db, album_id) - db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + genres = [] + item = db.exec("SELECT albums.id as id, albums.title as title, array_to_string(array_agg(genres.name), ',') as genre, array_to_string(array_agg(genres.id), ',') as genre_id + FROM albums,song_genres, genres + WHERE song_genres.album_id = $1 AND albums.id = $1 AND song_genres.genre_id = genres.id + GROUP BY albums.id", [album_id]).first + + res = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + if (item != nil) + genre_list = item['genre'].split(',') + genre_ids = item['genre_id'].split(',') + for i in 0..(genre_list.size - 1) + genres << {'id' => genre_ids[i], 'name' => genre_list[i]} + end + res['genres'] = genres + end + + return res + 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']]) + result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'].to_i, 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']]) album_data['id'] = result.entries.first['id'] album_data + if (album_data["genre_ids"] != nil) + album_data["genre_ids"].each do |g| + db.exec("INSERT INTO song_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], g]) + end + end end + album_data end def self.destroy(db, album_id) diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index 9865dc00..e8f1d45b 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -11,6 +11,10 @@ def self.find(db, song_id) db.exec("SELECT * FROM songs WHERE id=$1", [song_id]).first end + def self.album_songs(db, album_id) + db.exec("SELECT * FROM songs WHERE album_id=$1", [album_id]).first + end + def self.save(db, song_data) if song_data['id'] result = db.exec("UPDATE songs SET title = $2 WHERE id = $1", [song_data['id'], song_data['title']]) diff --git a/server.rb b/server.rb index 361bacc7..5ea081fb 100644 --- a/server.rb +++ b/server.rb @@ -10,22 +10,32 @@ 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_id] }) 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 + puts params.inspect() + db = Songify.create_db_connection('songify_dev') + Songify::SongRepo.save(db, {'title' => params[:title], 'album_id' => params[:album_id]}) + 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 8adb9369..6f19e259 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/albums/index.erb b/views/albums/index.erb index fa67cd23..0069eac7 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -10,6 +10,36 @@

New Album

- - + +
+ +
+ 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..5b73ac61 --- /dev/null +++ b/views/songs/index.erb @@ -0,0 +1,14 @@ +<- Back to Everything +

Add A Song

+ +
+ + + +
\ No newline at end of file