Favorite and playlist models
This commit is contained in:
parent
9531230f6e
commit
9558f720ca
@ -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
6
app/models/favorite.rb
Normal 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
4
app/models/playlist.rb
Normal file
@ -0,0 +1,4 @@
|
||||
class Playlist < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
has_many :playlist_tracks
|
||||
end
|
4
app/models/playlist_item.rb
Normal file
4
app/models/playlist_item.rb
Normal file
@ -0,0 +1,4 @@
|
||||
class PlaylistItem < ActiveRecord::Base
|
||||
belongs_to :playlist
|
||||
belongs_to :track
|
||||
end
|
16
db/migrate/20110621233641_create_favorites.rb
Normal file
16
db/migrate/20110621233641_create_favorites.rb
Normal 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
|
14
db/migrate/20110622000213_create_playlists.rb
Normal file
14
db/migrate/20110622000213_create_playlists.rb
Normal 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
|
14
db/migrate/20110622000253_create_playlist_items.rb
Normal file
14
db/migrate/20110622000253_create_playlist_items.rb
Normal 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
13
test/fixtures/favorites.yml
vendored
Normal 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
9
test/fixtures/playlist_items.yml
vendored
Normal 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
9
test/fixtures/playlists.yml
vendored
Normal 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
|
8
test/unit/favorite_test.rb
Normal file
8
test/unit/favorite_test.rb
Normal 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
|
8
test/unit/playlist_item_test.rb
Normal file
8
test/unit/playlist_item_test.rb
Normal 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
|
8
test/unit/playlist_test.rb
Normal file
8
test/unit/playlist_test.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user