From c9271c0508c785b527415d26cc0df219ee456bb7 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Tue, 9 Dec 2014 13:29:56 -0600 Subject: [PATCH 1/5] add server routes including extensions 1 and 2 --- lib/songify.rb | 11 +++++++--- lib/songify/album_repo.rb | 28 +++++++++++++++++++++++++ lib/songify/song_repo.rb | 2 +- server.rb | 43 ++++++++++++++++++++++++++++++++++++++- views/albums/index.erb | 28 ++++++++++++++++++++----- views/albums/show.erb | 10 +++++++++ views/songs/create.erb | 10 +++++++++ views/songs/index.erb | 12 +++++++++++ 8 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 views/albums/show.erb create mode 100644 views/songs/create.erb create mode 100644 views/songs/index.erb diff --git a/lib/songify.rb b/lib/songify.rb index 90548c82..79acf2a4 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -10,7 +10,7 @@ def self.clear_db(db) DELETE FROM albums; DELETE FROM songs; DELETE FROM genres; - /* TODO: Clear rest of the tables (books, etc.) */ + SQL end @@ -22,14 +22,19 @@ 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 album_genres( + id SERIAL PRIMARY KEY, + album_id integer REFERENCES albums (id), + genre_id integer REFERENCES genres (id) + ); + /* TODO: Create album_genres table */ SQL end diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..87b8ba71 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -29,5 +29,33 @@ def self.destroy(db, album_id) # ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS ALBUM AND ITS GENRES end + def self.all_songs_by_album(db, album_id) + sql = %q[ + SELECT s.title FROM songs s + JOIN albums a + ON a.id = s.album_id + WHERE album_id = $1] + db.exec(sql, [album_id]).to_a + end + + def self.all_songs_join_albums(db) + sql = %q[ + SELECT s.title as song, a.title FROM songs s + JOIN albums a + ON a.id = s.album_id + ] + db.exec(sql).to_a + end + + + def self.count_songs_join_albums(db) + sql = %q[ + select a.id, a.title, count(s.id) as numberOfSongs + from albums a LEFT OUTER join songs s + ON a.id = s.album_id + group by a.title, a.id ; + ] + db.exec(sql).to_a + end end 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..cf0150b8 100644 --- a/server.rb +++ b/server.rb @@ -1,7 +1,7 @@ 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 +10,7 @@ get '/albums' do db = Songify.create_db_connection('songify_dev') @albums = Songify::AlbumRepo.all(db) + @songs = Songify::AlbumRepo.count_songs_join_albums(db) erb :"albums/index" end @@ -23,6 +24,8 @@ get '/songs' do + db = Songify.create_db_connection('songify_dev') + @songs = Songify::SongRepo.all(db) erb :"songs/index" end @@ -40,3 +43,41 @@ }) redirect to '/genres' end + +post '/create' do + db = Songify.create_db_connection('songify_dev') + id = params[:album_id] + redirect to "/albums/#{id}/songs/create" +end + +get '/albums/:id/songs/create' do + db = Songify.create_db_connection('songify_dev') + @album = Songify::AlbumRepo.find(db, params[:id]) + erb :"songs/create" +end + +get '/albums/:id/songs' do + db = Songify.create_db_connection('songify_dev') + @album_id = params[:id] + @album = Songify::AlbumRepo.find(db, params[:id]) + @songs = Songify::AlbumRepo.all_songs_by_album(db, @album_id) + erb :"albums/show" +end + + +post '/albums/:id/songs' do + db = Songify.create_db_connection('songify_dev') + song = Songify::SongRepo.save(db, { + 'title' => params[:song_title], 'album_id' => params[:id] + }) + # song = Songify::SongRepo.save(db, {'title' => 'Ciao', 'album_id' =>2 + # }) + redirect to '/albums' +end + + +post '/albums/show' do + db = Songify.create_db_connection('songify_dev') + id = params['album_id'] + redirect to "/albums/#{id}/songs" +end \ No newline at end of file diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..f2b6aa94 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,11 +1,27 @@ <- Back to Everything

All Albums

- +
+ + +
+ + +

View Albums

+ +
+ + +

New Album

@@ -13,3 +29,5 @@
+ + diff --git a/views/albums/show.erb b/views/albums/show.erb new file mode 100644 index 00000000..92840589 --- /dev/null +++ b/views/albums/show.erb @@ -0,0 +1,10 @@ +

Songify

+Return to Home Page + +

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

+

+ diff --git a/views/songs/create.erb b/views/songs/create.erb new file mode 100644 index 00000000..4a0b0ea3 --- /dev/null +++ b/views/songs/create.erb @@ -0,0 +1,10 @@ + +Register a song for <%= @album['title'] %> + +
+ + + + + +
\ No newline at end of file diff --git a/views/songs/index.erb b/views/songs/index.erb new file mode 100644 index 00000000..3074b335 --- /dev/null +++ b/views/songs/index.erb @@ -0,0 +1,12 @@ +

Songs

+Return to Home Page + +

<%= @song['title'] %>

+

+ + + \ No newline at end of file From ddc3811ec829b1f8c2b9b74969e11aac6931e968 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Tue, 9 Dec 2014 15:41:59 -0600 Subject: [PATCH 2/5] add part2 --- Suspicious Activity?, | 0 [gid_1[id], | 0 lib/songify.rb | 19 +++++++++---------- lib/songify/album_repo.rb | 21 +++++++++++++++++++-- server.rb | 8 +++++--- spec/repos/album_repo_spec.rb | 17 +++++------------ 6 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 Suspicious Activity?, create mode 100644 [gid_1[id], diff --git a/Suspicious Activity?, b/Suspicious Activity?, new file mode 100644 index 00000000..e69de29b diff --git a/[gid_1[id], b/[gid_1[id], new file mode 100644 index 00000000..e69de29b diff --git a/lib/songify.rb b/lib/songify.rb index 79acf2a4..ff4bad3d 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -7,43 +7,42 @@ 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 album_genres; + DELETE FROM albums; + DELETE FROM genres; 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 ); - CREATE TABLE album_genres( + CREATE TABLE if not exists album_genres( id SERIAL PRIMARY KEY, album_id integer REFERENCES albums (id), genre_id integer REFERENCES genres (id) ); - /* TODO: Create album_genres table */ SQL end def self.drop_tables(db) db.exec <<-SQL - DROP TABLE albums; DROP TABLE songs; + DROP TABLE album_genres; DROP TABLE genres; - /* TODO: Drop song_genres table */ + DROP TABLE albums; SQL end end diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 87b8ba71..b77a801d 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -7,14 +7,30 @@ def self.all(db) db.exec("SELECT * FROM albums").to_a end - def self.find(db, album_id) - db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + def self.find(db, album_id) + genres = [] + result = db.exec("SELECT * FROM albums WHERE id = $1", [album_id]).first + if result + album_genres = db.exec("SELECT * FROM album_genres a JOIN genres g ON g.id = a.genre_id WHERE a.album_id = $1", [album_id]).to_a + album_genres.each do |line| + genres << {'id' => line['genre_id'], 'name' => line['name']} + end + result['genres'] = genres + end + result 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']]) self.find(db, album_data['id']) + elsif album_data['genre_ids'] + album_id = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]).first + album_data['id'] = album_id['id'] + album_data['genre_ids'].each do |genre| + db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2)", [album_id['id'], genre]) + end + album_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']]) @@ -57,5 +73,6 @@ def self.count_songs_join_albums(db) ] db.exec(sql).to_a end + end end diff --git a/server.rb b/server.rb index cf0150b8..b0f7b6d5 100644 --- a/server.rb +++ b/server.rb @@ -17,7 +17,8 @@ post '/albums' do db = Songify.create_db_connection('songify_dev') album = Songify::AlbumRepo.save(db, { - 'title' => params[:title] + 'title' => params[:title], + 'genres' => params[:genre_ids] }) redirect to '/albums' end @@ -38,8 +39,9 @@ post '/genres' do db = Songify.create_db_connection('songify_dev') - album = Songify::GenreRepo.save(db, { - 'name' => params[:name] + album = Songify::AlbumRepo.save(db, { + 'title' => params[:title], + 'genres' => params[:genre_ids] }) redirect to '/genres' end diff --git a/spec/repos/album_repo_spec.rb b/spec/repos/album_repo_spec.rb index 8adb9369..2b950dd4 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -45,22 +45,15 @@ 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_data = repo.save(db, { - 'title' => 'Suspicious Activity?', - 'genre_ids' => [gid_1['id'], gid_2['id'], gid_3['id']] - }) - album = repo.find(db, album_data['id']) - + album = repo.save(db, { 'title' => 'Suspicious Activity?', + 'genre_ids' => [gid_1['id'], gid_2['id'], gid_3['id']] }) + album = repo.find(db, album['id']) expect(album['genres'].count).to eq 3 - genre = album['genres'].first - expect(genre).to be_a Hash - expect(genre['id']).to_not be_nil - expect(genre['name']).to_not be_nil names = album['genres'].map {|g| g['name'] } expect(names).to include 'rock', 'avant-garde', 'jazz' @@ -83,4 +76,4 @@ def album_count expect(song3['title']).to eq "Alicia" end -end +end \ No newline at end of file From df66c5c3e7d42315341dcb557b575976d683a4b1 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Tue, 9 Dec 2014 15:45:12 -0600 Subject: [PATCH 3/5] add part2 --- Suspicious Activity?, | 0 [gid_1[id], | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Suspicious Activity?, delete mode 100644 [gid_1[id], diff --git a/Suspicious Activity?, b/Suspicious Activity?, deleted file mode 100644 index e69de29b..00000000 diff --git a/[gid_1[id], b/[gid_1[id], deleted file mode 100644 index e69de29b..00000000 From 97fc7242ad975df3a0c21c67e3a3b5904f56d993 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Tue, 9 Dec 2014 22:38:53 -0600 Subject: [PATCH 4/5] add part2 and part3finish --- lib/songify/genre_repo.rb | 4 ++ public/script.js | 11 +++++ public/style.css | 75 ++++++++++++++++++++++++++++++++++ server.rb | 52 +++++++++++++---------- views/albums/index.erb | 86 +++++++++++++++++++++++++-------------- views/albums/show.erb | 51 +++++++++++++++++++---- views/genres/index.erb | 25 ++++++++---- views/index.erb | 14 ++++--- views/layout.erb | 12 ++++++ 9 files changed, 257 insertions(+), 73 deletions(-) create mode 100644 public/script.js create mode 100644 public/style.css create mode 100644 views/layout.erb diff --git a/lib/songify/genre_repo.rb b/lib/songify/genre_repo.rb index 558261b5..2260cd0b 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.all_album_genres(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/public/script.js b/public/script.js new file mode 100644 index 00000000..d73dc169 --- /dev/null +++ b/public/script.js @@ -0,0 +1,11 @@ +$(function() { + var select = $('select'); + var link = $('a'); + select.on('change', function(event) { + var el = $(event.target); + var id = el.val(); + var href = link.attr('href'); + href = href + '/' + id; + link.attr('href', href); + }); +}); \ No newline at end of file diff --git a/public/style.css b/public/style.css new file mode 100644 index 00000000..c25259f7 --- /dev/null +++ b/public/style.css @@ -0,0 +1,75 @@ +h1{ + font-size: 2rem; +} +div.wrapper { +width: 90%; +margin: 0 auto; +margin-top: 10%; +padding-left: 4%; +background-color: #e7a61a; + +border-radius: 53px 53px 53px 53px; +-moz-border-radius: 53px 53px 53px 53px; +-webkit-border-radius: 53px 53px 53px 53px; +border: 0px dotted #000000; +} + +div.main{ + padding-left: 35%; +} +button { + background-color:#571909; + width: 30%; + border-radius: 4%; +} + + +button.btn { + background: #34d9bd; + background-image: -webkit-linear-gradient(top, #34d9bd, #2980b9); + background-image: -moz-linear-gradient(top, #34d9bd, #2980b9); + background-image: -ms-linear-gradient(top, #34d9bd, #2980b9); + background-image: -o-linear-gradient(top, #34d9bd, #2980b9); + background-image: linear-gradient(to bottom, #34d9bd, #2980b9); + -webkit-border-radius: 52; + -moz-border-radius: 52; + border-radius: 52px; + font-family: Georgia; + color: #ffffff; + font-size: 20px; + padding: 8px 27px 10px 20px; + text-decoration: none; +} + +button.btn:hover { + background: #3cb0fd; + background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db); + background-image: -moz-linear-gradient(top, #3cb0fd, #3498db); + background-image: -ms-linear-gradient(top, #3cb0fd, #3498db); + background-image: -o-linear-gradient(top, #3cb0fd, #3498db); + background-image: linear-gradient(to bottom, #3cb0fd, #3498db); + text-decoration: none; +} +input[type="text"].input_album{ + width: 60%; +} + +input[type="text"].select, select.select { + width: 60%; +} +form.album-show{ + color: pink; + +} + +select.album-title{ + width: 60%; +} +ul +{ + list-style-type: none; +} +a.main-links{ + font-size: 1.2rem; + color: blue; +} \ No newline at end of file diff --git a/server.rb b/server.rb index b0f7b6d5..bab5463e 100644 --- a/server.rb +++ b/server.rb @@ -11,6 +11,7 @@ db = Songify.create_db_connection('songify_dev') @albums = Songify::AlbumRepo.all(db) @songs = Songify::AlbumRepo.count_songs_join_albums(db) + @genres = Songify::GenreRepo.all(db) erb :"albums/index" end @@ -23,14 +24,12 @@ redirect to '/albums' end - get '/songs' do db = Songify.create_db_connection('songify_dev') @songs = Songify::SongRepo.all(db) erb :"songs/index" end - get '/genres' do db = Songify.create_db_connection('songify_dev') @genres = Songify::GenreRepo.all(db) @@ -39,47 +38,58 @@ post '/genres' do db = Songify.create_db_connection('songify_dev') - album = Songify::AlbumRepo.save(db, { - 'title' => params[:title], - 'genres' => params[:genre_ids] + genre = Songify::GenreRepo.save(db, { + 'name' => params[:name] }) redirect to '/genres' end -post '/create' do - db = Songify.create_db_connection('songify_dev') - id = params[:album_id] - redirect to "/albums/#{id}/songs/create" -end - -get '/albums/:id/songs/create' do - db = Songify.create_db_connection('songify_dev') - @album = Songify::AlbumRepo.find(db, params[:id]) - erb :"songs/create" -end - -get '/albums/:id/songs' do +get '/albums/:id/' do db = Songify.create_db_connection('songify_dev') @album_id = params[:id] @album = Songify::AlbumRepo.find(db, params[:id]) @songs = Songify::AlbumRepo.all_songs_by_album(db, @album_id) + @genres = Songify::GenreRepo.all_album_genres(db, params[:id]) erb :"albums/show" end +# post '/create' do +# db = Songify.create_db_connection('songify_dev') +# id = params[:album_id] +# redirect to "/albums/#{id}/songs/create" +# end + +# get '/albums/:id/songs/create' do +# db = Songify.create_db_connection('songify_dev') +# @album = Songify::AlbumRepo.find(db, params[:id]) +# erb :"songs/create" +# end post '/albums/:id/songs' do db = Songify.create_db_connection('songify_dev') song = Songify::SongRepo.save(db, { - 'title' => params[:song_title], 'album_id' => params[:id] + 'title' => params[:title], 'album_id' => params[:id] }) # song = Songify::SongRepo.save(db, {'title' => 'Ciao', 'album_id' =>2 # }) - redirect to '/albums' + redirect to '/albums/' + params[:id] 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[:genre_ids].to_a + }) + + redirect to '/albums/' + params[:id] +end post '/albums/show' do db = Songify.create_db_connection('songify_dev') id = params['album_id'] - redirect to "/albums/#{id}/songs" + redirect to "/albums/#{id}/" end \ No newline at end of file diff --git a/views/albums/index.erb b/views/albums/index.erb index f2b6aa94..c3b4d8fb 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,33 +1,59 @@ <- Back to Everything -

All Albums

- -
- - -
- - -

View Albums

- -
- - -
- -
-

New Album

- - - -
+ + +
+ +
+
+

Create a New Album

+
+ + +
+
+ add another genre

+ +
+
+
+

Select an Album

+
+ +
+ +
+
+
+
+ + + + + + + + + + diff --git a/views/albums/show.erb b/views/albums/show.erb index 92840589..714c6c31 100644 --- a/views/albums/show.erb +++ b/views/albums/show.erb @@ -1,10 +1,45 @@ -

Songify

+ Return to Home Page +
+

The Songify System

+

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

+

Genre(s): <% @genres.each{|g| %> + <%= g["name"]+"," %> + <% } %>

+

Songs:

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

Add Songs

+ + + +
-

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

-

-
    - <% @songs.each do |song| %> -
  • <%= song['title'] %>

  • - <% end %> -
+
method="post"> +

Add Genres:

+
+
+
+ add another genre

+ +
+ +
+ + + + \ No newline at end of file diff --git a/views/genres/index.erb b/views/genres/index.erb index e62f9b7d..2e4d00da 100644 --- a/views/genres/index.erb +++ b/views/genres/index.erb @@ -1,15 +1,22 @@ -<- Back to Everything -

All Genres

+ + +
<- Back to Everything +
+

All Genres

+ + -
    - <% @genres.each do |genre| %> -
  • <%= genre['name'] %>
  • - <% end %> -
-

New Genre

+

Add a new genre

- +
+
+
+ \ No newline at end of file diff --git a/views/index.erb b/views/index.erb index 8f52ed98..830bba11 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,8 +1,12 @@ -

The Songify System

+
+
+

The Songify System

-

You're gonna be big

-
+
\ No newline at end of file diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 00000000..231fe82d --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,12 @@ + + + + Songify For You + + + + + + <%= yield %> + + \ No newline at end of file From d51d76a6e53b8095abf987c8600afb49ff740b06 Mon Sep 17 00:00:00 2001 From: Cristina Caputo Date: Tue, 9 Dec 2014 22:40:37 -0600 Subject: [PATCH 5/5] part 3 finish --- server.rb | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/server.rb b/server.rb index bab5463e..0f0bd584 100644 --- a/server.rb +++ b/server.rb @@ -53,18 +53,6 @@ erb :"albums/show" end -# post '/create' do -# db = Songify.create_db_connection('songify_dev') -# id = params[:album_id] -# redirect to "/albums/#{id}/songs/create" -# end - -# get '/albums/:id/songs/create' do -# db = Songify.create_db_connection('songify_dev') -# @album = Songify::AlbumRepo.find(db, params[:id]) -# erb :"songs/create" -# end - post '/albums/:id/songs' do db = Songify.create_db_connection('songify_dev') song = Songify::SongRepo.save(db, {