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

Songify ex complete #452

Open
wants to merge 6 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
22 changes: 13 additions & 9 deletions lib/songify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,42 @@ def self.create_db_connection(dbname)

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM albums;
DELETE FROM songs;
DELETE FROM genres;
/* TODO: Clear rest of the tables (books, etc.) */
DELETE FROM album_genres;
DELETE FROM albums;
DELETE FROM genres;
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 albums (id),
title VARCHAR
);
CREATE TABLE genres(
CREATE TABLE if not exists genres(
id SERIAL PRIMARY KEY,
name VARCHAR
);
/* TODO: Create song_genres table */
CREATE TABLE if not exists album_genres(
id SERIAL PRIMARY KEY,
album_id integer REFERENCES albums (id),
genre_id integer REFERENCES genres (id)
);
SQL
end

def self.drop_tables(db)
db.exec <<-SQL
DROP TABLE albums;
DROP TABLE songs;
DROP TABLE album_genres;
DROP TABLE genres;
/* TODO: Drop song_genres table */
DROP TABLE albums;
SQL
end
end
Expand Down
49 changes: 47 additions & 2 deletions lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@ def self.all(db)
db.exec("SELECT * FROM albums").to_a
end

def self.find(db, album_id)
db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
def self.find(db, album_id)
genres = []
result = db.exec("SELECT * FROM albums WHERE id = $1", [album_id]).first
if result
album_genres = db.exec("SELECT * FROM album_genres a JOIN genres g ON g.id = a.genre_id WHERE a.album_id = $1", [album_id]).to_a
album_genres.each do |line|
genres << {'id' => line['genre_id'], 'name' => line['name']}
end
result['genres'] = genres
end
result
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']])
self.find(db, album_data['id'])
elsif album_data['genre_ids']
album_id = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']]).first
album_data['id'] = album_id['id']
album_data['genre_ids'].each do |genre|
db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2)", [album_id['id'], genre])
end
album_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']])
Expand All @@ -29,5 +45,34 @@ def self.destroy(db, album_id)
# ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS ALBUM AND ITS GENRES
end

def self.all_songs_by_album(db, album_id)
sql = %q[
SELECT s.title FROM songs s
JOIN albums a
ON a.id = s.album_id
WHERE album_id = $1]
db.exec(sql, [album_id]).to_a
end

def self.all_songs_join_albums(db)
sql = %q[
SELECT s.title as song, a.title FROM songs s
JOIN albums a
ON a.id = s.album_id
]
db.exec(sql).to_a
end


def self.count_songs_join_albums(db)
sql = %q[
select a.id, a.title, count(s.id) as numberOfSongs
from albums a LEFT OUTER join songs s
ON a.id = s.album_id
group by a.title, a.id ;
]
db.exec(sql).to_a
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.all_album_genres(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
11 changes: 11 additions & 0 deletions public/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$(function() {
var select = $('select');
var link = $('a');
select.on('change', function(event) {
var el = $(event.target);
var id = el.val();
var href = link.attr('href');
href = href + '/' + id;
link.attr('href', href);
});
});
75 changes: 75 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
h1{
font-size: 2rem;
}
div.wrapper {
width: 90%;
margin: 0 auto;
margin-top: 10%;
padding-left: 4%;
background-color: #e7a61a;

border-radius: 53px 53px 53px 53px;
-moz-border-radius: 53px 53px 53px 53px;
-webkit-border-radius: 53px 53px 53px 53px;
border: 0px dotted #000000;
}

div.main{
padding-left: 35%;
}
button {
background-color:#571909;
width: 30%;
border-radius: 4%;
}


button.btn {
background: #34d9bd;
background-image: -webkit-linear-gradient(top, #34d9bd, #2980b9);
background-image: -moz-linear-gradient(top, #34d9bd, #2980b9);
background-image: -ms-linear-gradient(top, #34d9bd, #2980b9);
background-image: -o-linear-gradient(top, #34d9bd, #2980b9);
background-image: linear-gradient(to bottom, #34d9bd, #2980b9);
-webkit-border-radius: 52;
-moz-border-radius: 52;
border-radius: 52px;
font-family: Georgia;
color: #ffffff;
font-size: 20px;
padding: 8px 27px 10px 20px;
text-decoration: none;
}

button.btn:hover {
background: #3cb0fd;
background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
text-decoration: none;
}
input[type="text"].input_album{
width: 60%;
}

input[type="text"].select, select.select {
width: 60%;
}
form.album-show{
color: pink;

}

select.album-title{
width: 60%;
}
ul
{
list-style-type: none;
}
a.main-links{
font-size: 1.2rem;
color: blue;
}
51 changes: 46 additions & 5 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,23 +10,26 @@
get '/albums' do
db = Songify.create_db_connection('songify_dev')
@albums = Songify::AlbumRepo.all(db)
@songs = Songify::AlbumRepo.count_songs_join_albums(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[:genre_ids]
})
redirect to '/albums'
end


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


get '/genres' do
db = Songify.create_db_connection('songify_dev')
@genres = Songify::GenreRepo.all(db)
Expand All @@ -35,8 +38,46 @@

post '/genres' do
db = Songify.create_db_connection('songify_dev')
album = Songify::GenreRepo.save(db, {
genre = Songify::GenreRepo.save(db, {
'name' => params[:name]
})
redirect to '/genres'
end

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

post '/albums/:id/songs' do
db = Songify.create_db_connection('songify_dev')
song = Songify::SongRepo.save(db, {
'title' => params[:title], 'album_id' => params[:id]
})
# song = Songify::SongRepo.save(db, {'title' => 'Ciao', 'album_id' =>2
# })
redirect to '/albums/' + params[:id]
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[:genre_ids].to_a
})

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

post '/albums/show' do
db = Songify.create_db_connection('songify_dev')
id = params['album_id']
redirect to "/albums/#{id}/"
end
17 changes: 5 additions & 12 deletions spec/repos/album_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,15 @@ 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_data = repo.save(db, {
'title' => 'Suspicious Activity?',
'genre_ids' => [gid_1['id'], gid_2['id'], gid_3['id']]
})
album = repo.find(db, album_data['id'])

album = repo.save(db, { 'title' => 'Suspicious Activity?',
'genre_ids' => [gid_1['id'], gid_2['id'], gid_3['id']] })
album = repo.find(db, album['id'])
expect(album['genres'].count).to eq 3
genre = album['genres'].first
expect(genre).to be_a Hash
expect(genre['id']).to_not be_nil
expect(genre['name']).to_not be_nil

names = album['genres'].map {|g| g['name'] }
expect(names).to include 'rock', 'avant-garde', 'jazz'
Expand All @@ -83,4 +76,4 @@ def album_count
expect(song3['title']).to eq "Alicia"
end

end
end
Loading