#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Market::Command::AjaxDeleteItem;
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::WatchListItem;

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

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

sub cmd      { return 'delete_item'; }
sub area     { return 'market'; }
sub pageName { return 'Delete from List'; }

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

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

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

    my $watchListItemID = $self->getParam("WatchListItemID");
    assert($watchListItemID);

    my $undo = $self->getParam("Undo");

    my $item = BookPub::DB::Item::WatchListItem->Lookup( watch_list_item_id => $watchListItemID );
    if ($item) {
        if ($undo) {
            $item->deleted(0);
        } else {
            $item->deleted(1);
        }
        $item->save();
        return RSApache::Response::JSON->new( { status => 'success' } );
    }

    return RSApache::Response::JSON->new('failed to update item');
}

1;
