From 29147d56d764d449690a900774eef3337f97961b Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Thu, 13 Oct 2011 17:47:19 +0400 Subject: [PATCH] Track vote methods --- app/controllers/track_controller.rb | 46 ++++++++++++++++++++++++++++ app/models/{ => music}/track_file.rb | 0 2 files changed, 46 insertions(+) rename app/models/{ => music}/track_file.rb (100%) diff --git a/app/controllers/track_controller.rb b/app/controllers/track_controller.rb index 98601f6..f70cd17 100644 --- a/app/controllers/track_controller.rb +++ b/app/controllers/track_controller.rb @@ -15,4 +15,50 @@ class TrackController < ApplicationController end render :json => { :result => :failed } end + + def report_unavailable + track = Track.find(params[:id]) + unless track.nil? or track.available + track.available = true + track.save + render :json => { :status => :success } + return + end + render :json => { :status => :failed } + end + + def report_good + track = Track.find(params[:id]) + unless track.nil? + # Track is now definitely available + track.available = true + track.save + # Saving track file + vote(params[:track_id], params[:owner_id], params[:audio_id], 1) + render :json => { :status => :success } + return + end + render :json => { :status => :failed } + end + + def report_bad + track = Track.find(params[:id]) + unless track.nil? + # Saving track file + vote(params[:track_id], params[:owner_id], params[:audio_id], -1) + render :json => { :status => :success } + return + end + render :json => { :status => :failed } + end + +private + + def vote track_id, owner_id, audio_id, delta = 0 + track_file = TrackFile.find_or_create_by_track_id_and_owner_id_and_audio_id( + track_id, owner_id, audio_id + ){ |tf| tf.rating = 0 } + track_file.rating += delta + track_file.save + end end \ No newline at end of file diff --git a/app/models/track_file.rb b/app/models/music/track_file.rb similarity index 100% rename from app/models/track_file.rb rename to app/models/music/track_file.rb