Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillip Taylor committed May 27, 2011
1 parent ea1f202 commit 98ea5c1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 42 deletions.
48 changes: 10 additions & 38 deletions lib/Wardrobe/Controller/Root.pm
Expand Up @@ -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
);
}

Expand All @@ -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
);

}
Expand Down
57 changes: 53 additions & 4 deletions 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({
Expand All @@ -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({
Expand All @@ -26,7 +74,8 @@ sub create_clothing_and_category {
name => $clothing_name,
category => $category
});


return 1;
}

1;
24 changes: 24 additions & 0 deletions root/index.tt
Expand Up @@ -7,4 +7,28 @@
<input type="file" name="csv_file">
<input type="submit" value="Import">
</form>

[% IF upload_complete %]
<p>
Upload Complete. Statistics:
<table>
<tr>
<th></th>
<th></th>
</tr>
<tr>
<td>Total Rows</td>
<td>[% rows %]</td>
</tr>
<tr>
<td>Bad Record</td>
<td>[% bad %]</td>
</tr>
<tr>
<td>Dupes</td>
<td>[% dupes %]</td>
</tr>
</p>
[% END %]
</p>

0 comments on commit 98ea5c1

Please sign in to comment.