[Catalyst] How to load all the methods on to the catalyst

chveerendra nadh chchveeru at gmail.com
Thu Feb 9 14:18:37 CET 2006


*Here is my code......Please help me t create sessions using database
tables..........i mean creating a session table and storing sessions which
were created, into the Session Table....... *


package  Shop;

use strict;
use Catalyst qw/-Debug /;

BEGIN { use lib qw( /var/www/lib ); }
our $VERSION = '0.01';

use  Conf;
use  Shop::Model::Session;

 Shop->config(
        name => ' Shop',

        root => '/var/www/Shop/root/template'
 );

Shop->setup;


sub authenticate {

        my ($self, $c) = @_;

        # get the session related to this request
        $c->{session} =  Shop::Model::Session->get_create_session($c);

        return DECLINED unless $c->{session};

        return OK;
}



sub default : Private {
        my ( $self, $c ) = @_;
        $c->stash->{template} = 'index.tt';
}

sub end : Private {
        my ( $self, $c ) = @_;
        # set the content type to html and default charset, otherwise
charset is defaulting to utf-8
        $c->res->headers->content_type('text/html;') unless
$c->res->headers->content_type();
        $c->forward(' Shop::V::TT') unless( $c->res->output ||
!$c->stash->{template});
}


1;


*And Here is my Shop::Controller::Basket program please go through them and
help me..........*



package  Shop::Controller::Basket;

use strict; use warnings;
use base 'Catalyst::Base';
use  Shop::Shop::Model::Orders;
use  Util;
use  Conf;
use CGI::Untaint;
use URI::Escape;
use Date::Format;
use Log::Log4perl;
my $LOGGER = Log::Log4perl->get_logger( __PACKAGE__);
use Data::Dumper;


my %billing_map = (     first_name => "printable",
                        last_name => "printable",
                        card_num => "printable",
                        card_exp_year => "integer",
                  );

my %shipping_map = (    ship_first_name => "printable",
                        ship_last_name => "printable",
                        ship_company => "printable",
                        ship_address2 => "printable",
                  );


sub view : Exported {

        my ( $self, $c ) = @_;

        my $session = $c->{session};

        my @basket_items = $session->basket_items;

        $c->stash->{template} = 'template/basket/view.tt';

        $c->stash->{title} = "View/Modify Cart";

        $c->stash->{edit_cart} = 1;

        if(@basket_items) {

                $c->stash->{basket_items} = [@basket_items];

                my ($total, $shipping_cost, $overweight_surcharge,
$discount_percent, $discount) = get_totals($session->user_id,
@basket_items);

                $c->stash->{total} = $total;

                $c->stash->{shipping} = $shipping_cost;

                $c->stash->{overweight_surcharge} = $overweight_surcharge;

                $c->stash->{discount_percent} = $discount_percent;

                $c->stash->{discount} = $discount;
        }
}

# It takes user_id and an array of basket items and returns (Total cost of
products, shipping cost,
# non-ups surcharge, preset disount for this user, total discount for this
user )
sub get_totals {

        my ($user_id, @bi) = @_;
         # total is is always conforms to %.2f format
        my $total = 0;

        foreach my $basket_item (@bi) {
                $total += $basket_item->product->price *
$basket_item->count;
        }

        $total = sprintf("%.2f", $total);

        my ($discount_percent, $discount_throshold, $discount) = (0, 0, 0);

        # get the dicount throsholds if the user logged in
        ($discount_percent, $discount_throshold) =
Shop::Model::Users->get_discount_thresholds($user_id) if $user_id;

        my ($shipping_cost, $overweight_surcharge) =
Shop::Model::Users->get_shipping($user_id, $total, @bi);

        $shipping_cost = sprintf("%.2f", $shipping_cost);

        $overweight_surcharge = sprintf("%.2f", $overweight_surcharge);

        if ($total >= $discount_throshold) {
                $discount = $total * $discount_percent;

                $discount = sprintf("%.2f", $discount);
        }

        return ($total, $shipping_cost, $overweight_surcharge,
$discount_percent, $discount);
}


sub delete : Exported {

        my ($self, $c ) = @_;

        my $session = $c->{session};

        my @matched_items = $session->basket_items($c->{args}->[0]);

        @matched_items and $matched_items[0]->delete();
        $self->view($c);
}


*Thank You........Once again*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/catalyst/attachments/20060209/220b93ce/attachment.htm


More information about the Catalyst mailing list