diff --git a/Gemfile b/Gemfile index cc9b3f83..5746a6b3 100644 --- a/Gemfile +++ b/Gemfile @@ -6,3 +6,4 @@ gem 'pry-byebug' gem 'pg' gem 'sinatra' gem 'rake' +gem 'sinatra-contrib' diff --git a/Gemfile.lock b/Gemfile.lock index 8da63939..3ef8a723 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,7 @@ GEM remote: https://rubygems.org/ specs: + backports (3.6.4) byebug (3.5.1) columnize (~> 0.8) debugger-linecache (~> 1.2) @@ -10,6 +11,7 @@ GEM 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) @@ -21,6 +23,8 @@ GEM rack (1.5.2) rack-protection (1.5.3) rack + rack-test (0.6.2) + rack (>= 1.0) rake (10.3.2) rspec (2.14.1) rspec-core (~> 2.14.0) @@ -34,6 +38,13 @@ GEM 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) @@ -46,3 +57,4 @@ DEPENDENCIES rake rspec (~> 2.14.1) sinatra + sinatra-contrib diff --git a/lib/songify.rb b/lib/songify.rb index 1761c936..91c4e8db 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -1,49 +1,61 @@ -require 'pg' - -module Songify - def self.create_db_connection(dbname) - PG.connect(host: 'localhost', dbname: dbname) - end - - def self.clear_db(db) - db.exec <<-SQL - DELETE FROM songs; - DELETE FROM albums; - DELETE FROM genres; - /* TODO: Clear rest of the tables (books, etc.) */ - SQL - end - - def self.create_tables(db) - db.exec <<-SQL - CREATE TABLE albums( - id SERIAL PRIMARY KEY, - title VARCHAR - ); - CREATE TABLE songs( - id SERIAL PRIMARY KEY, - album_id integer REFERENCES albums (id), - title VARCHAR - ); - CREATE TABLE genres( - id SERIAL PRIMARY KEY, - name VARCHAR - ); - /* TODO: Create song_genres table */ - SQL - end - - def self.drop_tables(db) - db.exec <<-SQL - DROP TABLE albums; - DROP TABLE songs; - DROP TABLE genres; - /* TODO: Drop song_genres table */ - SQL - end -end - -require_relative 'songify/errors/invalid_record_data.rb' -require_relative 'songify/album_repo' -require_relative 'songify/genre_repo' -require_relative 'songify/song_repo' +require 'pg' + +module Songify + def self.create_db_connection(dbname) + PG.connect(host: 'localhost', dbname: dbname) + end + + def self.clear_db(db) + db.exec <<-SQL + DELETE FROM album_genres; + DELETE FROM songs; + DELETE FROM albums; + DELETE FROM genres; + + /* TODO: Clear rest of the tables (books, etc.) */ + SQL + end + + def self.create_tables(db) + db.exec <<-SQL + CREATE TABLE IF NOT EXISTS albums( + id SERIAL PRIMARY KEY, + title VARCHAR + ); + CREATE TABLE IF NOT EXISTS songs( + id SERIAL PRIMARY KEY, + album_id integer REFERENCES albums (id) + on delete cascade + on update cascade, + title VARCHAR + ); + CREATE TABLE IF NOT EXISTS genres( + id SERIAL PRIMARY KEY, + name VARCHAR + ); + CREATE TABLE IF NOT EXISTS album_genres( + id SERIAL PRIMARY KEY, + album_id integer REFERENCES albums + on delete cascade + on update cascade, + genre_id integer REFERENCES genres + ); + /* TODO: Create song_genres table */ + SQL + end + + def self.drop_tables(db) + db.exec <<-SQL + DROP TABLE albums; + DROP TABLE songs; + DROP TABLE genres; + DROP TABLE album_genres; + /* TODO: Drop song_genres table */ + SQL + end +end + +require_relative 'songify/errors/invalid_record_data.rb' +require_relative 'songify/album_repo' +require_relative 'songify/genre_repo' +require_relative 'songify/song_repo' diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 65a4baa2..28a620d3 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -1,36 +1,52 @@ -module Songify - class AlbumRepo - - def self.all(db) - # Other code should not have to deal with the PG:Result. - # Therefore, convert the results into a plain array. - 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 - 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']) - else - if album_data['title'].nil? || album_data['title'] == '' - raise Errors::InvalidRecordData.new("title is required.") - end - - result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) - album_data['id'] = result.entries.first['id'] - album_data - end - end - - def self.destroy(db, album_id) - # TODO: Delete SQL statement - # ALSO DELETE SONGS - # ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS ALBUM AND ITS GENRES - end - - end -end +module Songify + class AlbumRepo + + def self.all(db) + # Other code should not have to deal with the PG:Result. + # Therefore, convert the results into a plain array. + db.exec("SELECT * FROM albums").to_a + end + + def self.find(db, album_id) + album_result = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first + album_info = db.exec("SELECT g.name, g.id FROM genres g JOIN album_genres ag ON g.id = ag.genre_id JOIN albums a ON a.id = ag.album_id WHERE ag.album_id = $1", [album_id]).to_a + if album_info[0] + final_genre = [] + album_info.each do |a| #Push the album_info Hash + final_genre.push(a) #Array of Hashes { "(genre)id" => "x", "(genre)name" => "y"}, in this case, just one Hash + end + album_result['genres'] = final_genre + return album_result + end + album_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']) #Brings back the first HASH + else + if album_data['title'].nil? || album_data['title'] == '' + raise Errors::InvalidRecordData.new("title is required.") + end + + result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]) + album_data['id'] = result.entries.first['id'] + + if album_data['genre_ids'] + album_data['genre_ids'].each do |genre| + db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre]) + end + end + album_data #Returns this + end + end + + def self.destroy(db, album_id) + # TODO: Delete SQL statement + # 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 077ceb18..554d6868 100644 --- a/lib/songify/genre_repo.rb +++ b/lib/songify/genre_repo.rb @@ -1,35 +1,36 @@ -module Songify - class GenreRepo - - def self.all(db) - # Other code should not have to deal with the PG:Result. - # Therefore, convert the results into a plain array. - db.exec("SELECT * FROM genres").to_a - end - - def self.find(db, genre_id) - db.exec("SELECT * FROM genres WHERE id=$1", [genre_id]).first - end - - def self.save(db, genre_data) - if genre_data['id'] - result = db.exec("UPDATE genres SET name = $2 WHERE id = $1", [genre_data['id'], genre_data['name']]) - self.find(db, genre_data['id']) - else - if genre_data['name'].nil? || genre_data['name'] == '' - raise Errors::InvalidRecordData.new("name is required.") - end - - result = db.exec("INSERT INTO genres (name) VALUES ($1) RETURNING id", [genre_data['name']]) - genre_data['id'] = result.entries.first['id'] - genre_data - end - end - - def self.destroy(db, genre_id) - # TODO: Delete SQL statement - # ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS GENRE AND ITS ALBUMS - end - - end -end +module Songify + class GenreRepo + + def self.all(db) + # Other code should not have to deal with the PG:Result. + # Therefore, convert the results into a plain array. + db.exec("SELECT * FROM genres").to_a + end + + def self.find(db, genre_id) + db.exec("SELECT * FROM genres WHERE id=$1", [genre_id]).first + end + + def self.save(db, genre_data) + if genre_data['id'] + result = db.exec("UPDATE genres SET name = $2 WHERE id = $1", [genre_data['id'], genre_data['name']]) + self.find(db, genre_data['id']) + else + + if genre_data['name'].nil? || genre_data['name'] == '' + raise Errors::InvalidRecordData.new("name is required.") + end + + result = db.exec("INSERT INTO genres (name) VALUES ($1) RETURNING id", [genre_data['name']]) + genre_data['id'] = result.entries.first['id'] + genre_data + end + end + + def self.destroy(db, genre_id) + # TODO: Delete SQL statement + # ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS GENRE AND ITS ALBUMS + end + + end +end diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index 1bca55c0..a97b2c0d 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -1,36 +1,36 @@ -module Songify - class SongRepo - - def self.all(db) - # Other code should not have to deal with the PG:Result. - # Therefore, convert the results into a plain array. - 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 - 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['title'].nil? || song_data['title'] == '' - raise Errors::InvalidRecordData.new("title is required.") - end - - # Ensure album exists - album = AlbumRepo.find(db, song_data['album_id']) - if album.nil? - raise raise Errors::InvalidRecordData.new("A valid album_id is required.") - end - - 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 - end - - end -end +module Songify + class SongRepo + + def self.all(db) + # Other code should not have to deal with the PG:Result. + # Therefore, convert the results into a plain array. + 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 + 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['title'].nil? || song_data['title'] == '' + raise Errors::InvalidRecordData.new("A title is required.") + end + + # Ensure album exists + album = AlbumRepo.find(db, song_data['album_id']) + if album.nil? + raise raise Errors::InvalidRecordData.new("A valid album_id is required.") + end + + 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 #returns Hash { "id" => "x", "album_id" => "y", title" => "z"} + end + end + + end +end diff --git a/server.rb b/server.rb index 361bacc7..bd425256 100644 --- a/server.rb +++ b/server.rb @@ -1,42 +1,71 @@ -require 'sinatra' -require './lib/songify.rb' - -# set :bind, '0.0.0.0' # This is needed for Vagrant - -get '/' do - erb :index -end - -get '/albums' do - db = Songify.create_db_connection('songify_dev') - @albums = Songify::AlbumRepo.all(db) - erb :"albums/index" -end - -post '/albums' do - db = Songify.create_db_connection('songify_dev') - album = Songify::AlbumRepo.save(db, { - 'title' => params[:title] - }) - redirect to '/albums' -end - - -get '/songs' do - erb :"songs/index" -end - - -get '/genres' do - db = Songify.create_db_connection('songify_dev') - @genres = Songify::GenreRepo.all(db) - erb :"genres/index" -end - -post '/genres' do - db = Songify.create_db_connection('songify_dev') - album = Songify::GenreRepo.save(db, { - 'name' => params[:name] - }) - redirect to '/genres' -end +require "sinatra" +require "bundler/setup" +require "sinatra/reloader" +require "./lib/songify.rb" + +set :bind, '0.0.0.0' # This is needed for Vagrant + +get '/' do + erb :index +end + + +## ALBUMS +get '/albums' do + db = Songify.create_db_connection('songify_dev') + @albums = Songify::AlbumRepo.all(db) #Brought back as an 'array of arrays' (for .each method) + #@songs = Songify::SongRepo.all(db) #FOR EXTENSION #6 + @genres = Songify::GenreRepo.all(db) #FOR EX3 + + erb :"albums/index" +end + +post '/albums' do + db = Songify.create_db_connection('songify_dev') + # genre = Songify::GenreRepo.save(db, { ## DON'T KNOW IF THIS IS CORRECT + # 'name' => params[:genre_names] + # }) + album = Songify::AlbumRepo.save(db, { #Returns the album hash { "id" => " "; "title" => " "} + 'title' => params[:album_title], + 'genre_ids' => params[:genre_ids] + }) + redirect to '/albums' +end +## END ALBUMS + + +## GENRES +get '/genres' do + db = Songify.create_db_connection('songify_dev') + @genres = Songify::GenreRepo.all(db) #Brought back as an 'array of arrays' (for .each method) + erb :"genres/index" +end + +post '/genres' do + db = Songify.create_db_connection('songify_dev') + genre = Songify::GenreRepo.save(db, { #Returns the genre hash { "id" => " "; "title" => " "} + #'title' => params[:title] #Changed 'album =' to 'genre =' above + 'name' => params[:genre_name] #'name' needs to reference genre name column(?) + }) + redirect to '/genres' +end +## END GENRES + + +## SONGS +get '/songs' do + db = Songify.create_db_connection('songify_dev') + @songs = Songify::SongRepo.all(db) #Brought back as an 'array of arrays' (for .each method) + @albums = Songify::AlbumRepo.all(db) #Brought back as an 'array of arrays' (for .each method) + erb :"songs/index" +end + +post '/songs' do + db = Songify.create_db_connection('songify_dev') + song = Songify::SongRepo.save(db, { + 'album_id' => params[:album_name], #'album_id' needs to reference song album_id column(?) + 'title' => params[:song_title] #'title' needs to reference song title column(?) + }) + redirect to '/songs' +end +## END SONGS diff --git a/spec/repos/album_repo_spec.rb b/spec/repos/album_repo_spec.rb index 8b2708e8..3af459f7 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -1,86 +1,90 @@ -require 'spec_helper' - -describe Songify::AlbumRepo do - - def album_count - repo.all(db).count - end - - let(:repo) { Songify::AlbumRepo } - let(:db) { Songify.create_db_connection('songify_test') } - - before(:each) do - Songify.clear_db(db) - end - - it "gets all albums" do - album = repo.save(db, { 'title' => "Allybum" }) - album = repo.save(db, { 'title' => "Bluesbum" }) - - albums = repo.all(db) - expect(albums).to be_a Array - expect(albums.count).to eq 2 - - titles = albums.map {|u| u['title'] } - expect(titles).to include "Allybum", "Bluesbum" - end - - it "creates albums" do - expect(album_count).to eq 0 - - album = repo.save(db, { 'title' => "Allybum" }) - expect(album['id']).to_not be_nil - expect(album['title']).to eq "Allybum" - - # Check for persistence - expect(album_count).to eq 1 - - album = repo.all(db).first - expect(album['title']).to eq "Allybum" - end - - it "requires a title" do - expect { repo.save(db, {}) }.to raise_error(Songify::Errors::InvalidRecordData) {|e| - expect(e.message).to match /title/ - } - end - - xit "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']) - - 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' - end - - it "finds albums" do - album = repo.save(db, { 'title' => "Allybum" }) - retrieved_song = repo.find(db, album['id']) - expect(retrieved_song['title']).to eq "Allybum" - end - - it "updates albums" do - song1 = repo.save(db, { 'title' => "Allybum" }) - song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) - expect(song2['id']).to eq(song1['id']) - expect(song2['title']).to eq "Alicia" - - # Check for persistence - song3 = repo.find(db, song1['id']) - expect(song3['title']).to eq "Alicia" - end - -end +require 'spec_helper' + +describe Songify::AlbumRepo do + + def album_count + repo.all(db).count + end + + let(:repo) { Songify::AlbumRepo } + let(:db) { Songify.create_db_connection('songify_test') } + + before(:each) do + Songify.clear_db(db) + end + + it "gets all albums" do + album = repo.save(db, { 'title' => "Allybum" }) + album = repo.save(db, { 'title' => "Bluesbum" }) + + albums = repo.all(db) + expect(albums).to be_a Array + expect(albums.count).to eq 2 + + titles = albums.map {|u| u['title'] } + expect(titles).to include "Allybum", "Bluesbum" + end + + it "creates albums" do + expect(album_count).to eq 0 + + album = repo.save(db, { 'title' => "Allybum" }) + expect(album['id']).to_not be_nil + expect(album['title']).to eq "Allybum" + + # Check for persistence + expect(album_count).to eq 1 + + album = repo.all(db).first + expect(album['title']).to eq "Allybum" + end + + + it "requires a title" do + expect { repo.save(db, {}) }.to raise_error(Songify::Errors::InvalidRecordData) {|e| + expect(e.message).to match /title/ + } + + end + + ## UPDATED ON 12/9 (twice) + 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']) + + 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' + + end + + it "finds albums" do + album = repo.save(db, { 'title' => "Allybum" }) + retrieved_song = repo.find(db, album['id']) + expect(retrieved_song['title']).to eq "Allybum" + end + + it "updates albums" do + song1 = repo.save(db, { 'title' => "Allybum" }) + song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) + expect(song2['id']).to eq(song1['id']) + expect(song2['title']).to eq "Alicia" + + # Check for persistence + song3 = repo.find(db, song1['id']) + expect(song3['title']).to eq "Alicia" + end + +end diff --git a/spec/repos/genre_repo_spec.rb b/spec/repos/genre_repo_spec.rb index e1dddac4..20e3fb3b 100644 --- a/spec/repos/genre_repo_spec.rb +++ b/spec/repos/genre_repo_spec.rb @@ -1,66 +1,66 @@ -require 'spec_helper' - -describe Songify::GenreRepo do - - def genre_count - repo.all(db).count - end - - let(:repo) { Songify::GenreRepo } - let(:db) { Songify.create_db_connection('songify_test') } - - before(:each) do - Songify.clear_db(db) - end - - it "gets all genres" do - genre = repo.save(db, { 'name' => "The Ally" }) - genre = repo.save(db, { 'name' => "Barnway Blues" }) - - genres = repo.all(db) - expect(genres).to be_a Array - expect(genres.count).to eq 2 - - titles = genres.map {|u| u['name'] } - expect(titles).to include "The Ally", "Barnway Blues" - end - - it "creates genres" do - expect(genre_count).to eq 0 - - genre = repo.save(db, { 'name' => "The Ally" }) - expect(genre['id']).to_not be_nil - expect(genre['name']).to eq "The Ally" - - # Check for persistence - expect(genre_count).to eq 1 - - genre = repo.all(db).first - expect(genre['name']).to eq "The Ally" - end - - it "requires a name" do - expect { repo.save(db, {}) }.to raise_error(Songify::Errors::InvalidRecordData) {|e| - expect(e.message).to match /name/ - } - end - - - it "finds genres" do - genre = repo.save(db, { 'name' => "The Ally" }) - retrieved_song = repo.find(db, genre['id']) - expect(retrieved_song['name']).to eq "The Ally" - end - - it "updates genres" do - song1 = repo.save(db, { 'name' => "The Ally" }) - song2 = repo.save(db, { 'id' => song1['id'], 'name' => "Alicia" }) - expect(song2['id']).to eq(song1['id']) - expect(song2['name']).to eq "Alicia" - - # Check for persistence - song3 = repo.find(db, song1['id']) - expect(song3['name']).to eq "Alicia" - end - -end +require 'spec_helper' + +describe Songify::GenreRepo do + + def genre_count + repo.all(db).count + end + + let(:repo) { Songify::GenreRepo } + let(:db) { Songify.create_db_connection('songify_test') } + + before(:each) do + Songify.clear_db(db) + end + + it "gets all genres" do + genre = repo.save(db, { 'name' => "The Ally" }) + genre = repo.save(db, { 'name' => "Barnway Blues" }) + + genres = repo.all(db) + expect(genres).to be_a Array + expect(genres.count).to eq 2 + + titles = genres.map {|u| u['name'] } + expect(titles).to include "The Ally", "Barnway Blues" + end + + it "creates genres" do + expect(genre_count).to eq 0 + + genre = repo.save(db, { 'name' => "The Ally" }) + expect(genre['id']).to_not be_nil + expect(genre['name']).to eq "The Ally" + + # Check for persistence + expect(genre_count).to eq 1 + + genre = repo.all(db).first + expect(genre['name']).to eq "The Ally" + end + + it "requires a name" do + expect { repo.save(db, {}) }.to raise_error(Songify::Errors::InvalidRecordData) {|e| + expect(e.message).to match /name/ + } + end + + + it "finds genres" do + genre = repo.save(db, { 'name' => "The Ally" }) + retrieved_song = repo.find(db, genre['id']) + expect(retrieved_song['name']).to eq "The Ally" + end + + it "updates genres" do + song1 = repo.save(db, { 'name' => "The Ally" }) + song2 = repo.save(db, { 'id' => song1['id'], 'name' => "Alicia" }) + expect(song2['id']).to eq(song1['id']) + expect(song2['name']).to eq "Alicia" + + # Check for persistence + song3 = repo.find(db, song1['id']) + expect(song3['name']).to eq "Alicia" + end + +end diff --git a/spec/repos/song_repo_spec.rb b/spec/repos/song_repo_spec.rb index cb4063c3..7049bbcc 100644 --- a/spec/repos/song_repo_spec.rb +++ b/spec/repos/song_repo_spec.rb @@ -1,84 +1,86 @@ -require 'spec_helper' - -describe Songify::SongRepo do - - def song_count - repo.all(db).count - end - - let(:repo) { Songify::SongRepo } - let(:db) { Songify.create_db_connection('songify_test') } - - before(:each) do - Songify.clear_db(db) - @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] - end - - it "gets all songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) - - songs = repo.all(db) - expect(songs).to be_a Array - expect(songs.count).to eq 2 - - titles = songs.map {|u| u['title'] } - expect(titles).to include "The Ally", "Barnway Blues" - end - - it "creates songs" do - expect(song_count).to eq 0 - - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - expect(song['id']).to_not be_nil - expect(song['title']).to eq "The Ally" - - # Check for persistence - expect(song_count).to eq 1 - - song = repo.all(db).first - expect(song['title']).to eq "The Ally" - end - - it "requires a title" do - expect { repo.save(db, {}) }.to raise_error(Songify::Errors::InvalidRecordData) {|e| - expect(e.message).to match /title/ - } - end - - it "requires an album id" do - expect { - repo.save(db, { 'title' => "The Ally" }) - } - .to raise_error(Songify::Errors::InvalidRecordData) {|e| - expect(e.message).to match /album_id/ - } - end - - it "requires an album id that exists" do - expect { - repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) - } - .to raise_error(Songify::Errors::InvalidRecordData) {|e| - expect(e.message).to match /album_id/ - } - end - - it "finds songs" do - song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - retrieved_song = repo.find(db, song['id']) - expect(retrieved_song['title']).to eq "The Ally" - end - - it "updates songs" do - song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) - song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) - expect(song2['id']).to eq(song1['id']) - expect(song2['title']).to eq "Alicia" - - # Check for persistence - song3 = repo.find(db, song1['id']) - expect(song3['title']).to eq "Alicia" - end - -end +require 'spec_helper' + +describe Songify::SongRepo do + + def song_count + repo.all(db).count + end + + let(:repo) { Songify::SongRepo } + let(:db) { Songify.create_db_connection('songify_test') } + + before(:each) do + Songify.clear_db(db) + @album_id = Songify::AlbumRepo.save(db, { 'title' => "MegaCorps" })['id'] + end + + it "gets all songs" do + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song = repo.save(db, { 'album_id' => @album_id, 'title' => "Barnway Blues" }) + + songs = repo.all(db) + expect(songs).to be_a Array + expect(songs.count).to eq 2 + + titles = songs.map {|u| u['title'] } + expect(titles).to include "The Ally", "Barnway Blues" + end + + it "creates songs" do + expect(song_count).to eq 0 + + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + expect(song['id']).to_not be_nil + expect(song['title']).to eq "The Ally" + + # Check for persistence + expect(song_count).to eq 1 + + song = repo.all(db).first + expect(song['title']).to eq "The Ally" + end + + # ASK HOW THIS WORKS + it "requires a title" do + expect { repo.save(db, {}) }.to raise_error(Songify::Errors::InvalidRecordData) {|e| + expect(e.message).to match /title/ + } + end + + it "requires an album id" do + expect { + repo.save(db, { 'title' => "The Ally" }) + } + .to raise_error(Songify::Errors::InvalidRecordData) {|e| + expect(e.message).to match /album_id/ + } + end + + it "requires an album id that exists" do + expect { + repo.save(db, { 'album_id' => 999, 'title' => "The Ally" }) + } + .to raise_error(Songify::Errors::InvalidRecordData) {|e| + expect(e.message).to match /album_id/ + } + end + # END EXPLAIN HOW THIS WORKS + + it "finds songs" do + song = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + retrieved_song = repo.find(db, song['id']) + expect(retrieved_song['title']).to eq "The Ally" + end + + it "updates songs" do + song1 = repo.save(db, { 'album_id' => @album_id, 'title' => "The Ally" }) + song2 = repo.save(db, { 'id' => song1['id'], 'title' => "Alicia" }) + expect(song2['id']).to eq(song1['id']) + expect(song2['title']).to eq "Alicia" + + # Check for persistence + song3 = repo.find(db, song1['id']) + expect(song3['title']).to eq "Alicia" + end + +end diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..ad969d97 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,15 +1,52 @@ -<- Back to Everything -
You're gonna be big
+Time to make a kick-a$$ playlist
diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 00000000..fb57ebde --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,12 @@ + + + +