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

Rufino: Songify-Ex #442

Open
wants to merge 3 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 Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gem 'pry-byebug'
gem 'pg'
gem 'sinatra'
gem 'rake'
gem 'sinatra-contrib'
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
backports (3.6.4)
byebug (3.5.1)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
Expand All @@ -10,6 +11,7 @@ GEM
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
method_source (0.8.2)
multi_json (1.10.1)
pg (0.17.1)
pry (0.10.1)
coderay (~> 1.1.0)
Expand All @@ -21,6 +23,8 @@ GEM
rack (1.5.2)
rack-protection (1.5.3)
rack
rack-test (0.6.2)
rack (>= 1.0)
rake (10.3.2)
rspec (2.14.1)
rspec-core (~> 2.14.0)
Expand All @@ -34,6 +38,13 @@ GEM
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
sinatra-contrib (1.4.2)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (~> 1.3)
slop (3.6.0)
tilt (1.4.1)

Expand All @@ -46,3 +57,4 @@ DEPENDENCIES
rake
rspec (~> 2.14.1)
sinatra
sinatra-contrib
110 changes: 61 additions & 49 deletions lib/songify.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,61 @@
require 'pg'

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

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM songs;
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(
id SERIAL PRIMARY KEY,
title VARCHAR
);
CREATE TABLE songs(
id SERIAL PRIMARY KEY,
album_id integer REFERENCES albums (id),
title VARCHAR
);
CREATE TABLE genres(
id SERIAL PRIMARY KEY,
name VARCHAR
);
/* TODO: Create song_genres table */
SQL
end

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

require_relative 'songify/errors/invalid_record_data.rb'
require_relative 'songify/album_repo'
require_relative 'songify/genre_repo'
require_relative 'songify/song_repo'
require 'pg'

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

def self.clear_db(db)
db.exec <<-SQL
DELETE FROM album_genres;
DELETE FROM songs;
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 IF NOT EXISTS albums(
id SERIAL PRIMARY KEY,
title VARCHAR
);
CREATE TABLE IF NOT EXISTS songs(
id SERIAL PRIMARY KEY,
album_id integer REFERENCES albums (id)
on delete cascade
on update cascade,
title VARCHAR
);
CREATE TABLE IF NOT EXISTS genres(
id SERIAL PRIMARY KEY,
name VARCHAR
);
CREATE TABLE IF NOT EXISTS album_genres(
id SERIAL PRIMARY KEY,
album_id integer REFERENCES albums
on delete cascade
on update cascade,
genre_id integer REFERENCES genres
);
/* TODO: Create song_genres table */
SQL
end

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

require_relative 'songify/errors/invalid_record_data.rb'
require_relative 'songify/album_repo'
require_relative 'songify/genre_repo'
require_relative 'songify/song_repo'
88 changes: 52 additions & 36 deletions lib/songify/album_repo.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
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

def self.find(db, album_id)
db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
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'])
else
if album_data['title'].nil? || album_data['title'] == ''
raise Errors::InvalidRecordData.new("title is required.")
end

result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']])
album_data['id'] = result.entries.first['id']
album_data
end
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
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

def self.find(db, album_id)
album_result = db.exec("SELECT * FROM albums WHERE id=$1", [album_id]).first
album_info = db.exec("SELECT g.name, g.id FROM genres g JOIN album_genres ag ON g.id = ag.genre_id JOIN albums a ON a.id = ag.album_id WHERE ag.album_id = $1", [album_id]).to_a
if album_info[0]
final_genre = []
album_info.each do |a| #Push the album_info Hash
final_genre.push(a) #Array of Hashes { "(genre)id" => "x", "(genre)name" => "y"}, in this case, just one Hash
end
album_result['genres'] = final_genre
return album_result
end
album_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']) #Brings back the first HASH
else
if album_data['title'].nil? || album_data['title'] == ''
raise Errors::InvalidRecordData.new("title is required.")
end

result = db.exec("INSERT INTO albums (title) VALUES ($1) RETURNING id", [album_data['title']])
album_data['id'] = result.entries.first['id']

if album_data['genre_ids']
album_data['genre_ids'].each do |genre|
db.exec("INSERT INTO album_genres (album_id, genre_id) VALUES ($1, $2) RETURNING id", [album_data['id'], genre])
end
end
album_data #Returns this
end
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
71 changes: 36 additions & 35 deletions lib/songify/genre_repo.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
module Songify
class GenreRepo

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 genres").to_a
end

def self.find(db, genre_id)
db.exec("SELECT * FROM genres WHERE id=$1", [genre_id]).first
end

def self.save(db, genre_data)
if genre_data['id']
result = db.exec("UPDATE genres SET name = $2 WHERE id = $1", [genre_data['id'], genre_data['name']])
self.find(db, genre_data['id'])
else
if genre_data['name'].nil? || genre_data['name'] == ''
raise Errors::InvalidRecordData.new("name is required.")
end

result = db.exec("INSERT INTO genres (name) VALUES ($1) RETURNING id", [genre_data['name']])
genre_data['id'] = result.entries.first['id']
genre_data
end
end

def self.destroy(db, genre_id)
# TODO: Delete SQL statement
# ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS GENRE AND ITS ALBUMS
end

end
end
module Songify
class GenreRepo

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 genres").to_a
end

def self.find(db, genre_id)
db.exec("SELECT * FROM genres WHERE id=$1", [genre_id]).first
end

def self.save(db, genre_data)
if genre_data['id']
result = db.exec("UPDATE genres SET name = $2 WHERE id = $1", [genre_data['id'], genre_data['name']])
self.find(db, genre_data['id'])
else

if genre_data['name'].nil? || genre_data['name'] == ''
raise Errors::InvalidRecordData.new("name is required.")
end

result = db.exec("INSERT INTO genres (name) VALUES ($1) RETURNING id", [genre_data['name']])
genre_data['id'] = result.entries.first['id']
genre_data
end
end

def self.destroy(db, genre_id)
# TODO: Delete SQL statement
# ALSO DELETE JOIN TABLE ENTRIES BETWEEN THIS GENRE AND ITS ALBUMS
end

end
end
72 changes: 36 additions & 36 deletions lib/songify/song_repo.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
module Songify
class SongRepo

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 songs").to_a
end

def self.find(db, song_id)
db.exec("SELECT * FROM songs WHERE id=$1", [song_id]).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']])
self.find(db, song_data['id'])
else
if song_data['title'].nil? || song_data['title'] == ''
raise Errors::InvalidRecordData.new("title is required.")
end

# Ensure album exists
album = AlbumRepo.find(db, song_data['album_id'])
if album.nil?
raise raise Errors::InvalidRecordData.new("A valid album_id is required.")
end

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
end
end

end
end
module Songify
class SongRepo
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 songs").to_a
end
def self.find(db, song_id)
db.exec("SELECT * FROM songs WHERE id=$1", [song_id]).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']])
self.find(db, song_data['id'])
else
if song_data['title'].nil? || song_data['title'] == ''
raise Errors::InvalidRecordData.new("A title is required.")
end
# Ensure album exists
album = AlbumRepo.find(db, song_data['album_id'])
if album.nil?
raise raise Errors::InvalidRecordData.new("A valid album_id is required.")
end
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 #returns Hash { "id" => "x", "album_id" => "y", title" => "z"}
end
end
end
end
Loading