market/products: reuse existing entry

This commit is contained in:
Juerd Waalboer 2019-12-12 23:16:10 +01:00
parent c3b0b86c97
commit 29b3eea131
2 changed files with 31 additions and 5 deletions

View file

@ -37,10 +37,26 @@ sub command :Tab(market,&tab) {
my $space = parse_amount($product->{ space }) or return NEXT; my $space = parse_amount($product->{ space }) or return NEXT;
my $description = $product->{description}; my $description = $product->{description};
$cart my @existing = grep {
->add(-($seller + $space), "$description (sold by $username)", {product_id=>$command}) $_->attribute('plugin') eq $self->id and
->add_contra($username, 0+$seller, "\$you bought $description") $_->attribute('product_id') eq $command
if 0+$seller; } $cart->entries('plugin');
if (@existing) {
$existing[0]->quantity($existing[0]->quantity + 1);
return ACCEPT;
}
$cart->add(
-($seller + $space),
"$description (sold by $username)",
{ product_id => $command, plugin => $self->id }
)->add_contra(
$username,
$seller,
"\$you bought $description"
);
return ACCEPT; return ACCEPT;
} }

View file

@ -34,10 +34,20 @@ sub command :Tab(edit,&tab) {
my $price = parse_amount( $product->{price} ) or return NEXT; my $price = parse_amount( $product->{price} ) or return NEXT;
my @existing = grep {
$_->attribute('plugin') eq $self->id and
$_->attribute('product_id') eq $product->{id}
} $cart->entries('plugin');
if (@existing) {
$existing[0]->quantity($existing[0]->quantity + 1);
return ACCEPT;
}
$cart->add( $cart->add(
-$price, -$price,
$product->{description}, $product->{description},
{ product_id => $product->{id} } { product_id => $product->{id}, plugin => $self->id }
); );
return ACCEPT; return ACCEPT;
} }