1
0
Fork 0

Favorite and playlist models

This commit is contained in:
magnolia-fan 2011-06-22 04:05:12 +04:00
parent 9531230f6e
commit 9558f720ca
13 changed files with 143 additions and 0 deletions

View File

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

6
app/models/favorite.rb Normal file
View File

@ -0,0 +1,6 @@
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :artist
belongs_to :album
belongs_to :track
end

4
app/models/playlist.rb Normal file
View File

@ -0,0 +1,4 @@
class Playlist < ActiveRecord::Base
belongs_to :user
has_many :playlist_tracks
end

View File

@ -0,0 +1,4 @@
class PlaylistItem < ActiveRecord::Base
belongs_to :playlist
belongs_to :track
end

View File

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

View File

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

View File

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

13
test/fixtures/favorites.yml vendored Normal file
View File

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

9
test/fixtures/playlist_items.yml vendored Normal file
View File

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

9
test/fixtures/playlists.yml vendored Normal file
View File

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

View File

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

View File

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

View File

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