ee34e5ca0b8aeb7d47ff310af5fb1bdcbca5e423
[cs356-p2-videostore.git] / app / controllers / video_controller.rb
1 class VideoController < ApplicationController
2
3   # Make sure that a user logs in before doing any action here
4   before_filter :authorize, :except => :login
5
6   def index
7     list
8     render :action => 'list'
9   end
10
11   # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
12   verify :method => :post, :only => [ :destroy, :create, :update ],
13          :redirect_to => { :action => :list }
14
15   def list
16     @video_pages, @videos = paginate :videos, :per_page => 10
17   end
18
19   def show
20     @video = Video.find(params[:id])
21   end
22
23   def new
24     @video = Video.new
25   end
26
27   def create
28     @video = Video.new(params[:video])
29     if @video.save
30       flash[:notice] = 'Video was successfully created.'
31       redirect_to :action => 'list'
32     else
33       render :action => 'new'
34     end
35   end
36
37   def edit
38     @video = Video.find(params[:id])
39   end
40
41   def update
42     @video = Video.find(params[:id])
43     if @video.update_attributes(params[:video])
44       flash[:notice] = 'Video was successfully updated.'
45       redirect_to :action => 'show', :id => @video
46     else
47       render :action => 'edit'
48     end
49   end
50
51   def destroy
52     Video.find(params[:id]).destroy
53     redirect_to :action => 'list'
54   end
55
56   def searchbyname
57     render :action => 'searchbyname'
58   end
59
60   def searchresults
61     query = params[:q]
62     @videos = Video.find(:all, :conditions => ["title like ?", query[0]+"%"])
63   end
64 end