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

Charles Milam - Songify ex #451

Open
wants to merge 19 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.bundle
vendor/bundle
.DS_Store
.gems
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'https://rubygems.org'
ruby '2.0.0'
ruby '2.1.3'

gem 'rspec', '~> 2.14.1'
gem 'pry-byebug'
Expand Down
36 changes: 25 additions & 11 deletions lib/songify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

module Songify
def self.create_db_connection(dbname)
PG.connect(host: 'localhost', dbname: dbname)
PG.connect(dbname: dbname)
end

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 albums cascade;
DELETE FROM songs cascade;
DELETE FROM genres cascade;
delete from album_genres cascade;
SQL
end

Expand All @@ -22,27 +22,41 @@ def self.create_tables(db)
);
CREATE TABLE songs(
id SERIAL PRIMARY KEY,
album_id integer REFERENCES albums (id),
album_id integer REFERENCES albums (id)
on delete cascade
on update cascade,
title VARCHAR
);
CREATE TABLE genres(
id SERIAL PRIMARY KEY,
name VARCHAR
);
/* TODO: Create song_genres table */
create table album_genres(
id serial primary key,
album_id integer references albums (id)
on delete cascade
on update cascade,
genres_id integer references genres (id)
on delete cascade
on update cascade
)
SQL
end

def self.drop_tables(db)
db.exec <<-SQL
DROP TABLE albums;
DROP TABLE songs;
DROP TABLE genres;
/* TODO: Drop song_genres table */
DROP TABLE if exists albums cascade;
DROP TABLE if exists songs cascade;
DROP TABLE if exists genres cascade;
drop table if exists album_genres cascade;
SQL
end
end

require_relative 'songify/album_repo'
require_relative 'songify/genre_repo'
require_relative 'songify/song_repo'

# db = Songify.create_db_connection("songify_dev")
# Songify.drop_tables(db)
# Songify.create_tables(db)
40 changes: 39 additions & 1 deletion lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,55 @@ def self.all(db)
end

def self.find(db, album_id)
db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
result = db.exec("SELECT * FROM albums WHERE id = $1", [album_id]).first

if result
genres = []
genre_result = {}
sql = %Q[
SELECT
albums.title,
genres.name
FROM
album_genres,
genres,
albums
WHERE
album_genres.genres_id = genres.id AND
album_genres.album_id = $1
]
genre_result = db.exec(sql, [album_id])

genre_result.each do |r|
genres << {'id' => r['genre_id'], 'name' => r['name']}
end
result['genres'] = genres
end
result
end

def self.save(db, album_data)
#puts 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'])
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']])
#puts "album result", result.entries.first
album_data['id'] = result.entries.first['id']

if album_data["genre_ids"]
sql = %Q[
insert into album_genres
(album_id, genres_id)
values ($1, $2)
]
album_data["genre_ids"].each do |g_id|
db.exec(sql, [album_data["id"].to_i, g_id.to_i])
end
end

album_data
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 @@ -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) VALUES ($1) RETURNING id", [song_data["title"]])
song_data['id'] = result.entries.first['id']
song_data
end
Expand Down
16 changes: 14 additions & 2 deletions server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@
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[:genres]
})
redirect to '/albums'
end


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")
puts params.inspect
Songify::SongRepo.save(db, {"title" => params[:title], "album_id" => params[:album_id]})

redirect back
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
61 changes: 47 additions & 14 deletions views/albums/index.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
<a href="/">&lt;- Back to Everything</a>
<h1>All Albums</h1>

<ul>
<% @albums.each do |album| %>
<li><%= album['title'] %></li>
<% end %>
</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>
<h1>All Albums</h1>

<ul>
<% @albums.each do |album| %>
<li><%= album['title'] %></li>
<% end %>
</ul>

<form action="/albums" method="post">
<h3>New Album</h3>
<label>Album Title:</label>
<input name="title" type="text" />
<label>Genre:</label>
<select class="genres" name="genres[]" style="width: 25%; margins: auto">
<% @genres.each do |g| %>
<option value=<%= g["id"] %>><%= g["name"] %></option>
<% end %>
</select><br />

<a href="#" class="add-genre">add another genre</a><br />
&nbsp;&nbsp;&nbsp;<div><button>Create Album</button></div><br />


</form>
</div>

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(".add-genre").click(function(e) {
e.preventDefault()
console.log("in add")
$(this).before( $(".genres").last().clone())
$(this).before("<a href='#' class='remove-genre' style='margin: auto auto auto 10px;'>remove this genre</a>")
$(this).before( "<br />")

})

$(document).on("click", ".remove-genre", function(e) {
e.preventDefault()
console.log("in remove")
$(this).prev().remove()
$(this).prev().remove()
$(this).remove()
})

</script>
26 changes: 14 additions & 12 deletions views/genres/index.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<a href="/">&lt;- Back to Everything</a>
<h1>All Genres</h1>
<div>
<h1>All Genres</h1>

<ul>
<% @genres.each do |genre| %>
<li><%= genre['name'] %></li>
<% end %>
</ul>
<ul>
<% @genres.each do |genre| %>
<li><%= genre['name'] %></li>
<% end %>
</ul>

<form action="/genres" method="post">
<h3>New Genre</h3>
<label>Genre Name:</label>
<input name="name" type="text" />
<button>Create Genre</button>
</form>
<form action="/genres" method="post">
<h3>New Genre</h3>
<label>Genre Name:</label>
<input name="name" type="text" />
<button>Create Genre</button>
</form>
</div>
15 changes: 9 additions & 6 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<h1>The Songify System</h1>
<div>
<h1>The Songify System</h1>

<p>You're gonna be big</p>
<p>You're gonna be big</p>

<ul>
<li><a href="/albums">View Albums</a></li>
<li><a href="/genres">View Genres</a></li>
</ul>
<ul>
<li><a href="/albums">View/Add Albums</a></li>
<li><a href="/songs">View/Add Songs</a>
<li><a href="/genres">View/Add Genres</a></li>
</ul>
</div>
20 changes: 20 additions & 0 deletions views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>Songify</title>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/foundation/5.1.1/css/foundation.css">
<style type="text/css">
div {
width: 50%;
margin: auto;
}
ul {
list-style-type: none;
}
</style>
</head>
<body>
<%= yield %>
<br /><br /><br />
</body>
</html>
16 changes: 16 additions & 0 deletions views/songs/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<a href="/">&lt;- Back to Everything</a>
<div>
<form method="post" action="/songs">
<h3>Add a Song</h3>
<label>Title:</label>
<input type="text" name="title" />
<label>Album:</label>
<select name="album_id">
<% @albums.each do |a| %>
<option value=<%= a["id"] %>><%= a["title"] %></option>
<% end %>
</select>

<button>Add Song</button>
</form>
</div>