From 7c733ef5efe892cff2e3e9b636185631885f49bb Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Tue, 9 Dec 2014 13:31:12 -0600 Subject: [PATCH 1/2] songify progress --- lib/songify.rb | 7 ++++++- lib/songify/album_repo.rb | 1 - lib/songify/song_repo.rb | 22 +++++++++++----------- server.rb | 15 +++++++++++++++ views/albums/index.erb | 35 ++++++++++++++++++++++++++++++++++- views/albums/show.erb | 27 +++++++++++++++++++++++++++ views/layout.erb | 10 ++++++++++ 7 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 views/albums/show.erb create mode 100644 views/layout.erb diff --git a/lib/songify.rb b/lib/songify.rb index 90548c82..f1abf90d 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -22,13 +22,18 @@ 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 ); + 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..7e2d9875 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -28,6 +28,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/song_repo.rb b/lib/songify/song_repo.rb index a9fc10c0..af625c7e 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -7,25 +7,25 @@ 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) VALUES ($1) RETURNING id", [song_data['title']]) - 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..998b2122 100644 --- a/server.rb +++ b/server.rb @@ -10,6 +10,8 @@ get '/albums' do db = Songify.create_db_connection('songify_dev') @albums = Songify::AlbumRepo.all(db) + @genres = Songify::GenreRepo.all(db) + puts @genres erb :"albums/index" end @@ -21,6 +23,19 @@ redirect to '/albums' end +get '/albums/:id' do + db = Songify.create_db_connection('songify_dev') + @album = Songify::AlbumRepo.find(db, params[:id]) + @songs = Songify::SongRepo.findbyalbum(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..3209190e 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,9 +1,24 @@ + + + + <- Back to Everything

All Albums

@@ -11,5 +26,23 @@

New Album

+
+ add another genre
+ + + + + diff --git a/views/albums/show.erb b/views/albums/show.erb new file mode 100644 index 00000000..35f2c2d9 --- /dev/null +++ b/views/albums/show.erb @@ -0,0 +1,27 @@ + + + + +<- Back to Everything +

<%= @album["title"] %>

+

Songs:

+
    <% @songs.each{|song| %> +
  1. <%= song["title"] %>
  2. + <% } %> +
+
method="post"> +

Add Songs

+ + + +
+ diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 00000000..f8d146b8 --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,10 @@ + + + + Songify + + + + <%= yield %> + + \ No newline at end of file From 08f1e6971b80fc0158519b55778cdbf0983d3b24 Mon Sep 17 00:00:00 2001 From: Julia Baritz Date: Tue, 9 Dec 2014 15:47:31 -0600 Subject: [PATCH 2/2] add index and finish songify --- views/albums/index.erb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/views/albums/index.erb b/views/albums/index.erb index 832592f8..0cca4dbe 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -32,7 +32,7 @@ select { <% @genres.each{ |g| %> <% } %> -
+ add another genre

@@ -41,7 +41,12 @@ select {