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

use lib '/app/tools/bookpub/lib';
use BookPub::Market::WatchList;
use BookPub::Market::Command::Main;

use base 'BookPub::Command';

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Log;
use RSApache::Response::XSLT;

sub cmd  { return 'delete'; }
sub area { return 'market'; }

sub pageName { return 'Delete Watch List'; }

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

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

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

    my $id = $self->getParam('WatchListID');
    my $list;

    if ($id) {
        $list = BookPub::Market::WatchList->new( watchListID => $id );
    } else {
        my $command = BookPub::Market::Command::Main->new();
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    }

    if ( $list->delete() ) {

        # Redirect to the Main command.
        #
        my $successMessage = BookPub::Message->new( type => "success", code => Common::FormObject::kSuccessDelete );
        my $command = BookPub::Market::Command::Main->new( msg => $successMessage->toString() );
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    } else {
        my $errorMsg = BookPub::Message->new( type => "error", code => Common::FormObject::kErrDelete );
        my %editParams = (
            WatchListID => $id,
            msg         => $errorMsg->toString(),
        );
        my $command = BookPub::Market::Command::Edit->new(%editParams);
        return RSApache::Response::Redirect->new( $command->getRedirect() );
    }
}

1;
