diff --git a/.gitignore b/.gitignore deleted file mode 100644 index e941da68..00000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -.bundle -vendor/bundle -.DS_Store diff --git a/.rspec b/.rspec deleted file mode 100644 index 660778bd..00000000 --- a/.rspec +++ /dev/null @@ -1 +0,0 @@ ---colour --format documentation diff --git a/Gemfile b/Gemfile index 1f478929..da476bb8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,8 @@ -source 'https://rubygems.org' -ruby '2.0.0' +source ('http://www.rubygems.org') -gem 'rspec', '~> 2.14.1' -gem 'pry-byebug' +gem 'pg', '~> 0.17.1' +gem 'rspec', '~> 3.1.0' +gem 'sinatra', '~> 1.4.5' +gem 'sinatra-contrib', '~> 1.4.2' +gem 'pry-byebug', '~> 1.3.3' +gem 'rake', '~> 10.4.2' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index a885a8c7..3065f990 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,35 +1,63 @@ GEM - remote: https://rubygems.org/ + remote: http://www.rubygems.org/ specs: - byebug (3.5.1) - columnize (~> 0.8) + backports (3.6.4) + byebug (2.7.0) + columnize (~> 0.3) debugger-linecache (~> 1.2) - slop (~> 3.6) coderay (1.1.0) - columnize (0.8.9) + columnize (0.9.0) debugger-linecache (1.2.0) diff-lcs (1.2.5) method_source (0.8.2) + multi_json (1.10.1) + pg (0.17.1) pry (0.10.1) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - pry-byebug (2.0.0) - byebug (~> 3.4) + pry-byebug (1.3.3) + byebug (~> 2.7) pry (~> 0.10) - rspec (2.14.1) - rspec-core (~> 2.14.0) - rspec-expectations (~> 2.14.0) - rspec-mocks (~> 2.14.0) - rspec-core (2.14.7) - rspec-expectations (2.14.5) - diff-lcs (>= 1.1.3, < 2.0) - rspec-mocks (2.14.5) + rack (1.5.2) + rack-protection (1.5.3) + rack + rack-test (0.6.2) + rack (>= 1.0) + rake (10.4.2) + rspec (3.1.0) + rspec-core (~> 3.1.0) + rspec-expectations (~> 3.1.0) + rspec-mocks (~> 3.1.0) + rspec-core (3.1.7) + rspec-support (~> 3.1.0) + rspec-expectations (3.1.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.1.0) + rspec-mocks (3.1.3) + rspec-support (~> 3.1.0) + rspec-support (3.1.2) + sinatra (1.4.5) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (~> 1.3, >= 1.3.4) + sinatra-contrib (1.4.2) + backports (>= 2.0) + multi_json + rack-protection + rack-test + sinatra (~> 1.4.0) + tilt (~> 1.3) slop (3.6.0) + tilt (1.4.1) PLATFORMS ruby DEPENDENCIES - pry-byebug - rspec (~> 2.14.1) + pg (~> 0.17.1) + pry-byebug (~> 1.3.3) + rake (~> 10.4.2) + rspec (~> 3.1.0) + sinatra (~> 1.4.5) + sinatra-contrib (~> 1.4.2) diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..945e0635 --- /dev/null +++ b/Rakefile @@ -0,0 +1,118 @@ +require_relative 'spec_helper.rb' + +@album_repo = Songify::AlbumRepo.new +@song_repo = Songify::SongRepo.new + +task :seed do + @album_repo.drop_table + @song_repo.drop_table + puts 'tables dropped' + @album_repo.create_table + @song_repo.create_table + puts 'tables created' + @album_repo.add_new_album( + {title: 'Darkness on the Edge of Town', + artist: 'Bruce Springsteen', + cover: 'http://upload.wikimedia.org/wikipedia/en/a/af/BruceSpringsteenDarknessontheEdgeofTown.jpg' + }) + @song_repo.add_new_song({ + name: 'Prove It All Night', + album_id: 1, + embed: "spotify:track:0Ye9OGxiHF3jylblTCuX7Q" + }) + @song_repo.add_new_song({ + name: "Streets Of FIre", + album_id: 1, + embed: "spotify:track:1PuHLoIL9LTev7T5ONv5zI" + }) + @song_repo.add_new_song({ + name: "Badlands", + album_id: 1, + embed: "spotify:track:0M1YQiRGel1tTMjA3orfRd" + }) + puts 'seeded bruce' + @album_repo.add_new_album( + {title: 'Pet Sounds', + artist: 'The Beach Boys', + cover: 'http://upload.wikimedia.org/wikipedia/en/b/bb/PetSoundsCover.jpg' + }) + @song_repo.add_new_song({ + name: 'Sloop John B', + album_id: 2, + embed: "spotify:track:2ULL3VZf4WwBKO4vjwT7Bg" + }) + @song_repo.add_new_song({ + name: 'God Only Knows', + album_id: 2, + embed: "spotify:track:6iGU74CwXuT4XVepjc9Emf" + }) + @song_repo.add_new_song({ + name: "Hang On To Your Ego", + album_id: 2, + embed: "spotify:track:4ZSBGLE9j9buk6DXVSryBI" + }) + puts 'seeded the beach boys' + @album_repo.add_new_album( + {title: 'Run The Jewels 2', + artist: 'Killer Mike & El P', + cover: 'http://36.media.tumblr.com/4d1d4fa86923323f2686e9f8baacfd23/tumblr_ngbryfp3IH1t00btlo1_1280.jpg' + }) + @song_repo.add_new_song({ + name: "Blockbuster Night Part 1", + album_id: 3, + embed: "spotify:track:2KxIMZDazuXN3yvPC6Kqwn" + }) + @song_repo.add_new_song({ + name: "Crown", + album_id: 3, + embed: "spotify:track:43ZfXZ1rtlnIuhfwePQUwU" + }) + @song_repo.add_new_song({ + name: "Lie, Cheat, Steal", + album_id: 3, + embed: "spotify:track:3UN6UkL6M0l8vfZS7OffZ6" + }) + puts 'seeded RTJ2' + @album_repo.add_new_album( + {title: 'Quality Street', + artist: 'Nick Lowe', + cover: 'http://ecx.images-amazon.com/images/I/91ZdT8CmwiL._SX425_.jpg' + }) + @song_repo.add_new_song({ + name: "Christmas At The Airport", + album_id: 4, + embed: "spotify:track:2bcAQKVDXBSp8NJumciMlC" + }) + @song_repo.add_new_song({ + name: "Old Toy Trains", + album_id: 4, + embed: "spotify:track:6XewVgajWGBcCYZ3LRM1Vu" + }) + @song_repo.add_new_song({ + name: "I Wish It Could Be Christmas Every Day", + album_id: 4, + embed: "spotify:track:4Evc12lXqVVcyb5XzMVUpa" + }) + puts 'seeded Quality Street' + @album_repo.add_new_album({ + title: 'Summerteeth', + artist: 'Wilco', + cover: 'http://ecx.images-amazon.com/images/I/51bJzaovu7L.jpg' + }) + @song_repo.add_new_song({ + name: "How To Fight Loneliness", + album_id: 5, + embed: "spotify:track:1CsMuJeMzRqNgS7G0fo1Gv" + }) + @song_repo.add_new_song({ + name: "Via Chicago", + album_id: 5, + embed: "spotify:track:58nPDufBVhMa2bT8G59CzS" + }) + @song_repo.add_new_song({ + name: "When you Wake Up Feeling Old", + album_id: 5, + embed: "spotify:track:2cc0vgUmSTfWCIxfmdD2uS" + }) + puts 'seeded Wilco' +end \ No newline at end of file diff --git a/lib/entity/album.rb b/lib/entity/album.rb new file mode 100644 index 00000000..0307d72b --- /dev/null +++ b/lib/entity/album.rb @@ -0,0 +1,12 @@ +class Songify::Album + + attr_reader :title, :artist, :cover, :id + + def initialize(params) + @title = params[:title] + @artist = params[:artist] + @cover = params[:cover] + @id = params[:id].to_i + end + +end \ No newline at end of file diff --git a/lib/entity/playlist.rb b/lib/entity/playlist.rb new file mode 100644 index 00000000..d8792431 --- /dev/null +++ b/lib/entity/playlist.rb @@ -0,0 +1,9 @@ +class Songify::Playlist + + attr_reader :id, :title + + def initialize(params) + @id = params[:id].to_i + @title = params[:title] + end +end \ No newline at end of file diff --git a/lib/entity/song.rb b/lib/entity/song.rb new file mode 100644 index 00000000..11c5834d --- /dev/null +++ b/lib/entity/song.rb @@ -0,0 +1,11 @@ +class Songify::Song + + attr_reader :name, :album_id, :id, :embed + + def initialize(params) + @name = params[:name] + @album_id = params[:album_id].to_i + @embed = params[:embed] + @id = params[:id].to_i + end +end \ No newline at end of file diff --git a/lib/repo/album_repo.rb b/lib/repo/album_repo.rb new file mode 100644 index 00000000..764658de --- /dev/null +++ b/lib/repo/album_repo.rb @@ -0,0 +1,78 @@ +class Songify::AlbumRepo < Songify::Repo + + def create_table + command = <<-SQL + CREATE TABLE IF NOT EXISTS + albums( + id SERIAL PRIMARY KEY, + title TEXT, + artist TEXT, + cover TEXT + ); + SQL + @db.exec(command) + end + + def drop_table + command = <<-SQL + DROP TABLE IF EXISTS albums CASCADE; + SQL + @db.exec(command) + end + + def add_new_album(params) + command = <<-SQL + INSERT INTO albums + (title, artist, cover) + VALUES ('#{params[:title]}', '#{params[:artist]}', '#{params[:cover]}') + RETURNING *; + SQL + result = @db.exec(command).first + build_album(result) + end + + def build_album(params) + Songify::Album.new(Hash[params.map{|k,v| [k.to_sym, v]}]) + end + + def find_album(params) + col = params.keys.first + val = params[col] + query = <<-SQL + SELECT * FROM albums + WHERE #{col} = '#{val}' + SQL + result = @db.exec(query).to_a + result.map {|album| build_album(album)} + end + + def get_all_albums + query = <<-SQL + SELECT * FROM albums; + SQL + result = @db.exec(query).to_a + result.map{|album| build_album(album)} + end + + def update_album(params) + command = <<-SQL + UPDATE albums + SET (title, artist, cover) = ('#{params[:title]}', '#{params[:artist]}', '#{params[:cover]}') + WHERE id = #{params[:id]} + RETURNING *; + SQL + result = @db.exec(command).first + build_album(result) + end + + def delete_album(params) + command = <<-SQL + DELETE FROM albums + WHERE id = #{params[:id]} + RETURNING *; + SQL + result = @db.exec(command).first + build_album(result) + end + +end \ No newline at end of file diff --git a/lib/repo/playlist_repo.rb b/lib/repo/playlist_repo.rb new file mode 100644 index 00000000..9cf23cd7 --- /dev/null +++ b/lib/repo/playlist_repo.rb @@ -0,0 +1,92 @@ +class Songify::PlaylistRepo < Songify::Repo + + def create_table + command = <<-SQL + CREATE TABLE IF NOT EXISTS playlists( + id SERIAL PRIMARY KEY, + title text + ); + SQL + @db.exec(command) + end + + def drop_table + command = <<-SQL + DROP TABLE IF EXISTS playlists CASCADE; + SQL + @db.exec(command) + end + + def create_join + command = <<-SQL + CREATE TABLE IF NOT EXISTS song_playlists( + id SERIAL PRIMARY KEY, + song_id INTEGER REFERENCES songs (id), + playlist_id INTEGER REFERENCES playlists(id) + ); + SQL + @db.exec(command) + end + + def drop_join + command = <<-SQL + DROP TABLE IF EXISTS song_playlists CASCADE; + SQL + @db.exec(command) + end + + def add_new_playlist(params) + command = <<-SQL + INSERT INTO playlists + (title) + VALUES ('#{params[:title]}') + RETURNING *; + SQL + result = @db.exec(command).first + build_playlist(result) + end + + def build_playlist(params) + Songify::Playlist.new(Hash[params.map{|k,v| [k.to_sym, v]}]) + end + + def find_playlist(params) + col = params.keys.first + val = params[col] + query = <<-SQL + SELECT * FROM playlists + WHERE #{col} = '#{val}' + SQL + result = @db.exec(query).to_a + result.map{|playlist| build_playlist(playlist)} + end + + def get_all_playlists + query = <<-SQL + SELECT * FROM playlists; + SQL + result = @db.exec(query).to_a + result.map{|playlist| build_playlist(playlist)} + end + + def add_song_to_playlist(params) + command = <<-SQL + INSERT INTO song_playlists (playlist_id, song_id) + VALUES (#{params[:playlist_id]}, #{params[:song_id]}) + RETURNING *; + SQL + result = @db.exec(command).first + find_playlist({id: result['playlist_id']}) + end + + def get_playlists_for_songs(params) + query = <<-SQL + SELECT * FROM song_playlists, songs, playlists + WHERE song_playlists.song_id = #{params[:song_id]} AND playlists.id = song_playlists.playlist_id + SQL + result = @db.exec(query).to_a + result.map{|playlist| build_playlist(playlist)} + end + + +end \ No newline at end of file diff --git a/lib/repo/repo.rb b/lib/repo/repo.rb new file mode 100644 index 00000000..7cbbd4b2 --- /dev/null +++ b/lib/repo/repo.rb @@ -0,0 +1,7 @@ +require 'pg' + +class Songify::Repo + def initialize + @db = PG.connect(dbname: 'songify-db') + end +end \ No newline at end of file diff --git a/lib/repo/song_repo.rb b/lib/repo/song_repo.rb new file mode 100644 index 00000000..fa3a962c --- /dev/null +++ b/lib/repo/song_repo.rb @@ -0,0 +1,87 @@ +class Songify::SongRepo < Songify::Repo + + def create_table + command = <<-SQL + CREATE TABLE IF NOT EXISTS + songs( + id SERIAL PRIMARY KEY, + name TEXT, + embed TEXT, + album_id INTEGER REFERENCES albums (id) ON DELETE CASCADE + ); + SQL + @db.exec(command) + end + + def drop_table + command = <<-SQL + DROP TABLE IF EXISTS songs CASCADE; + SQL + @db.exec(command) + end + + def add_new_song(params) + command = <<-SQL + INSERT INTO songs + (name, album_id, embed) + VALUES ('#{params[:name]}', #{params[:album_id]}, '#{params[:embed]}') + RETURNING *; + SQL + result = @db.exec(command).first + build_song(result) + end + + def build_song(params) + Songify::Song.new(Hash[params.map{|k,v| [k.to_sym, v]}]) + end + + def get_all_songs + query = <<-SQL + SELECT * FROM songs; + SQL + results = @db.exec(query).to_a + results.map{|song| build_song(song)} + end + + def find_songs(params) + col = params.keys.first + val = params[col] + query = <<-SQL + SELECT * FROM songs + WHERE #{col} = '#{val}' + SQL + result = @db.exec(query).to_a + result.map{|song| build_song(song)} + end + + def update_song(params) + command = <<-SQL + UPDATE songs + SET (name, embed, album_id) = ('#{params[:name]}', '#{params[:embed]}', #{params[:album_id]}) + WHERE id = #{params[:id]} + RETURNING *; + SQL + result = @db.exec(command).first + build_song(result) + end + + def delete_song(params) + command = <<-SQL + DELETE FROM songs + WHERE id = #{params[:id]} + RETURNING *; + SQL + result = @db.exec(command).first + build_song(result) + end + + def get_songs_in_playlist(params) + query = <<-SQL + SELECT * FROM song_playlists, songs, playlists + WHERE song_playlists.playlist_id = #{params[:playlist_id]} AND songs.id = song_playlists.song_id + SQL + result = @db.exec(query) + result.map{|song| build_song(song)} + end + +end \ No newline at end of file diff --git a/public/assets/plus_sign.png b/public/assets/plus_sign.png new file mode 100644 index 00000000..715cc930 Binary files /dev/null and b/public/assets/plus_sign.png differ diff --git a/public/css/main.css b/public/css/main.css new file mode 100644 index 00000000..41e2338e --- /dev/null +++ b/public/css/main.css @@ -0,0 +1,24 @@ +a { + text-decoration: none; + color: black; +} + +.album-wrapper { + text-align: center; + letter-spacing: 0; +} + +.album-cover { + margin: auto; +} + +.album-artist { + padding-bottom: 5%; +} + +.add-inner, .edit-inner { + padding: 5px; +} +img { + max-width: 90%; +} \ No newline at end of file diff --git a/server.rb b/server.rb new file mode 100644 index 00000000..c15e8f49 --- /dev/null +++ b/server.rb @@ -0,0 +1,71 @@ +require 'sinatra' +require 'sinatra/json' +require 'sinatra/reloader' if development? +require 'pry-byebug' + +require_relative 'songify.rb' + +set :bind, '0.0.0.0' # This is needed for Vagrant + +album_repo = Songify::AlbumRepo.new +song_repo = Songify::SongRepo.new + +get '/' do + @albums = album_repo.get_all_albums + erb :index +end + +post '/' do + album_repo.add_new_album(params) + redirect to('/') +end + +get '/albums/:album_id/songs/:id' do + album_id = params[:album_id] + song_id = params[:id] + @album = album_repo.find_album({id: album_id}).first + @songs = song_repo.find_songs({album_id: album_id}) + @song = song_repo.find_songs({id: song_id}).first + erb :album, :layout => :layout do + erb :song + end +end + +put '/albums/:album_id/songs/:id' do + new_album = album_repo.find_album({title: params[:album]}).first + song_repo.update_song({ + id: params[:id], + name: params[:name], + album_id: new_album.id, + embed: params[:embed] + }) + redirect back +end + +delete '/albums/:album_id/songs/:id' do + @song = song_repo.delete_song({id: params[:id]}) + redirect back +end + +get '/albums/:id' do + album_id = params[:id] + @album = album_repo.find_album({id: album_id}).first + @songs = song_repo.find_songs({album_id: album_id}) + erb :album, :layout => :layout +end + +delete '/albums/:id' do + @album = album_repo.delete_album({id: params[:id]}) + redirect('/') +end + +put '/albums/:id' do + @album = album_repo.update_album( + {id: params[:id], + title: params[:title], + artist: params[:artist], + cover: params[:cover] + }) + redirect to('/') +end + diff --git a/songify.rb b/songify.rb new file mode 100644 index 00000000..4fd15ae5 --- /dev/null +++ b/songify.rb @@ -0,0 +1,12 @@ +module Songify +end + +# require all lib/ entities and repos files here +require_relative 'lib/entity/album.rb' +require_relative 'lib/entity/song.rb' +require_relative 'lib/entity/playlist.rb' + +require_relative 'lib/repo/repo.rb' +require_relative 'lib/repo/album_repo.rb' +require_relative 'lib/repo/song_repo.rb' +require_relative 'lib/repo/playlist_repo.rb' \ No newline at end of file diff --git a/spec/.gitkeep b/spec/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/spec/entity/album_spec.rb b/spec/entity/album_spec.rb new file mode 100644 index 00000000..205a6280 --- /dev/null +++ b/spec/entity/album_spec.rb @@ -0,0 +1,16 @@ +require_relative '../../spec_helper.rb' + +describe 'an album' do + let(:album) { + Songify::Album.new( + {title: 'Darkness on the Edge of Town', + artist: 'Bruce Springsteen', + cover: 'http://upload.wikimedia.org/wikipedia/en/a/af/BruceSpringsteenDarknessontheEdgeofTown.jpg' + }) + } + it 'has a title, an artist, and a cover' do + expect(album.title).to eq('Darkness on the Edge of Town') + expect(album.artist).to eq('Bruce Springsteen') + expect(album.cover).to eq('http://upload.wikimedia.org/wikipedia/en/a/af/BruceSpringsteenDarknessontheEdgeofTown.jpg') + end +end \ No newline at end of file diff --git a/spec/entity/playlist_spec.rb b/spec/entity/playlist_spec.rb new file mode 100644 index 00000000..3f1eb5e4 --- /dev/null +++ b/spec/entity/playlist_spec.rb @@ -0,0 +1,10 @@ +require_relative '../../spec_helper.rb' + +describe 'playlist' do + let(:playlist){Songify::Playlist.new({id: 1, title: "My Chill Playlist"})} + + it 'has an id and a title' do + expect(playlist.id).to eq(1) + expect(playlist.title).to eq("My Chill Playlist") + end +end \ No newline at end of file diff --git a/spec/entity/song_spec.rb b/spec/entity/song_spec.rb new file mode 100644 index 00000000..db95ae9b --- /dev/null +++ b/spec/entity/song_spec.rb @@ -0,0 +1,22 @@ +require_relative '../../spec_helper.rb' + +describe 'Song' do + let(:album_repo){Songify::AlbumRepo.new} + before(:each) do + album_repo.add_new_album( + {title: 'Darkness on the Edge of Town', + artist: 'Bruce Springsteen', + cover: 'http://upload.wikimedia.org/wikipedia/en/a/af/BruceSpringsteenDarknessontheEdgeofTown.jpg' + }) + end + it 'has a name and an album id' do + result = Songify::Song.new({name: 'Prove It All Night', album_id: 1}) + expect(result.name).to eq('Prove It All Night') + expect(result.album_id).to eq(1) + end + + it 'has an embed link' do + result = Songify::Song.new({name: 'Streets of Fire', album_id: 1, embed: "https://embed.spotify.com/?uri=spotify:track:1PuHLoIL9LTev7T5ONv5zI"}) + expect(result.embed).to eq("https://embed.spotify.com/?uri=spotify:track:1PuHLoIL9LTev7T5ONv5zI") + end +end \ No newline at end of file diff --git a/spec/repo/album_repo_spec.rb b/spec/repo/album_repo_spec.rb new file mode 100644 index 00000000..bcdc322c --- /dev/null +++ b/spec/repo/album_repo_spec.rb @@ -0,0 +1,106 @@ +require_relative '../../spec_helper.rb' + +describe 'the album repo' do + let(:album_repo){ + Songify::AlbumRepo.new + } + + before(:each){ + album_repo.drop_table + album_repo.create_table + } + + describe 'add new album' do + it 'should be able to add an album' do + result = album_repo.add_new_album( + {title: 'Darkness on the Edge of Town', + artist: 'Bruce Springsteen', + cover: 'http://upload.wikimedia.org/wikipedia/en/a/af/BruceSpringsteenDarknessontheEdgeofTown.jpg' + }) + albums = album_repo.get_all_albums + expect(result.title).to eq('Darkness on the Edge of Town') + expect(albums.length).to eq(1) + end + end + + describe 'read, update, delete' do + before(:each) do + album_repo.add_new_album( + {title: 'Darkness on the Edge of Town', + artist: 'Bruce Springsteen', + cover: 'http://upload.wikimedia.org/wikipedia/en/a/af/BruceSpringsteenDarknessontheEdgeofTown.jpg' + }) + album_repo.add_new_album( + {title: 'Pet Sounds', + artist: 'The Beach Boys', + cover: 'http://upload.wikimedia.org/wikipedia/en/b/bb/PetSoundsCover.jpg' + }) + album_repo.add_new_album( + {title: 'Run The Jewels 2', + artist: 'Killer Mike & El P', + cover: 'http://36.media.tumblr.com/4d1d4fa86923323f2686e9f8baacfd23/tumblr_ngbryfp3IH1t00btlo1_1280.jpg' + }) + album_repo.add_new_album( + {title: 'Run The Jewels 2', + artist: 'Killer Mike & El P', + cover: 'http://36.media.tumblr.com/4d1d4fa86923323f2686e9f8baacfd23/tumblr_ngbryfp3IH1t00btlo1_1280.jpg' + }) + end + + it 'should be able to find an album by id' do + result = album_repo.find_album({id: 1}) + expect(result.first.artist).to eq('Bruce Springsteen') + end + + it 'should be able to find an album by title' do + result = album_repo.find_album({title: 'Pet Sounds'}) + expect(result.first.id).to eq(2) + end + + it 'should be able to find multiple albums of the same title' do + result = album_repo.find_album({title: "Run The Jewels 2"}) + res_arr = [] + expect(result.length).to eq(2) + result.each{|x| res_arr << x.id} + expect(res_arr).to eq([3,4]) + end + + describe 'update' do + it 'can update an album' do + result = album_repo.update_album({id: 1, title: 'Born To Run', artist: 'Bruce Springsteen', cover: 'www.some_cover.jpg'}) + expect(result.title).to eq('Born To Run') + end + + it 'can update the artist' do + result = album_repo.update_album({id: 2, artist: 'TBB', title: 'Pet Sounds', cover: 'some_cover.jpg'}) + expect(result.artist).to eq('TBB') + expect(result.title).to eq('Pet Sounds') + end + + it 'can update the cover' do + result = album_repo.update_album({id: 3, artist: 'Killer Mike & El P', title: 'RTJ2', cover: 'test.jpg'}) + expect(result.artist).to eq('Killer Mike & El P') + expect(result.cover).to eq('test.jpg') + end + + it 'can update multiple params' do + result = album_repo.update_album({id: 4, artist: 'KMEP', title: 'RTJ2', cover: 'some_cover.jpg'}) + expect(result.artist).to eq('KMEP') + expect(result.title).to eq('RTJ2') + expect(result.id).to eq(4) + end + + end + + describe 'delete' do + it 'can delete an album' do + result = album_repo.delete_album({id: 2}) + expect(result.title).to eq('Pet Sounds') + albums = album_repo.get_all_albums + expect(albums.length).to eq(3) + end + end + end + + +end \ No newline at end of file diff --git a/spec/repo/playlist_repo_spec.rb b/spec/repo/playlist_repo_spec.rb new file mode 100644 index 00000000..f29b3b75 --- /dev/null +++ b/spec/repo/playlist_repo_spec.rb @@ -0,0 +1,101 @@ +require_relative '../../spec_helper.rb' + +describe 'the playlist repo' do + let(:playlist_repo){ + Songify::PlaylistRepo.new + } + let(:song_repo){ + Songify::SongRepo.new + } + let(:album_repo){ + Songify::AlbumRepo.new + } + before(:each){ + album_repo.drop_table + album_repo.create_table + song_repo.drop_table + song_repo.create_table + playlist_repo.drop_table + playlist_repo.create_table + playlist_repo.drop_join + playlist_repo.create_join + album_repo.add_new_album( + {title: 'Darkness on the Edge of Town', + artist: 'Bruce Springsteen', + cover: 'http://upload.wikimedia.org/wikipedia/en/a/af/BruceSpringsteenDarknessontheEdgeofTown.jpg' + }) + song_repo.add_new_song({name: 'Prove It All Night', album_id: 1, embed:'http://hello.world'}) + song_repo.add_new_song({name: 'Song 2', album_id: 1, embed:"another.html"}) + song_repo.add_new_song({name: 'third song', album_id: 1, embed:"more embed"}) + } + + describe 'add new playlist' do + it 'can create a playlist' do + result = playlist_repo.add_new_playlist({title: 'My Sweet Playlist'}) + expect(result.id).to eq(1) + playlists = playlist_repo.get_all_playlists + expect(playlists.length).to eq(1) + end + end + + describe 'read,edit,delete' do + + before(:each){ + playlist_repo.add_new_playlist({title: "My chillass playlist"}) + } + it 'can find a playlist' do + result = playlist_repo.find_playlist({id: 1}) + expect(result.first.title).to eq("My chillass playlist") + end + + it 'can get all playlists' do + playlist_repo.add_new_playlist({title: "My second playlist"}) + result = playlist_repo.get_all_playlists + expect(result.length).to eq(2) + end + + it 'can add a song to two different playlists' do + playlist_repo.add_new_playlist({title: "Second sweet playlist"}) + playlist_repo.add_song_to_playlist({song_id: 1, playlist_id: 1}) + playlist_repo.add_song_to_playlist({song_id: 1, playlist_id: 2}) + one = song_repo.get_songs_in_playlist({playlist_id: 1}) + two = song_repo.get_songs_in_playlist({playlist_id: 2}) + expect(one.first.name).to eq('Prove It All Night') + expect(two.first.name).to eq('Prove It All Night') + end + + it 'can get all the playlists a song appears on' do + playlist_repo.add_new_playlist({title: "Second playlist"}) + playlist_repo.add_song_to_playlist({song_id: 1, playlist_id: 1}) + playlist_repo.add_song_to_playlist({song_id: 1, playlist_id: 2}) + result = playlist_repo.get_playlists_for_songs({song_id: 1}) + expect(result.length).to eq(2) + expect(result.first.title).to eq('My chillass playlist') + expect(result[1].title).to eq('Second playlist') + end + + describe 'edit/update' do + it 'can update the name of a playlist' do + result = playlist_repo.update_playlist({id: 1, title: 'edited'}) + expect(result.title).to eq('edited') + end + + describe 'delete' do + it 'can delete a playlist' do + playlist_repo.add_new_playlist({title: "Second playlist"}) + playlist_repo.delete_playlist({id: 1}) + expect(playlist_repo.get_all_playlists.length).to eq(1) + end + + it 'can delete a join' do + playlist_repo.add_song_to_playlist({song_id: 1, playlist_id: 1}) + playlist_repo.add_song_to_playlist({song_id: 2, playlist_id: 1}) + playlist_repo.delete_join({song_id: 1, playlist_id: 1}) + result = song_repo.get_songs_in_playlist({playlist_id: 1}) + expect(result.length).to eq(1) + end + end + end + + end +end \ No newline at end of file diff --git a/spec/repo/song_repo_spec.rb b/spec/repo/song_repo_spec.rb new file mode 100644 index 00000000..3c5ebc38 --- /dev/null +++ b/spec/repo/song_repo_spec.rb @@ -0,0 +1,96 @@ +require_relative '../../spec_helper.rb' + +describe 'the song repo' do + let(:album_repo){ + Songify::AlbumRepo.new + } + let(:song_repo){ + Songify::SongRepo.new + } + before(:each){ + album_repo.drop_table + album_repo.create_table + song_repo.drop_table + song_repo.create_table + album_repo.add_new_album( + {title: 'Darkness on the Edge of Town', + artist: 'Bruce Springsteen', + cover: 'http://upload.wikimedia.org/wikipedia/en/a/af/BruceSpringsteenDarknessontheEdgeofTown.jpg' + }) + } + describe 'add new song' do + it 'should be able to add a song associated with a table' do + result = song_repo.add_new_song({name: 'Prove It All Night', album_id: 1, embed:'http://hello.world'}) + songs = song_repo.get_all_songs + expect(result.name).to eq('Prove It All Night') + expect(result.album_id).to eq(1) + expect(result.embed).to eq('http://hello.world') + expect(songs.length).to eq(1) + end + end + + describe 'read, update, delete' do + before(:each) do + album_repo.add_new_album( + {title: 'Pet Sounds', + artist: 'The Beach Boys', + cover: 'http://upload.wikimedia.org/wikipedia/en/b/bb/PetSoundsCover.jpg' + }) + song_repo.add_new_song( + {name: 'Sloop John B', + album_id: 2 + }) + song_repo.add_new_song( + {name: 'Something in the Night', + album_id: 1 + }) + end + it 'can get all songs' do + songs = song_repo.get_all_songs + expect(songs.length).to eq(2) + expect(songs[0].name).to eq('Sloop John B') + expect(songs[1].name).to eq('Something in the Night') + end + + it 'can find a song by id' do + result = song_repo.find_songs({id: 1}) + expect(result.first.name).to eq('Sloop John B') + end + + it 'can find a song by name' do + result = song_repo.find_songs({name: 'Something in the Night'}) + expect(result.first.album_id).to eq(1) + end + + it 'can find multiple songs of the same name' do + song_repo.add_new_song({name: 'Sloop John B', album_id: 1}) + result = song_repo.find_songs({name: 'Sloop John B'}) + expect(result[0].album_id).to eq(2) + expect(result[1].album_id).to eq(1) + end + + it 'can find all songs from an album' do + song_repo.add_new_song({name: 'Prove It All Night', album_id: 1}) + result = song_repo.find_songs({album_id: 1}) + expect(result.length).to eq(2) + expect(result[0].name).to eq('Something in the Night') + expect(result[1].name).to eq('Prove It All Night') + end + + it 'can update a song' do + result = song_repo.update_song({id: 1, album_id: 1, name: 'Sloop John'}) + album = song_repo.find_songs({album_id: 1}) + expect(album.length).to eq(2) + expect(result.id).to eq(1) + expect(result.name).to eq('Sloop John') + end + + it 'can delete a song' do + result = song_repo.delete_song({id: 1}) + songs = song_repo.get_all_songs + expect(songs.length).to eq(1) + expect(result.name).to eq('Sloop John B') + end + + end +end \ No newline at end of file diff --git a/spec_helper.rb b/spec_helper.rb new file mode 100644 index 00000000..ccb3be82 --- /dev/null +++ b/spec_helper.rb @@ -0,0 +1,4 @@ +require 'rspec' +require 'pry-byebug' + +require_relative 'songify.rb' \ No newline at end of file diff --git a/views/album.erb b/views/album.erb new file mode 100644 index 00000000..9b828d57 --- /dev/null +++ b/views/album.erb @@ -0,0 +1,37 @@ +