From e562a45b10ad4b7336dfd07606331c3e4520f068 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 27 May 2008 04:01:41 +0000 Subject: [PATCH] Fix two bugs with importing product data. Begin to build code for adding and editing products. --- lib/WebGUI/Shop/Products.pm | 22 +++++++++++++++++++--- t/Shop/Products.t | 3 +++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/WebGUI/Shop/Products.pm b/lib/WebGUI/Shop/Products.pm index 99aa045aa..b542feb00 100644 --- a/lib/WebGUI/Shop/Products.pm +++ b/lib/WebGUI/Shop/Products.pm @@ -96,7 +96,7 @@ sub importProducts { my @headers = WebGUI::Text::splitCSV($headers); WebGUI::Error::InvalidFile->throw(error => qq{Bad header found in the CSV file}, brokenFile => $filePath) unless (join(q{-}, sort @headers) eq 'mastersku-price-quantity-shortdescription-sku-title-weight') - and (scalar @headers == 5); + and (scalar @headers == 7); my @productData = (); my $line = 1; while (my $productRow = <$table>) { @@ -104,13 +104,29 @@ sub importProducts { $productRow =~ s/\s*#.+$//; next unless $productRow; local $_; - my @productRow = map { tr/|/,/; $_; } WebGUI::Text::splitCSV($productRow); + my @productRow = WebGUI::Text::splitCSV($productRow); WebGUI::Error::InvalidFile->throw(error => qq{Error found in the CSV file}, brokenFile => $filePath, brokenLine => $line) - unless scalar @productRow == 5; + unless scalar @productRow == 7; push @productData, [ @productRow ]; } ##Okay, if we got this far, then the data looks fine. return unless scalar @productData; + my $fetchProductId = $session->db->prepare('select assetId from Product where mastersku=? order by revisionDate DESC limit 1'); + foreach my $productRow (@productData) { + my %productRow; + ##Order the data according to the headers, in whatever order they exist. + @productRow { @headers } = @{ $productRow }; + $fetchProductId->execute([$productRow->{mastersku}]); + my ($assetId) = $fetchProductId->hashRef->{assetId}; + ##If the assetId exists, we update data for it + if ($assetId) { + my $product = WebGUI::Asset->newPending($session, $assetId); + ##Error handling for locked assets + } + else { + ##Insert a new product; + } + } return 1; } diff --git a/t/Shop/Products.t b/t/Shop/Products.t index eb5837b73..0fda98a88 100644 --- a/t/Shop/Products.t +++ b/t/Shop/Products.t @@ -24,6 +24,7 @@ use Data::Dumper; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; use WebGUI::Text; +use WebGUI::Asset::Sku::Product; #---------------------------------------------------------------------------- # Init @@ -53,6 +54,8 @@ SKIP: { # ####################################################################### + my $importNode = WebGUI::Asset::Sku::Product->getProductImportNode($session); + eval { WebGUI::Shop::Products::importProducts($session); }; $e = Exception::Class->caught(); isa_ok($e, 'WebGUI::Error::InvalidParam', 'importProducts: error handling for an undefined path to file');