#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Market::Command::AjaxCreateList;
use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use lib '/app/tools/common/lib';
use Common::Preference;
use Common::Assert;

use BookPub::DB::Item::WatchList;
use BookPub::Market::WatchList;

use RSApache::Response::XSLT;
use RSApache::Response::JSON;
use RSApache::Command::XMLCommand;

use base 'RSApache::Command::XMLCommand';

sub cmd      { return 'create_list'; }
sub area     { return 'market'; }
sub pageName { return 'Add to List'; }

use constant kTemplate => "/app/tools/bookpub/templates/market/ajax_add_item.xsl";

sub execute {
    my ($self) = @_;

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $watchListName = $self->getParam("WatchListName");
    my $productID     = $self->getParam("ProductID");
    my $bookID        = $self->getParam("BookID");

    my $watchList = BookPub::DB::Item::WatchList->Create();

    $watchList->name($watchListName);
    $watchList->save();

    if ($watchList) {

        # And now let's add the product or book to the list

        my $watchListID = $watchList->watch_list_id;
        return RSApache::Response::Redirect->new(
            "/bookpub/market?c=add_item&amp;ProductID=$productID&amp;BookID=$bookID&amp;WatchListID=$watchListID");
    }

    $self->{xml}{WatchListList} = BookPub::Market::WatchListList->new();

    $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );
    return $response;
}

1;
