class JobController < ApplicationController
  before_filter :login_required  

  def create
    job = Job.new
    job.user = current_user
    job.save!
    redirect_to :action => :edit, :id => job.id
  end
  
  def edit
    @job = Job.find(params[:id])
    if @job.user != current_user && !current_user.admin
      flash[:notice] = 'Cannot edit other user\'s jobs'
      redirect_to :action => :list
    end
  end

  def update
    job = Job.find(params[:job][:id])
    if @job.user != current_user && !current_user.admin
      flash[:notice] = 'Cannot edit other user\'s jobs'
      redirect_to :action => :list
    else
      job.update_attributes!(params[:job])
      redirect_to :action => :show, :id => job.id
    end
  end

  def delete
    if Job.find(params[:id]).user != current_user && !current_user.admin
      flash[:notice] = 'Cannot delete other user\'s jobs'
    else
      Job.destroy(params[:id])
      flash[:notice] = "Job #{params[:id]} deleted "
    end
    redirect_to :action => :list
  end

  def show
    job = Job.find(params[:id])
  end

  def list
    @jobs = Job.find(:all, :conditions => ['user_id = ?', current_user.id])
  end

end
