diff --git a/lib/songify.rb b/lib/songify.rb index 63a55233..f1abf90d 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -29,6 +29,11 @@ def self.create_tables(db) id SERIAL PRIMARY KEY, name VARCHAR ); + CREATE TABLE album_genres ( + id SERIAL PRIMARY KEY, + album_id integer REFERENCES albums (id), + genre_id integer REFERENCES genres (id) + ); /* TODO: Create song_genres table */ SQL end diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..e25fe05b 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -13,13 +13,24 @@ def self.find(db, album_id) 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']]) - self.find(db, album_data['id']) + if album_data["genres"] + db.exec("UPDATE albums SET title = $1 WHERE id = $2", [album_data["title"], album_data['id']]) + album_data["genres"].each{|g| + db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], g]) + } + else + result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']]) + self.find(db, album_data['id']) + end 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 + puts album_data + album_data["genres"].each{|g| + db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], g]) + } + end end @@ -28,6 +39,5 @@ def self.destroy(db, album_id) # ALSO DELETE SONGS # ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS ALBUM AND ITS GENRES end - end end diff --git a/lib/songify/genre_repo.rb b/lib/songify/genre_repo.rb index 558261b5..0a8f0769 100644 --- a/lib/songify/genre_repo.rb +++ b/lib/songify/genre_repo.rb @@ -24,6 +24,10 @@ def self.save(db, genre_data) end end + def self.findalbumgenres(db, album_id) + db.exec("SELECT name from genres g join album_genres a on g.id = a.genre_id where album_id = $1", [album_id]) + end + def self.destroy(db, genre_id) # TODO: Delete SQL statement # ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS GENRE AND ITS ALBUMS diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index 9865dc00..17d55171 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -7,25 +7,27 @@ def self.all(db) db.exec("SELECT * FROM songs").to_a end - def self.find(db, song_id) - db.exec("SELECT * FROM songs WHERE id=$1", [song_id]).first + def self.findbyalbum(db, album_id) + db.exec("SELECT * FROM songs WHERE album_id=$1", [album_id]).to_a 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']]) - self.find(db, song_data['id']) - else + # if song_data['id'] + # result = db.exec("UPDATE songs SET title = $2 WHERE id = $1", [song_data['id'], song_data['title']]) + # self.find(db, song_data['id']) + # else raise "title is required." if song_data['title'].nil? || song_data['title'] == '' # Ensure album exists - album = AlbumRepo.find(db, song_data['album_id']) + album = AlbumRepo.find(db, song_data['id']) raise "A valid album_id is required." if album.nil? - 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 + + result = db.exec("INSERT INTO songs (title, album_id) VALUES ($1, $2)", [song_data["title"], song_data["id"]]) + # song_data['id'] = result.entries.first['id'] + # song_data + # end + end end diff --git a/server.rb b/server.rb index 361bacc7..4e664787 100644 --- a/server.rb +++ b/server.rb @@ -10,17 +10,48 @@ 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], + 'genres' => params[:genreid].to_a }) + redirect to '/albums' end +post '/albums/:id/genres' do + db = Songify.create_db_connection('songify_dev') + @album = Songify::AlbumRepo.find(db, params[:id]) + puts @album["title"] + album = Songify::AlbumRepo.save(db, { + 'title' => @album["title"], + 'id' => params[:id], + 'genres' => params[:genreid].to_a + }) + + redirect to '/albums/' + params[:id] +end + +get '/albums/:id' do + db = Songify.create_db_connection('songify_dev') + @genres = Songify::GenreRepo.all(db) + @album = Songify::AlbumRepo.find(db, params[:id]) + @songs = Songify::SongRepo.findbyalbum(db, params[:id] ) + @albgenres = Songify::GenreRepo.findalbumgenres(db, params[:id]) + erb :"albums/show" +end + +post '/albums/:id/songs' do + db = Songify.create_db_connection('songify_dev') + Songify::SongRepo.save(db, params) +redirect to '/albums/' + params[:id] +end + get '/songs' do erb :"songs/index" diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..0cca4dbe 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,9 +1,24 @@ +
+ + + <- Back to EverythingGenre(s): <% @albgenres.each{|g| %> + <%= g["name"]+"," %> + <% } %>
+