Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/songify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ def self.create_db_connection(dbname)

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM albums;
DELETE FROM albums_genres;
DELETE FROM songs;
DELETE FROM genres;
/* TODO: Clear rest of the tables (books, etc.) */
DELETE FROM albums;
SQL
end

Expand All @@ -29,20 +29,31 @@ def self.create_tables(db)
id SERIAL PRIMARY KEY,
name VARCHAR
);
/* TODO: Create song_genres table */
CREATE TABLE albums_genres(
id SERIAL PRIMARY KEY,
genre_id integer REFERENCES genres (id),
album_id integer REFERENCES albums (id)
);
CREATE TABLE songs_genres(
id SERIAL PRIMARY KEY,
song_id integer REFERENCES songs (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 genres;
/* TODO: Drop song_genres table */
DROP TABLE albums;
DROP TABLE albums_genres;
DROP TABLE song_genres;
SQL
end
end

require_relative 'songify/album_repo'
require_relative 'songify/genre_repo'
require_relative 'songify/song_repo'

24 changes: 23 additions & 1 deletion lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ def self.all(db)
end

def self.find(db, album_id)
db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
result = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
if result
newresult = db.exec("SELECT albums.title, albums.id, albums_genres.genre_id, genres.name FROM albums, albums_genres, genres WHERE albums.id = albums_genres.album_id AND genres.id = albums_genres.genre_id AND albums.id=$1;", [album_id]).to_a
genres = []
newresult.each do |entry|
genres << {"name" => entry['name'], "id" => entry["genre_id"]}
end
result['genres'] = genres
end
puts result
result
end

def self.albums_with_songs(db)
db.exec("select count(songs.album_id), albums.title , albums.id FROM songs, albums WHERE songs.album_id = albums.id GROUP BY albums.title, albums.id;")
end

def self.save(db, album_data)
Expand All @@ -20,7 +34,13 @@ def self.save(db, album_data)
result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']])
album_data['id'] = result.entries.first['id']
album_data
if album_data['genre_ids']
album_data['genre_ids'].each do |genre_id|
db.exec("INSERT INTO albums_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], genre_id])
end
end
end
album_data
end

def self.destroy(db, album_id)
Expand All @@ -31,3 +51,5 @@ def self.destroy(db, album_id)

end
end


2 changes: 1 addition & 1 deletion lib/songify/song_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions server.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
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
end

get '/albums' do
db = Songify.create_db_connection('songify_dev')
@albums = Songify::AlbumRepo.all(db)
@albums = Songify::AlbumRepo.albums_with_songs(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],
'genre_ids' => params[:genre_ids]
})
redirect to '/albums'
end


get '/songs' do
db = Songify.create_db_connection('songify_dev')
@songs = Songify::SongRepo.all(db)
@albums = Songify::AlbumRepo.all(db)
@genres = Songify::GenreRepo.all(db)
erb :"songs/index"
end

post '/songs' do
db = Songify.create_db_connection('songify_dev')
song = Songify::SongRepo.save(db, {
'title' => params[:title],
'album_id' => params[:album]
})
redirect to '/songs'
end


get '/genres' do
db = Songify.create_db_connection('songify_dev')
Expand Down
15 changes: 11 additions & 4 deletions spec/repos/album_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,22 @@ 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 = repo.save(db, { 'title' => 'Suspicious Activity?',
'genre_ids' => [gid_1['id'], gid_2['id'], gid_3['id']] })
album = repo.find(db, album['id'])
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'
Expand Down
44 changes: 43 additions & 1 deletion views/albums/index.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,57 @@
<!-- <!DOCTYPE html>
<html>
<body> -->

<a href="/">&lt;- Back to Everything</a>
<h1>All Albums</h1>

<ul>
<% @albums.each do |album| %>
<li><%= album['title'] %></li>
<li><%= album['title'] %>: <%= album['count'] %> songs</li>
<% end %>
</ul>

<form action="/albums" method="post">
<h3>New Album</h3>
<label>Album Title:</label>
<input name="title" type="text" />
<br>
<br>
<label> Genres: </label>
<br>
<div class="genre">
<select name="genre_ids[]">
<% @genres.each do |genre| %>
<option value="<%= genre['id'] %>"> <%= genre['name'] %> </option>
<% end %>
</select>
<a href=# class="remove-genre">Remove</a>
<br>
</div>
<a href=# class="add-genre">Add Genre</a>
<br>
<button>Create Album</button>
</form>

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>

$(document).on("click", ".add-genre", function(e){
e.preventDefault()
$( "button" ).before( $(".genre").last().clone())
// $( ".add-genre" ).last().remove()
// $( ".genre" ).last().append( $( ".add-genre" ) )

})

$(document).on("click", ".remove-genre", function(e){
e.preventDefault()
$(this).parent().remove()
})

</script>
</body>
<!-- eoifjweofiwej -->
<!-- eoifjweofiwej -->
<!-- eoifjweofiwej -->
<!-- eoifjweofiwej -->
1 change: 1 addition & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<ul>
<li><a href="/albums">View Albums</a></li>
<li><a href="/genres">View Genres</a></li>
<li><a href="/songs">View Songs</a></li>
</ul>
38 changes: 38 additions & 0 deletions views/songs/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<a href="/">&lt;- Back to Everything</a>
<h1>All Songs</h1>

<ul>
<% @songs.each do |song| %>
<li><%= song['title'] %></li>
<% end %>
</ul>

<form action="/songs" method="post">
<h3>New Song</h3>
<label>Song Title:</label>
<input name="title" type="text" />
<label> Album: </label>
<select name="album">
<% @albums.each do |album| %>
<option value="<%= album['id'] %>"> <%= album['title'] %> </option>
<% end %>
</select>
<br>
<br>
<label> Genres: </label>
<select name="genre_ids[]" class="genre">
<% @genres.each do |genre| %>
<option value="<%= genre['id'] %>"> <%= genre['name'] %> </option>
<% end %>
</select>
<br>
<br>
<button>Create Song</button>
</form>

<!-- weiufhwieufwieufh -->
<!-- weiufhwieufwieufh -->
<!-- weiufhwieufwieufh -->
<!-- weiufhwieufwieufh -->
<!-- weiufhwieufwieufh -->
<!-- weiufhwieufwieufh -->