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
5 changes: 5 additions & 0 deletions lib/songify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def self.create_tables(db)
id SERIAL PRIMARY KEY,
name VARCHAR
);
CREATE TABLE album_genres (
id SERIAL PRIMARY KEY,
album_id integer REFERENCES albums (id),
genre_id integer REFERENCES genres (id)
);
/* TODO: Create song_genres table */
SQL
end
Expand Down
18 changes: 14 additions & 4 deletions lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ def self.find(db, album_id)

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'])
if album_data["genres"]
db.exec("UPDATE albums SET title = $1 WHERE id = $2", [album_data["title"], album_data['id']])
album_data["genres"].each{|g|
db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], g])
}
else
result = db.exec("UPDATE albums SET title = $2 WHERE id = $1", [album_data['id'], album_data['title']])
self.find(db, album_data['id'])
end
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
puts album_data
album_data["genres"].each{|g|
db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], g])
}

end
end

Expand All @@ -28,6 +39,5 @@ def self.destroy(db, album_id)
# ALSO DELETE SONGS
# ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS ALBUM AND ITS GENRES
end

end
end
4 changes: 4 additions & 0 deletions lib/songify/genre_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def self.save(db, genre_data)
end
end

def self.findalbumgenres(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
Expand Down
24 changes: 13 additions & 11 deletions lib/songify/song_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ def self.all(db)
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
def self.findbyalbum(db, album_id)
db.exec("SELECT * FROM songs WHERE album_id=$1", [album_id]).to_a
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['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
raise "title is required." if song_data['title'].nil? || song_data['title'] == ''

# Ensure album exists
album = AlbumRepo.find(db, song_data['album_id'])
album = AlbumRepo.find(db, song_data['id'])
raise "A valid album_id is required." if album.nil?

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

result = db.exec("INSERT INTO songs (title, album_id) VALUES ($1, $2)", [song_data["title"], song_data["id"]])
# song_data['id'] = result.entries.first['id']
# song_data
# end

end

end
Expand Down
33 changes: 32 additions & 1 deletion server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,48 @@
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],
'genres' => params[:genreid].to_a
})

redirect to '/albums'
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[:genreid].to_a
})

redirect to '/albums/' + params[:id]
end

get '/albums/:id' do
db = Songify.create_db_connection('songify_dev')
@genres = Songify::GenreRepo.all(db)
@album = Songify::AlbumRepo.find(db, params[:id])
@songs = Songify::SongRepo.findbyalbum(db, params[:id] )
@albgenres = Songify::GenreRepo.findalbumgenres(db, params[:id])
erb :"albums/show"
end

post '/albums/:id/songs' do
db = Songify.create_db_connection('songify_dev')
Songify::SongRepo.save(db, params)
redirect to '/albums/' + params[:id]
end


get '/songs' do
erb :"songs/index"
Expand Down
41 changes: 40 additions & 1 deletion views/albums/index.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
<head>
<style media="screen" type="text/css">

body {
padding: 20px;
margin-left: 20px;
}
select {
width: 40%;
}


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

<ul>
<% @albums.each do |album| %>
<li><%= album['title'] %></li>
<li><a href=<%= "/albums/" + album['id'] %>><%= album['title'] %></a></li>
<% end %>
</ul>

<form action="/albums" method="post">
<h3>New Album</h3>
<label>Album Title:</label>
<input name="title" type="text" />
<div class="selectors">
<select name ="genreid[]">
<option value="" disabled selected>Select genre</option>
<% @genres.each{ |g| %>
<option value= <%= g["id"]%>><%=g["name"]%></option>
<% } %>
</select></div>
<a class="addlink">add another genre</a><br><br>
<button>Create Album</button>
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.addlink').on('click', function(e){
var remove = ('<a class="remove">remove</a>')
$('.addlink').before($('.selectors:first').clone());
$('.selectors:last').append(remove);
})
$(document).on('click', '.remove',function(e){
$(this).parent().remove();
})
})
</script>
</body>

51 changes: 51 additions & 0 deletions views/albums/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<head>
<style media="screen" type="text/css">

body {
padding: 20px;
margin-left: 20px;
}
form {
width: 30%;
}
</style>
</head>
<body>
<a href="/">&lt;- Back to Everything</a>
<h3><%= @album["title"] %></h3>
<p>Genre(s): <% @albgenres.each{|g| %>
<%= g["name"]+"," %>
<% } %></p>
<h4>Songs:</h4>
<ol><% @songs.each{|song| %>
<li><%= song["title"] %></li>
<% } %>
</ol>
<form action=<%= "/albums/" + @album["id"] + "/songs"%> method="post">
<h4>Add Songs</h4>
<label>Song Title:</label>
<input name="title" type="text" />
<button>Submit</button>
</form>

<form action=<%= "/albums/" + @album["id"] + "/genres"%> method="post">
<h4>Add Genres:</h4>
<div class="selectors">
<select name ="genreid[]">
<option value="" disabled selected>Select genre</option>
<% @genres.each{ |g| %>
<option value= <%= g["id"]%>><%=g["name"]%></option>
<% } %>
</select><br></div><a class="addlink">add another genre</a><br><br>
<button>Submit</button>
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.addlink').on('click', function(e){
$('.addlink').before($('.selectors:first').clone());
})
})
</script>
</body>
10 changes: 10 additions & 0 deletions views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Songify</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/foundation/5.1.1/css/foundation.css">
</head>
<body>
<%= yield %>
</body>
</html>