diff --git a/.gitignore b/.gitignore index e941da68..60f8eafa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .bundle vendor/bundle .DS_Store +.gems diff --git a/Gemfile b/Gemfile index cc9b3f83..1e4d1192 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,5 @@ source 'https://rubygems.org' -ruby '2.0.0' +ruby '2.1.3' gem 'rspec', '~> 2.14.1' gem 'pry-byebug' diff --git a/lib/songify.rb b/lib/songify.rb index 63a55233..80a13633 100644 --- a/lib/songify.rb +++ b/lib/songify.rb @@ -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 @@ -22,23 +22,33 @@ 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 @@ -46,3 +56,7 @@ def self.drop_tables(db) 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) \ No newline at end of file diff --git a/lib/songify/album_repo.rb b/lib/songify/album_repo.rb index 54b55451..136b4b9a 100644 --- a/lib/songify/album_repo.rb +++ b/lib/songify/album_repo.rb @@ -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 diff --git a/lib/songify/song_repo.rb b/lib/songify/song_repo.rb index a9fc10c0..9c89ed0e 100644 --- a/lib/songify/song_repo.rb +++ b/lib/songify/song_repo.rb @@ -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 diff --git a/server.rb b/server.rb index 361bacc7..ced49e4f 100644 --- a/server.rb +++ b/server.rb @@ -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') diff --git a/spec/repos/album_repo_spec.rb b/spec/repos/album_repo_spec.rb index 3f080f05..931ebde1 100644 --- a/spec/repos/album_repo_spec.rb +++ b/spec/repos/album_repo_spec.rb @@ -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' }) diff --git a/views/albums/index.erb b/views/albums/index.erb index fa67cd23..6101982f 100644 --- a/views/albums/index.erb +++ b/views/albums/index.erb @@ -1,15 +1,48 @@ <- Back to Everything -

All Albums

- - - -
-

New Album

- - - -
+
+

All Albums

+ + + +
+

New Album

+ + + +
+ + add another genre
+    

+ + +
+
+ + + diff --git a/views/genres/index.erb b/views/genres/index.erb index e62f9b7d..2e7299d0 100644 --- a/views/genres/index.erb +++ b/views/genres/index.erb @@ -1,15 +1,17 @@ <- Back to Everything -

All Genres

+
+

All Genres

- + -
-

New Genre

- - - -
+
+

New Genre

+ + + +
+
diff --git a/views/index.erb b/views/index.erb index 8f52ed98..1b460b07 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,8 +1,11 @@ -

The Songify System

+
+

The Songify System

-

You're gonna be big

+

You're gonna be big

- + +
diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 00000000..fdc92a27 --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,20 @@ + + + + Songify + + + + +<%= yield %> +


+ + \ No newline at end of file diff --git a/views/songs/index.erb b/views/songs/index.erb new file mode 100644 index 00000000..1f0230d8 --- /dev/null +++ b/views/songs/index.erb @@ -0,0 +1,16 @@ +<- Back to Everything +
+
+

Add a Song

+ + + + + + +
+
\ No newline at end of file