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

Glenn Meyer - Songify ex #446

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
finish exercise 2 extensions
  • Loading branch information
GlennMeyer committed Dec 10, 2014
commit 2f9c492421a2cf8fa9e0d7c8b3a980ce9c201085
8 changes: 3 additions & 5 deletions lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module Songify
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
end

Expand Down Expand Up @@ -42,9 +40,9 @@ def self.save(db, 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
db.exec("DELETE FROM album_genres WHERE album_id = $1", [album_id])
db.exec("DELETE FROM songs WHERE album_id = $1", [album_id])
db.exec("DELETE FROM albums WHERE id = $1", [album_id])
end

end
Expand Down
4 changes: 3 additions & 1 deletion lib/songify/genre_repo.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Songify
class GenreRepo
def self.albums(db, genre_id)
db.exec("SELECT g.name AS genre, a.title AS title, a.id AS album_id FROM genres g JOIN album_genres j ON g.id = j.genre_id JOIN albums a ON a.id = j.album_id WHERE g.id = $1", [genre_id])
db.exec("SELECT g.name AS genre, a.title AS title, a.id AS album_id, g.id AS genre_id FROM genres g JOIN album_genres j ON g.id = j.genre_id JOIN albums a ON a.id = j.album_id WHERE g.id = $1", [genre_id])
end

def self.all(db)
Expand Down Expand Up @@ -34,6 +34,8 @@ def self.save(db, genre_data)
def self.destroy(db, genre_id)
# TODO: Delete SQL statement
# ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS GENRE AND ITS ALBUMS
db.exec("DELETE FROM album_genres WHERE genre_id = $1", [genre_id])
db.exec("DELETE FROM genres WHERE id = $1", [genre_id])
end

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 @@ -12,7 +12,7 @@ def self.find(db, song_id)
end

def self.list(db, album_id)
db.exec("SELECT a.title AS title, s.title AS name FROM songs s JOIN albums a ON a.id = s.album_id WHERE album_id = $1", [album_id]).to_a
db.exec("SELECT a.title AS title, s.title AS name, a.id AS album_id FROM songs s JOIN albums a ON a.id = s.album_id WHERE album_id = $1", [album_id]).to_a
end

def self.save(db, song_data)
Expand Down
22 changes: 19 additions & 3 deletions server.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'sinatra'
require './lib/songify.rb'
# Rack::MethodOverride

set :bind, '0.0.0.0' # This is needed for Vagrant

Expand All @@ -17,18 +18,26 @@
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_ids]
})
redirect to '/albums'
end

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

delete '/albums/:id' do
db = Songify.create_db_connection('songify_dev')
Songify::AlbumRepo.destroy(db, params[:album_id])
redirect to '/albums'
end

get '/songs' do
db = Songify.create_db_connection('songify_dev')
@songs = Songify::SongRepo.all(db)
Expand Down Expand Up @@ -61,6 +70,13 @@

get '/genres/:id' do
db = Songify.create_db_connection('songify_dev')
@genres = Songify::GenreRepo.albums(db, params[:id])
@genres = Songify::GenreRepo.find(db, params[:id])
@genres_join = Songify::GenreRepo.albums(db, params[:id])
erb :"genres/genre"
end

delete '/genres/:id' do
db = Songify.create_db_connection('songify_dev')
Songify::GenreRepo.destroy(db, params[:genre_id])
redirect to '/genres'
end
27 changes: 23 additions & 4 deletions views/albums/album.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
<a href="/">&lt;- Back to Everything</a>
<h1> <%= @songs.first['title'] %> - All Songs </h1>
<h1> <%= @albums['title'] %> - All Songs </h1>
<p>
<% @genres.each do |genre| %>
<a href="/genres/<%= genre['id'] %>"><%= genre['name'] %></a> |
<% end %>
</p>

<ul>
<% if @songs.first %>
<% @songs.each do |song| %>
<li><%= song['name'] %></li>
<li> <%= song['name'] %> </li>
<% end %>
</ul>
<% else %>
<li> This album does not have any songs. </li>
<% end %>
</ul>

<form method="post" action="/albums/<%= @albums['id'] %>">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="album_id" value="<%= @albums['id']%>">
<button type="submit" class="delete_button">Delete</button>
</form>

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).on("click", ".delete_button", function(e){
var r = confirm("Are you sure?");
if (r == false){
e.preventDefault()
}
})
</script>
30 changes: 25 additions & 5 deletions views/genres/genre.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
<a href="/">&lt;- Back to Everything</a>
<h1> <%= @genres.first['genre'] %> - All Albums </h1>
<h1> <%= @genres['name'] %> - All Albums </h1>

<ul>
<% @genres.each do |album| %>
<li><a href="/albums/<%= album['album_id']%>"><%= album['title'] %></a></li>
<% end %>
</ul>
<% if @genres_join.first %>
<% @genres_join.each do |album| %>
<li> <a href="/albums/<%= album['album_id']%>"><%= album['title'] %> </a></li>
<% end %>
<% else %>
<li> There are no albums associated with this genre. </li>
<% end %>
</ul>

<form method="post" action="/genres/<%= @genres['id'] %>">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="genre_id" value="<%= @genres['id']%>">
<button type="submit" class="delete_button">Delete</button>
</form>

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).on("click", ".delete_button", function(e){
var r = confirm("Are you sure?");
if (r == false){
e.preventDefault()
}
})
</script>