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
16 changes: 11 additions & 5 deletions lib/songify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,34 @@ def self.create_db_connection(dbname)

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM albums;
DELETE FROM songs;
DELETE FROM albums_genres;
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 genres (id),
album_id integer REFERENCES albums (id),
title VARCHAR
);
CREATE TABLE genres(
CREATE TABLE if not exists genres(
id SERIAL PRIMARY KEY,
name VARCHAR
);
CREATE TABLE if not exists albums_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
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,23 @@ def self.all(db)
end

def self.find(db, album_id)
db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
response = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
# result = db.exec("SELECT * FROM albums_genres WHERE album_id = $1", [album_id])
# final = {}
result = db.exec("SELECT g.name FROM albums_genres x JOIN genres g ON x.genre_id = g.id JOIN albums a ON x.album_id = a.id WHERE x.album_id = $1", [album_id]).to_a
if result[0]
final_genre = []
result.each do |x|
final_genre << x
end
response['genres'] = final_genre
return response
end
response
end

def self.find_id(db, album_title)
db.exec("SELECT * FROM albums WHERE title=$1", [album_title]).first
end

def self.save(db, album_data)
Expand All @@ -20,7 +36,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|
db.exec("INSERT INTO albums_genres (album_id, genre_id) VALUES ($1, $2)", [album_data['id'], genre])
end
end
end
album_data
end

def self.destroy(db, album_id)
Expand Down
14 changes: 13 additions & 1 deletion 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.find_id(db, song_title)
db.exec("SELECT * FROM songs WHERE title=$1", [song_title]).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 All @@ -22,11 +26,19 @@ 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'], album['id']])
song_data['id'] = result.entries.first['id']
song_data
end
end

def self.songs_per_album(db, album_id)
db.exec("SELECT * FROM songs WHERE album_id=$1", [album_id])
end





end
end
31 changes: 29 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,49 @@
get '/albums' do
db = Songify.create_db_connection('songify_dev')
@albums = Songify::AlbumRepo.all(db)
@albums.each do |x|
x['count'] = Songify::SongRepo.songs_per_album(db, x['id']).count
end
@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[:genre_ids]
})
redirect to '/albums'
end

get '/albums/:id' do
db = Songify.create_db_connection('songify_dev')
@album = Songify::AlbumRepo.find(db, params[:id])
@songs = Songify::SongRepo.songs_per_album(db, params[:id]).to_a
erb :"albums/show"
end


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

post '/songs' do
db = Songify.create_db_connection('songify_dev')

album_title = params[:album_title]
album_id = Songify::AlbumRepo.find_id(db, album_title)
song = Songify::SongRepo.save(db, {
'title' => params[:title],
'album_id' => album_id['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
75 changes: 68 additions & 7 deletions views/albums/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,75 @@
<h1>All Albums</h1>

<ul>
<li>Album Title : song count </li>
<% @albums.each do |album| %>
<li><%= album['title'] %></li>
<% path = "/albums/#{album['id']}" %>
<li><a href=<%= path %>><%= album['title'] %></a> : <%= album['count'] %></li>
<% end %>
<!-- <li><%= @songs %></li> -->
</ul>

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

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


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

$(document).on('click', 'a.add-genre', function(e) {
e.preventDefault()
$select = $("div.genre").last()
$br = $('<br>')
$remove = $('<a>').attr('href', '#').addClass('remove-genre').text('remove genre')
$remove.append($br)
$div = $('div.genre').last()
$placement = $('a.add-genre')
if ($('a.remove-genre').length === 0) {
$div.append($remove)

// $genre = $("[name='genre']")
// $select.clone().before($placement)
// $placement.
$placement.before($div.clone())
} else {
$placement.before($div.clone())
}

})

$(document).on('click', 'a.remove-genre', function(e) {
e.preventDefault()
$remove = $("div.genre").last()
// this.remove()
// this.remove()
$remove.remove()
this.remove()
if ($('a.remove-genre').length === 1) {
$rere = $('a.remove-genre')
$rere.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>