diff --git a/lib/songify.rb b/lib/songify.rb index 63a55233..ff4bad3d 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -7,38 +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; - /* TODO: Clear rest of the tables (books, etc.) */ + 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 ); - /* TODO: Create song_genres table */ + CREATE TABLE if not exists album_genres( + 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 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 54b55451..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']]) @@ -29,5 +45,34 @@ 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/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 361bacc7..0f0bd584 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,23 +10,26 @@ get '/albums' do 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 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 - 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) @@ -35,8 +38,46 @@ post '/genres' do db = Songify.create_db_connection('songify_dev') - album = Songify::GenreRepo.save(db, { + genre = Songify::GenreRepo.save(db, { 'name' => params[:name] }) redirect to '/genres' end + +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 '/albums/:id/songs' do + db = Songify.create_db_connection('songify_dev') + song = Songify::SongRepo.save(db, { + 'title' => params[:title], 'album_id' => params[:id] + }) + # song = Songify::SongRepo.save(db, {'title' => 'Ciao', 'album_id' =>2 + # }) + 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}/" +end \ No newline at end of file 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 diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..c3b4d8fb 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,15 +1,59 @@ <- Back to Everything -

All 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 new file mode 100644 index 00000000..714c6c31 --- /dev/null +++ b/views/albums/show.erb @@ -0,0 +1,45 @@ + +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

+ + + +
+ +
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

+ + -
-

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 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