129 lines
3.6 KiB
Ruby
129 lines
3.6 KiB
Ruby
module JamRuby
|
|
class Recording < ActiveRecord::Base
|
|
|
|
self.primary_key = 'id'
|
|
|
|
has_many :musician_recordings, :class_name => "JamRuby::MusicianRecording"
|
|
has_many :band_recordings, :class_name => "JamRuby::BandRecording"
|
|
|
|
# genres
|
|
has_and_belongs_to_many :genres, :class_name => "JamRuby::Genre", :join_table => "recordings_genres"
|
|
|
|
# favorites
|
|
has_and_belongs_to_many :user_favorites, :class_name => "JamRuby::UserFavorite", :join_table => "users_favorites"
|
|
|
|
validates :description, presence: true, length: { maximum: 200 }
|
|
|
|
def self.save(id, is_public, description, genres, updater_id, owner_id, is_band)
|
|
|
|
creator = User.find(updater_id)
|
|
|
|
if is_band
|
|
band = Band.find(owner_id)
|
|
validate_user_is_band_member(creator, band)
|
|
else
|
|
user = User.find(owner_id)
|
|
validate_user_is_creator(user, creator)
|
|
validate_user_is_musician(user)
|
|
end
|
|
|
|
if id.nil?
|
|
validate_genres(genres, false)
|
|
recording = Recording.new()
|
|
recording.creator_id = updater_id
|
|
else
|
|
validate_genres(genres, true)
|
|
recording = Recording.find(id)
|
|
end
|
|
|
|
recording.updater_id = updater_id
|
|
|
|
# public flag
|
|
unless is_public.nil?
|
|
recording.public = is_public
|
|
end
|
|
|
|
# description
|
|
unless description.nil?
|
|
recording.description = description
|
|
end
|
|
|
|
# genres
|
|
unless genres.nil?
|
|
ActiveRecord::Base.transaction do
|
|
# delete all genres for this recording first
|
|
unless recording.id.nil? || recording.id.length == 0
|
|
recording.genres.delete_all
|
|
end
|
|
|
|
# loop through each genre in the array and save to the db
|
|
genres.each do |genre_id|
|
|
g = Genre.find(genre_id)
|
|
recording.genres << g
|
|
end
|
|
end
|
|
end
|
|
|
|
recording.updated_at = Time.now.getutc
|
|
|
|
# TODO: wrap in transaction with statements below
|
|
recording.save
|
|
|
|
if id.nil?
|
|
if is_band
|
|
recording.band_recordings << BandRecording.create(band_id: owner_id, recording_id: recording.id)
|
|
else
|
|
recording.musician_recordings << MusicianRecording.create(user_id: owner_id, recording_id: recording.id)
|
|
end
|
|
end
|
|
|
|
return recording
|
|
end
|
|
|
|
private
|
|
def self.validate_user_is_band_member(user, band)
|
|
unless band.users.exists? user
|
|
raise PermissionError, ValidationMessages::USER_NOT_BAND_MEMBER_VALIDATION_ERROR
|
|
end
|
|
end
|
|
|
|
def self.validate_user_is_creator(user, creator)
|
|
unless user.id == creator.id
|
|
raise PermissionError, ValidationMessages::PERMISSION_VALIDATION_ERROR
|
|
end
|
|
end
|
|
|
|
def self.validate_user_is_musician(user)
|
|
unless user.musician?
|
|
raise PermissionError, ValidationMessages::USER_NOT_MUSICIAN_VALIDATION_ERROR
|
|
end
|
|
end
|
|
|
|
def self.validate_genres(genres, is_nil_ok)
|
|
if is_nil_ok && genres.nil?
|
|
return
|
|
end
|
|
|
|
if genres.nil?
|
|
raise JamRuby::JamArgumentError, ValidationMessages::GENRE_MINIMUM_NOT_MET
|
|
else
|
|
if genres.size < Limits::MIN_GENRES_PER_RECORDING
|
|
raise JamRuby::JamArgumentError, ValidationMessages::GENRE_MINIMUM_NOT_MET
|
|
end
|
|
|
|
if genres.size > Limits::MAX_GENRES_PER_RECORDING
|
|
raise JamRuby::JamArgumentError, ValidationMessages::GENRE_LIMIT_EXCEEDED
|
|
end
|
|
end
|
|
end
|
|
|
|
=begin
|
|
def self.delete(id, owner_id, is_band)
|
|
if is_band?
|
|
JamRuby::Recording.delete_all "(user_id = '#{user_id}' AND follower_id = '#{follower_id}')"
|
|
else
|
|
end
|
|
end
|
|
=end
|
|
end
|
|
end |