very close to working

This commit is contained in:
JT Smith 2008-02-26 18:37:53 +00:00
parent 449c3c26e0
commit e3be583b20
3 changed files with 63 additions and 18 deletions

View file

@ -82,6 +82,8 @@ sub delete {
my ($self) = @_; my ($self) = @_;
$self->empty; $self->empty;
$self->session->db->write("delete from cart where cartId=?",[$self->getId]); $self->session->db->write("delete from cart where cartId=?",[$self->getId]);
undef $self;
return undef;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -144,9 +146,9 @@ Returns an array reference of WebGUI::Asset::Sku objects that are in the cart.
sub getItems { sub getItems {
my ($self) = @_; my ($self) = @_;
my @itemsObjects = (); my @itemsObjects = ();
my $items = $self->session->db->read("select assetId from cartItems where cartId=?",[$self->getId]); my $items = $self->session->db->read("select itemId from cartItems where cartId=?",[$self->getId]);
while (my ($assetId) = $items->array) { while (my ($itemId) = $items->array) {
push(@itemsObjects, WebGUI::Shop::CartItems->new($self->session, $assetId)); push(@itemsObjects, WebGUI::Shop::CartItem->new($self, $itemId));
} }
return \@itemsObjects; return \@itemsObjects;
} }

View file

@ -1,7 +1,6 @@
package WebGUI::Shop::CartItem; package WebGUI::Shop::CartItem;
use strict; use strict;
use Class::InsideOut qw{ :std }; use Class::InsideOut qw{ :std };
use Carp qw(croak); use Carp qw(croak);
use JSON; use JSON;
@ -81,7 +80,13 @@ sub get {
my ($self, $name) = @_; my ($self, $name) = @_;
if (defined $name) { if (defined $name) {
if ($name eq "options") { if ($name eq "options") {
return JSON::from_json($properties{id $self}{$name}); my $options = $properties{id $self}{$name};
if ($options eq "") {
return {};
}
else {
return JSON::from_json($properties{id $self}{$name});
}
} }
return $properties{id $self}{$name}; return $properties{id $self}{$name};
} }
@ -89,6 +94,20 @@ sub get {
return \%copyOfHashRef; return \%copyOfHashRef;
} }
#-------------------------------------------------------------------
=head2 getId ()
Returns the unique id of this item.
=cut
sub getId {
my $self = shift;
return $self->get("itemId");
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 getSku ( ) =head2 getSku ( )
@ -99,7 +118,7 @@ Returns an instanciated WebGUI::Asset::Sku object for this cart item.
sub getSku { sub getSku {
my ($self) = @_; my ($self) = @_;
my $asset = WebGUI::Asset->newByDynamicClass($self->session, $self->get("assetId")); my $asset = WebGUI::Asset->newByDynamicClass($self->cart->session, $self->get("assetId"));
$asset->applyOptions($self->get("options")); $asset->applyOptions($self->get("options"));
return $asset; return $asset;
} }
@ -109,11 +128,11 @@ sub getSku {
=head2 incrementQuantity ( [ quantity ] ) =head2 incrementQuantity ( [ quantity ] )
Increments quantity of item by one. Increments quantity of item by one. Returns the quantity of this item in the cart.
=head3 quantity =head3 quantity
If specified may increment quantity by more than one. If specified may increment quantity by more than one. Specify a negative number to decrement quantity. If the quantity ever reaches 0 or lower, the item will be removed from the cart.
=cut =cut
@ -124,8 +143,12 @@ sub incrementQuantity {
if ($self->get("quantity") + $quantity > $self->getSku->getMaxAllowedInCart) { if ($self->get("quantity") + $quantity > $self->getSku->getMaxAllowedInCart) {
croak "Cannot have that many in cart."; croak "Cannot have that many in cart.";
} }
if ($self->get("quantity") + $quantity <= 0) {
return $self->remove;
}
$properties{$id}{quantity} += $quantity; $properties{$id}{quantity} += $quantity;
$self->session->db->setRow("cartItems","itemId", $properties{$id}); $self->cart->session->db->setRow("cartItems","itemId", $properties{$id});
return $properties{$id}{quantity};
} }
@ -149,7 +172,7 @@ sub new {
my ($class, $cart, $itemId) = @_; my ($class, $cart, $itemId) = @_;
croak "Need a cart" unless (defined $cart && $cart->isa("WebGUI::Shop::Cart")); croak "Need a cart" unless (defined $cart && $cart->isa("WebGUI::Shop::Cart"));
croak "Need an itemId" unless defined $itemId; croak "Need an itemId" unless defined $itemId;
my $item = $cart->session->db->quickHashRef('select * from cart where itemId=?', [$itemId]); my $item = $cart->session->db->quickHashRef('select * from cartItems where itemId=?', [$itemId]);
croak "No item with id of $itemId" if ($item->{itemId} eq ""); croak "No item with id of $itemId" if ($item->{itemId} eq "");
croak "Item $itemId is not in this cart." if ($item->{cartId} ne $cart->getId); croak "Item $itemId is not in this cart." if ($item->{cartId} ne $cart->getId);
my $self = register $class; my $self = register $class;
@ -159,6 +182,22 @@ sub new {
return $self; return $self;
} }
#-------------------------------------------------------------------
=head2 remove ( )
Removes this item from the cart.
=cut
sub remove {
my $self = shift;
$self->cart->session->db->deleteRow("cartItems","itemId",$self->getId);
undef $self;
return undef;
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 update ( properties ) =head2 update ( properties )
@ -199,7 +238,7 @@ sub update {
$properties{$id}{options} = JSON::to_json($newProperties->{options}); $properties{$id}{options} = JSON::to_json($newProperties->{options});
} }
$properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $properties{$id}{shippingAddressId}; $properties{$id}{shippingAddressId} = $newProperties->{shippingAddressId} || $properties{$id}{shippingAddressId};
$self->session->db->setRow("cart","cartId",$properties{$id}); $self->cart->session->db->setRow("cartItems","cartId",$properties{$id});
} }

View file

@ -30,7 +30,7 @@ my $session = WebGUI::Test->session;
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# Tests # Tests
plan tests => 14; # Increment this number for each test you create plan tests => 16; # Increment this number for each test you create
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
# put your tests here # put your tests here
@ -48,26 +48,30 @@ my $product = $root->addChild({
my $item = $cart->addItem($product); my $item = $cart->addItem($product);
isa_ok($item, "WebGUI::Shop::CartItem"); isa_ok($item, "WebGUI::Shop::CartItem");
isa_ok($item->cart, "WebGUI::Cart", "Does the item have a cart?"); isa_ok($item->cart, "WebGUI::Shop::Cart", "Does the item have a cart?");
is(ref($item->get), "HASH", "Do we have a hash of properties?"); is(ref($item->get), "HASH", "Do we have a hash of properties?");
is($item->get("quantity"), 2, "Should have 1 of these in the cart."); is($item->get("quantity"), 1, "Should have 1 of these in the cart.");
$item->incrementQuantity(2); is($item->incrementQuantity(2), 3, "incrementQuantity() should tell us how many items of this type are in the cart");
is($item->get("quantity"), 3, "Should have 3 of these in the cart."); is($item->get("quantity"), 3, "Should have 3 of these in the cart.");
is(scalar(@{$cart->getItems}), 1, "Should have 3 of these in the cart."); is(scalar(@{$cart->getItems}), 1, "Should have 1 item type in cart regardless of quanity.");
$item->update({shippingAddressId => "XXXX"});
is($item->get("shippingAddressId"), "XXXX", "Can set values to the cart item properties.");
like($cart->getId, qr/[A-Za-z0-9\_\-]{22}/, "Id looks like a guid."); like($cart->getId, qr/[A-Za-z0-9\_\-]{22}/, "Id looks like a guid.");
is(ref($cart->get), "HASH", "Cart properties are a hash reference."); is(ref($cart->get), "HASH", "Cart properties are a hash reference.");
is($cart->get("sessionId"), $session->getId, "Can retrieve a value from the cart properties."); is($cart->get("sessionId"), $session->getId, "Can retrieve a value from the cart properties.");
$cart->set({shippingAddressId => "XXXX"});
$cart->update({shippingAddressId => "XXXX"});
is($cart->get("shippingAddressId"), "XXXX", "Can set values to the cart properties."); is($cart->get("shippingAddressId"), "XXXX", "Can set values to the cart properties.");
$cart->empty; $cart->empty;
is($session->db->quickScalar("select count(*) from cartItems where cartId=?",[$cart->getId]), 0, "Items are removed from cart."); is($session->db->quickScalar("select count(*) from cartItems where cartId=?",[$cart->getId]), 0, "Items are removed from cart.");
$cart->delete; $cart->delete;
is($cart, undef, "Can destroy cart."); is($cart->delete, undef, "Can destroy cart.");
$product->purge; $product->purge;