From 98ea5c1b568d6ccab0e18e6f237a21f9856ae35d Mon Sep 17 00:00:00 2001 From: Phillip Taylor Date: Fri, 27 May 2011 11:23:04 +0100 Subject: [PATCH] Clean up --- lib/Wardrobe/Controller/Root.pm | 48 ++++++--------------------- lib/Wardrobe/Model/Main.pm | 57 ++++++++++++++++++++++++++++++--- root/index.tt | 24 ++++++++++++++ 3 files changed, 87 insertions(+), 42 deletions(-) diff --git a/lib/Wardrobe/Controller/Root.pm b/lib/Wardrobe/Controller/Root.pm index 6a98d76..7b2f037 100644 --- a/lib/Wardrobe/Controller/Root.pm +++ b/lib/Wardrobe/Controller/Root.pm @@ -35,8 +35,8 @@ sub index :Path :Args(0) { # Hello World $c->stash( - template => 'index.tt', -# upload_count => $upload_count + template => 'index.tt', + upload_complete => 0 ); } @@ -58,45 +58,17 @@ sub csv_upload :Local { my @results = (); my $upload = $c->req->upload('csv_file'); - my $parser = Text::CSV::Encoded->new({ - encoding_in => "utf8", - escape_char => '"', - sep_char => ',', - allow_whitespace => 1 - }); + $c->log->debug("upload: $upload->tempname"); - $c->log->debug("filename is: " . $upload->filename); - - my $fh = $upload->fh; - my $line_no = 0; - - foreach my $line (<$fh>) { - - $c->log->debug("LINE: $line"); - if (!$parser->parse($line)) { - $c->log->warn("Skipped line $line_no - broken"); - push (@results, [ $line_no, $line, "failed to parse" ]); - } else { - - if ($line_no == 0) { - $line_no++; - next; - } else { - (my $clothing_name, my $category_name) = $parser->fields(); - $c->log->debug("Parsed: Clothing Name: $clothing_name| Category Name: $category_name"); - - Wardrobe::Model::Main->create_clothing_and_category($clothing_name, $category_name); - - } - } - - $line_no++; - } - - close $fh; + # assume header record + (my $rows, my $bad, my $dupes) = Wardrobe::Model::Main->create_from_csv_file($upload->tempname, 1); $c->stash( - template => 'index.tt' + template => 'index.tt', + upload_complete => 1, + rows => $rows, + bad => $bad, + dupes => $dupes ); } diff --git a/lib/Wardrobe/Model/Main.pm b/lib/Wardrobe/Model/Main.pm index 908c2d0..5c24c71 100644 --- a/lib/Wardrobe/Model/Main.pm +++ b/lib/Wardrobe/Model/Main.pm @@ -1,11 +1,59 @@ package Wardrobe::Model::Main; use base qw/DBIx::Class::Schema/; +use Text::CSV::Encoded; __PACKAGE__->load_namespaces; -# TODO: Only creates it if it doesn't already exist. +# creat_from_csv_file (str filename, bool header_record); +sub create_from_csv_file { + my ($self, $csv_filename, $header_record) = @_; + + my $parser = Text::CSV::Encoded->new({ + encoding_in => "utf8", + encoding_out => "utf8", + escape_char => '"', + sep_char => ',', + allow_whitespace => 1 + }); + + Wardrobe->log->info("loading in new data from $csv_filename"); + + open my $fh, "<", $csv_filename; + + my $line_no = 0; + my $bad = 0; + my $dupes = 0; + + foreach my $line (<$fh>) { + + if (!$parser->parse($line)) { + Wardrobe->log->warn("Skipped line $line_no - broken"); + $bad++; + } else { + + if ($line_no == 0 && $header_record) { + $line_no++; + next; + } else { + (my $clothing_name, my $category_name) = $parser->fields(); + my $added = create_clothing_and_category($clothing_name, $category_name); + + if (!$added) { + $dupes++; + } + } + } + + $line_no++; + } + + close $fh; + + return ($line_no, $bad, $dupes); +} + sub create_clothing_and_category { - my ($self, $clothing_name, $category_name) = @_; + my ($clothing_name, $category_name) = @_; Wardrobe->log->debug("Parsed: Clothing Name: $clothing_name| Category Name: $category_name"); my $clothing_item = Wardrobe->get_schema()->resultset('Clothing')->search({ @@ -14,7 +62,7 @@ sub create_clothing_and_category { if ($clothing_item) { Wardrobe->log->warn('Existing clothing item found. skipped'); - return; + return 0; } my $category = Wardrobe->get_schema()->resultset('Category')->find_or_create({ @@ -26,7 +74,8 @@ sub create_clothing_and_category { name => $clothing_name, category => $category }); - + + return 1; } 1; diff --git a/root/index.tt b/root/index.tt index 19c9b3d..0560e2c 100644 --- a/root/index.tt +++ b/root/index.tt @@ -7,4 +7,28 @@ + + [% IF upload_complete %] +

+ Upload Complete. Statistics: + + + + + + + + + + + + + + + + + +

+ [% END %]

+
Total Rows[% rows %]
Bad Record[% bad %]
Dupes[% dupes %]