Prettify the login page
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
index 4445c07..2ac4dae 100644 (file)
@@ -1,5 +1,8 @@
 class PurchaseController < ApplicationController
 
+  # Make sure that a user logs in before doing any action here
+  before_filter :authorize
+
   def index
     redirect_to :action => :begin
   end
@@ -12,6 +15,7 @@ class PurchaseController < ApplicationController
     # enter a customer id here
     render :action => 'begin'
     session[:total] = 0.00
+    session[:items] = []
   end
 
   def customer_ok
@@ -27,6 +31,7 @@ class PurchaseController < ApplicationController
   def menu
     @customer = Customer.find_by_id(session[:customer_id])
     @total_price = session[:total]
+    @items = session[:items]
     render :action => 'menu'
   end
 
@@ -56,6 +61,35 @@ class PurchaseController < ApplicationController
       return
     end
 
+    # Check Rentable Policies
+    @maxvideos = RentablePolicy.find_by_name("MaxVideos")
+    if @rentable.class == Video and @customer.checked_out_videos >= @maxvideos.value
+      flash[:error] = "#{@maxvideos.description} LIMIT REACHED"
+      redirect_to :action => :rent_begin
+      return
+    end
+
+    @maxgames = RentablePolicy.find_by_name("MaxGames")
+    if @rentable.class == Game and @customer.checked_out_games >= @maxgames.value
+      flash[:error] = "#{@maxgames.description} LIMIT REACHED"
+      redirect_to :action => :rent_begin
+      return
+    end
+
+    @maxoverduevideos = RentablePolicy.find_by_name("MaxOverdueVideos")
+    if @rentable.class == Video and @customer.overdue_videos >= @maxoverduevideos.value
+      flash[:error] = "#{@maxoverduevideos.description} LIMIT REACHED"
+      redirect_to :action => :rent_begin
+      return
+    end
+
+    @maxoverduegames = RentablePolicy.find_by_name("MaxOverdueGames")
+    if @rentable.class == Game and @customer.overdue_games >= @maxoverduegames.value
+      flash[:error] = "#{@maxoverduegames.description} LIMIT REACHED"
+      redirect_to :action => :rent_begin
+      return
+    end
+
     # Check out the item
     checkout = Coitem.new
     checkout.customer = @customer
@@ -69,10 +103,13 @@ class PurchaseController < ApplicationController
     purchase.customer_id = session[:customer_id]
     purchase.date = Time.now.to_date
     purchase.price = @rentable.calculated_price
-    session[:total] += @rentable.calculated_price
     purchase.rentable = @rentable
     purchase.save!
 
+    # Add te session variables
+    session[:total] += @rentable.calculated_price
+    session[:items].push @rentable
+
     flash[:notice] = "Successfully made purchase"
     redirect_to :action => :menu
   end
@@ -108,11 +145,14 @@ class PurchaseController < ApplicationController
     purchase.customer_id = session[:customer_id]
     purchase.date = Time.now.to_date
     purchase.price = @merchandise.price
-    session[:total] += @merchandise.price
     purchase.merchandise = @merchandise
     purchase.quantity = 1
     @merchandise.quantity -= 1
 
+    # Add to session variables
+    session[:total] += @merchandise.price
+    session[:items].push @merchandise
+
     # Save both the merchandise (we changed the quantity) and the purchase to the log
     @merchandise.save!
     purchase.save!