From 9558f720cad039e92492d9ff877dab17473787d9 Mon Sep 17 00:00:00 2001 From: magnolia-fan Date: Wed, 22 Jun 2011 04:05:12 +0400 Subject: [PATCH] Favorite and playlist models --- app/controllers/user_controller.rb | 30 +++++++++++++++++++ app/models/favorite.rb | 6 ++++ app/models/playlist.rb | 4 +++ app/models/playlist_item.rb | 4 +++ db/migrate/20110621233641_create_favorites.rb | 16 ++++++++++ db/migrate/20110622000213_create_playlists.rb | 14 +++++++++ .../20110622000253_create_playlist_items.rb | 14 +++++++++ test/fixtures/favorites.yml | 13 ++++++++ test/fixtures/playlist_items.yml | 9 ++++++ test/fixtures/playlists.yml | 9 ++++++ test/unit/favorite_test.rb | 8 +++++ test/unit/playlist_item_test.rb | 8 +++++ test/unit/playlist_test.rb | 8 +++++ 13 files changed, 143 insertions(+) create mode 100644 app/models/favorite.rb create mode 100644 app/models/playlist.rb create mode 100644 app/models/playlist_item.rb create mode 100644 db/migrate/20110621233641_create_favorites.rb create mode 100644 db/migrate/20110622000213_create_playlists.rb create mode 100644 db/migrate/20110622000253_create_playlist_items.rb create mode 100644 test/fixtures/favorites.yml create mode 100644 test/fixtures/playlist_items.yml create mode 100644 test/fixtures/playlists.yml create mode 100644 test/unit/favorite_test.rb create mode 100644 test/unit/playlist_item_test.rb create mode 100644 test/unit/playlist_test.rb diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index a4a3424..c8259f3 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -43,6 +43,36 @@ class UserController < ApplicationController render :json => @res end + def fav + @res = {status: 'fail'} + check = check_auth(params) + + if check == true + fav = Favorite.new + if not params[:artist].nil? + artist = Artist.find(params[:artist]); + unless artist.nil? + fav.artist_id = artist.id + res[:status] = 'added' + end + elsif not params[:album].nil? + album = Album.find(params[:album]); + unless album.nil? + fav.album_id = album.id + res[:status] = 'added' + end + elsif not params[:track].nil? + track = Track.find(params[:track]); + unless track.nil? + fav.track_id = track.id + res[:status] = 'added' + end + end + end + + render :json => @res + end + private def check_auth params diff --git a/app/models/favorite.rb b/app/models/favorite.rb new file mode 100644 index 0000000..39c7718 --- /dev/null +++ b/app/models/favorite.rb @@ -0,0 +1,6 @@ +class Favorite < ActiveRecord::Base + belongs_to :user + belongs_to :artist + belongs_to :album + belongs_to :track +end diff --git a/app/models/playlist.rb b/app/models/playlist.rb new file mode 100644 index 0000000..3708723 --- /dev/null +++ b/app/models/playlist.rb @@ -0,0 +1,4 @@ +class Playlist < ActiveRecord::Base + belongs_to :user + has_many :playlist_tracks +end diff --git a/app/models/playlist_item.rb b/app/models/playlist_item.rb new file mode 100644 index 0000000..93145e8 --- /dev/null +++ b/app/models/playlist_item.rb @@ -0,0 +1,4 @@ +class PlaylistItem < ActiveRecord::Base + belongs_to :playlist + belongs_to :track +end diff --git a/db/migrate/20110621233641_create_favorites.rb b/db/migrate/20110621233641_create_favorites.rb new file mode 100644 index 0000000..30cbc3e --- /dev/null +++ b/db/migrate/20110621233641_create_favorites.rb @@ -0,0 +1,16 @@ +class CreateFavorites < ActiveRecord::Migration + def self.up + create_table :favorites do |t| + t.integer :user_id + t.integer :artist_id + t.integer :album_id + t.integer :track_id + + t.timestamps + end + end + + def self.down + drop_table :favorites + end +end diff --git a/db/migrate/20110622000213_create_playlists.rb b/db/migrate/20110622000213_create_playlists.rb new file mode 100644 index 0000000..c66f72a --- /dev/null +++ b/db/migrate/20110622000213_create_playlists.rb @@ -0,0 +1,14 @@ +class CreatePlaylists < ActiveRecord::Migration + def self.up + create_table :playlists do |t| + t.integer :user_id + t.string :name + + t.timestamps + end + end + + def self.down + drop_table :playlists + end +end diff --git a/db/migrate/20110622000253_create_playlist_items.rb b/db/migrate/20110622000253_create_playlist_items.rb new file mode 100644 index 0000000..77270d6 --- /dev/null +++ b/db/migrate/20110622000253_create_playlist_items.rb @@ -0,0 +1,14 @@ +class CreatePlaylistItems < ActiveRecord::Migration + def self.up + create_table :playlist_items do |t| + t.integer :playlist_id + t.integer :track_id + + t.timestamps + end + end + + def self.down + drop_table :playlist_items + end +end diff --git a/test/fixtures/favorites.yml b/test/fixtures/favorites.yml new file mode 100644 index 0000000..cb11d29 --- /dev/null +++ b/test/fixtures/favorites.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + user_id: 1 + artist_id: 1 + album_id: 1 + track_id: 1 + +two: + user_id: 1 + artist_id: 1 + album_id: 1 + track_id: 1 diff --git a/test/fixtures/playlist_items.yml b/test/fixtures/playlist_items.yml new file mode 100644 index 0000000..68079fe --- /dev/null +++ b/test/fixtures/playlist_items.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + playlist_id: 1 + track_id: 1 + +two: + playlist_id: 1 + track_id: 1 diff --git a/test/fixtures/playlists.yml b/test/fixtures/playlists.yml new file mode 100644 index 0000000..7473351 --- /dev/null +++ b/test/fixtures/playlists.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + user_id: 1 + name: MyString + +two: + user_id: 1 + name: MyString diff --git a/test/unit/favorite_test.rb b/test/unit/favorite_test.rb new file mode 100644 index 0000000..4067023 --- /dev/null +++ b/test/unit/favorite_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class FavoriteTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/test/unit/playlist_item_test.rb b/test/unit/playlist_item_test.rb new file mode 100644 index 0000000..96466b9 --- /dev/null +++ b/test/unit/playlist_item_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class PlaylistItemTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/test/unit/playlist_test.rb b/test/unit/playlist_test.rb new file mode 100644 index 0000000..770f9ab --- /dev/null +++ b/test/unit/playlist_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class PlaylistTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end