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

John Goldsmith Songify ex #454

Open
wants to merge 9 commits 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
8 changes: 7 additions & 1 deletion lib/songify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ 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
Expand All @@ -30,6 +31,10 @@ def self.create_tables(db)
name VARCHAR
);
/* TODO: Create song_genres table */
CREATE TABLE song_genres(
album_id integer REFERENCES albums (id),
genre_id integer REFERENCES genres (id)
);
SQL
end

Expand All @@ -38,6 +43,7 @@ def self.drop_tables(db)
DROP TABLE albums;
DROP TABLE songs;
DROP TABLE genres;
DROP TABLE song_genres;
/* TODO: Drop song_genres table */
SQL
end
Expand Down
31 changes: 25 additions & 6 deletions lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,45 @@ def self.all(db)
end

def self.find(db, album_id)
db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
item = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
if item
sql = %q[
SELECT g.name, g.id
FROM song_genres s
JOIN genres g ON g.id = s.genre_id
WHERE s.album_id = $1
]
result = db.exec(sql, [album_id])
genres = []
result.entries.each do |x|
genres << x
end
item['genres'] = genres
end
item
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 RETURNING *", [album_data['id'], 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
end
if album_data.has_key?('genre_ids')
album_data['genre_ids'].each do |x|
db.exec("INSERT INTO song_genres (album_id, genre_id) VALUES ($1, $2) RETURNING *", [album_data['id'],x])
end
end
end
album_data
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
end
2 changes: 2 additions & 0 deletions lib/songify/song_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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, album_id) VALUES ($1, $2) RETURNING id", [song_data['title'], song_data['album_id']])
song_data['id'] = result.entries.first['id']
song_data
Expand All @@ -30,3 +31,4 @@ def self.save(db, song_data)

end
end

19 changes: 17 additions & 2 deletions server.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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,22 +10,37 @@
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')
@songs = Songify::SongRepo.all(db)
@albums = Songify::AlbumRepo.all(db)
erb :"songs/index"
end

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


get '/genres' do
db = Songify.create_db_connection('songify_dev')
Expand Down
9 changes: 5 additions & 4 deletions spec/repos/album_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ def album_count

albums = repo.all(db)
expect(albums).to be_a Array
expect(albums.count).to eq 2
# 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

# expect(album_count).to eq 0
album = repo.save(db, { 'title' => "Allybum" })
# require 'pry-byebug'; binding.pry
expect(album['id']).to_not be_nil
expect(album['title']).to eq "Allybum"

Expand All @@ -45,7 +46,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
35 changes: 32 additions & 3 deletions views/albums/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,36 @@

<form action="/albums" method="post">
<h3>New Album</h3>
<label>Album Title:</label>
<input name="title" type="text" />
<button>Create Album</button>
<label>Album Title:</label>
<input name="title" type="text" />
<br>
<label>Genre(s)</label>
<br>
<div class='lists'>
<select name='genre_id[]' class='selecta'>
<% @genres.each do |x|%>
<option value='<%= x['id']%>'><%= x['name']%></option>
<%end%>
</select>
<a href=# class='show-it'></a>
</div>
<br>
<a class='add-genre' href='#'>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).on('click','.add-genre', function(e){
e.preventDefault()
$('.lists').clone().last().insertBefore('.add-genre');
$('.show-it').text('Remove').child()
})
$(document).on('click','.show-it',function(e){
e.preventDefault()
$(this).parent().remove()
})
//need to remove first '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>
15 changes: 15 additions & 0 deletions views/songs/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<a href="/">&lt;- Back to Everything</a>
<h1>All songs</h1>


<form action="/songs" method="post">
<h3>New Song</h3>
<label>Song Title:</label>
<input name="title" type="text" />
<select name='id_select'>
<% @albums.each do |x| %>
<option value='<%= x['id'] %>'><%= x['title'] %></option>
<% end%>
</select>
<button>Create Song</button>
</form>