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
17 changes: 10 additions & 7 deletions lib/songify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,41 @@ def self.create_db_connection(dbname)

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM albums;
DELETE FROM song_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 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 song_genres(
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 song_genres;
DROP TABLE songs;
DROP TABLE genres;
/* TODO: Drop song_genres table */
SQL
end
end
Expand Down
27 changes: 25 additions & 2 deletions lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,42 @@ def self.all(db)
end

def self.find(db, album_id)
db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
genres = []
item = db.exec("SELECT albums.id as id, albums.title as title, array_to_string(array_agg(genres.name), ',') as genre, array_to_string(array_agg(genres.id), ',') as genre_id
FROM albums,song_genres, genres
WHERE song_genres.album_id = $1 AND albums.id = $1 AND song_genres.genre_id = genres.id
GROUP BY albums.id", [album_id]).first

res = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
if (item != nil)
genre_list = item['genre'].split(',')
genre_ids = item['genre_id'].split(',')
for i in 0..(genre_list.size - 1)
genres << {'id' => genre_ids[i], 'name' => genre_list[i]}
end
res['genres'] = genres
end

return res

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']])
result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'].to_i, album_data['title']])
self.find(db, album_data['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']])
album_data['id'] = result.entries.first['id']
album_data
if (album_data["genre_ids"] != nil)
album_data["genre_ids"].each do |g|
db.exec("INSERT INTO song_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], g])
end
end
end
album_data
end

def self.destroy(db, album_id)
Expand Down
4 changes: 4 additions & 0 deletions lib/songify/song_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def self.find(db, song_id)
db.exec("SELECT * FROM songs WHERE id=$1", [song_id]).first
end

def self.album_songs(db, album_id)
db.exec("SELECT * FROM songs WHERE album_id=$1", [album_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']])
Expand Down
14 changes: 12 additions & 2 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@
get '/albums' do
db = Songify.create_db_connection('songify_dev')
@albums = Songify::AlbumRepo.all(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_id]
})
redirect to '/albums'
end


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

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


get '/genres' do
db = Songify.create_db_connection('songify_dev')
Expand Down
2 changes: 1 addition & 1 deletion spec/repos/album_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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' })
Expand Down
34 changes: 32 additions & 2 deletions views/albums/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@
<form action="/albums" method="post">
<h3>New Album</h3>
<label>Album Title:</label>
<input name="title" type="text" />
<button>Create Album</button>
<input name="title" class = "genre-form" type="text" />
<div class="new-genre">
<ul><select name="genre_id[]">
<option>-- Please select a Genre --</option>
<% @genres.each do |g| %>
<option value="<%= g['id'].to_i %>"><%= g['name'] %></option>
<% end %>
</select>
<a class="remove-genre" href= "#">Remove</a>
</ul>
</div>
<a class="add-genre" href= "#">Add Genre</a>

<ul><button>Create Album</button></ul>
</form>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
var $newGenreSelector = $('div.new-genre').clone()

$(document).on('click', 'a.add-genre', function(e){
e.preventDefault();
$location = $(e.currentTarget)
$location.before($newGenreSelector.clone())
});

$(document).on('click', 'a.remove-genre', function(e){
e.preventDefault();
$item = $(e.currentTarget).parent().parent()
$item.remove()
});
</script>
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>
14 changes: 14 additions & 0 deletions views/songs/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<a href="/">&lt;- Back to Everything</a>
<h1>Add A Song</h1>

<form action ="/songs" method="post">
<ul>Song Title:
<input name="title" type="text" /></ul>
<ul>Add to Album:
<select name="album_id">
<% @albums.each do |a| %>
<option value="<%= a['id'].to_i %>"><%= a['title'] %></option>
<% end %>
</select></ul>
<ul><input type="submit" value="Add Song"></ul>
</form>