Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greg Horne - Songify Project #455

Open
wants to merge 1 commit into
base: songify-ex
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions lib/songify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def self.create_db_connection(dbname)

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM albums;
DELETE FROM songs;
DELETE FROM genres;
DELETE FROM songs;
DELETE FROM albums;
/* TODO: Clear rest of the tables (books, etc.) */
SQL
end
Expand Down
6 changes: 3 additions & 3 deletions lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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
db.exec("SELECT albums.title, albums.id, count(songs.album_id) FROM albums LEFT OUTER JOIN songs on ( albums.id = songs.album_id) Group By albums.id").to_a
end

def self.find(db, album_id)
Expand All @@ -25,8 +25,8 @@ def self.save(db, album_data)

def self.destroy(db, album_id)
# TODO: Delete SQL statement
# ALSO DELETE SONGS
# ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS ALBUM AND ITS GENRES
resultSongs = db.exec("delete from songs where album_id = $1", album_id)
resultsAlbum = db.exec("delete from songs where album_id = $1", album_id)
end

end
Expand Down
12 changes: 9 additions & 3 deletions lib/songify/genre_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ def self.find(db, genre_id)

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']])
#gmh modified next line
#genre_data['name'].each {
result = db.exec("UPDATE genres SET name = $2 WHERE id = $1", [genre_data['id'], genre_data['name']])
#}

self.find(db, genre_data['id'])
else
raise "name is required." if genre_data['name'].nil? || genre_data['name'] == ''

result = db.exec("INSERT INTO genres (name) VALUES ($1) RETURNING id", [genre_data['name']])
#gmh modified next line
#genre_data['name'].each {
result = db.exec("INSERT INTO genres (name) VALUES ($1) RETURNING id", [genre_data['name']])
#}
genre_data['id'] = result.entries.first['id']
genre_data
end
Expand Down
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
24 changes: 23 additions & 1 deletion server.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'rubygems'
#require 'pry-byebug'
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
Expand All @@ -10,6 +12,7 @@
get '/albums' do
db = Songify.create_db_connection('songify_dev')
@albums = Songify::AlbumRepo.all(db)
@album = Songify::GenreRepo.all(db)
erb :"albums/index"
end

Expand All @@ -23,10 +26,29 @@


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

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


get '/genres' do
db = Songify.create_db_connection('songify_dev')
@genres = Songify::GenreRepo.all(db)
Expand Down
5 changes: 3 additions & 2 deletions spec/repos/album_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ 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']] })
'genre_id' => [gid_1['id'], gid_2['id'], gid_3['id']] })
#gmh some type of forEach statement to look
album = repo.find(db, album['id'])
expect(album['genres'].count).to eq 3

Expand Down
35 changes: 33 additions & 2 deletions views/albums/index.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
<a href="/">&lt;- Back to Everything</a>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>

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

<ul>
<h2>Title - ID - # Songs</h2>
<% @albums.each do |album| %>
<li><%= album['title'] %></li>
<li><%= album['title'] + " - " + album['id'] + " - " + album['count']%></li>
<% end %>
</ul>

<form action="/albums" method="post">
<h3>New Album</h3>
<label>Album Title:</label>
<input name="title" type="text" />
</br>

<label>Select Genre:</label>
<select name="genre_id[]" id="genre_pulldown" class="genre_pulldown">
<option>---Please select a Genre---</option>
<% @album.each do |album| %>
<option value= <%= album['id'] %>><%= album['name'] %></option>
<% end %>
</select><a href='http://dosomething'>remove</a>
</br></br>
<a class="add-genre"></a>


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

<script>
$('#genre_pulldown').change(function(){
$(".add-genre").after("<div class='somediv'>");
$(".genre_pulldown:last").clone().appendTo(".somediv:last");
$(".add-genre").after("<a href='http://dosomething'>remove</a><br/></div>");
//alert("Yup!");
});

//$("#sort").change(function(){
// alert('Selected value: ' + $(this).val());
//});

</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">Create Song</a></li>
</ul>