Added nicer explosions via try::tiny
This commit is contained in:
parent
d1cd30928e
commit
afc635fdb4
6 changed files with 27 additions and 33 deletions
|
@ -1,7 +1,7 @@
|
|||
package Pear::LocalLoop::Controller::Admin::ImportFrom;
|
||||
use Mojo::Base 'Mojolicious::Controller';
|
||||
use Moo;
|
||||
use Devel::Dwarn;
|
||||
use Try::Tiny;
|
||||
|
||||
use Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
||||
use Pear::LocalLoop::Import::LCCCsv::Transactions;
|
||||
|
@ -47,19 +47,22 @@ sub post_transactions {
|
|||
$c->flash( error => "CSV file size is too large" );
|
||||
return $c->redirect_to( '/admin/import_from' );
|
||||
}
|
||||
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Transactions->new(
|
||||
csv_string => $c->param('transactions_csv')->slurp,
|
||||
schema => $c->app->schema
|
||||
)->import_csv;
|
||||
|
||||
if ($csv_import->csv_error) {
|
||||
$c->flash( error => $csv_import->csv_error );
|
||||
my $csv_error;
|
||||
try {
|
||||
Pear::LocalLoop::Import::LCCCsv::Transactions->new(
|
||||
csv_string => $c->param('transactions_csv')->slurp,
|
||||
schema => $c->app->schema
|
||||
)->import_csv;
|
||||
} catch {
|
||||
$csv_error = $_;
|
||||
};
|
||||
if ( $csv_error ) {
|
||||
$c->flash( error => $csv_error );
|
||||
return $c->redirect_to( '/admin/import_from' );
|
||||
} else {
|
||||
$c->flash( success => "CSV imported" );
|
||||
return $c->redirect_to( '/admin/import_from' );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
||||
use Moo;
|
||||
use Devel::Dwarn;
|
||||
|
||||
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
||||
|
||||
|
@ -15,7 +14,6 @@ sub import_csv {
|
|||
my ($self) = @_;
|
||||
|
||||
my $rows = $self->csv_data;
|
||||
return $self unless $rows;
|
||||
foreach my $row ( @{$rows} ) {
|
||||
$self->_row_to_result($row);
|
||||
}
|
||||
|
|
|
@ -6,19 +6,19 @@ use DateTime::Format::Strptime;
|
|||
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
||||
|
||||
has '+csv_required_columns' => (
|
||||
builder => sub { return [ qw/
|
||||
transaction_id
|
||||
supplier_id
|
||||
net_amount
|
||||
gross_amount
|
||||
/ ]},
|
||||
builder => sub { return [ (
|
||||
'transaction_id',
|
||||
'supplier_id',
|
||||
'net_amount',
|
||||
'vat amount',
|
||||
'gross_amount',
|
||||
)]},
|
||||
);
|
||||
|
||||
sub import_csv {
|
||||
my ($self) = @_;
|
||||
|
||||
my $rows = $self->csv_data;
|
||||
return 0 unless $rows;
|
||||
my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" });
|
||||
foreach my $row ( @{$rows} ) {
|
||||
$self->_row_to_result($row, $lcc_org);
|
||||
|
@ -29,10 +29,6 @@ sub import_csv {
|
|||
sub _row_to_result {
|
||||
my ( $self, $row, $lcc_org ) = @_;
|
||||
|
||||
use Devel::Dwarn;
|
||||
|
||||
Dwarn $row;
|
||||
|
||||
my $supplier_id = $row->{supplier_id};
|
||||
|
||||
my $organisation = $self->schema->resultset('Organisation')->find({
|
||||
|
@ -49,7 +45,7 @@ sub _row_to_result {
|
|||
|
||||
my $paid_date = ( $row->{paid_date} ? $date_formatter->parse_datetime($row->{paid_date}) : DateTime->today );
|
||||
|
||||
# TODO negative values
|
||||
# TODO negative values are sometimes present
|
||||
$self->external_result->find_or_create_related('transactions', {
|
||||
external_id => $row->{transaction_id},
|
||||
transaction => {
|
||||
|
|
|
@ -70,17 +70,8 @@ has csv_data => (
|
|||
sub check_headers {
|
||||
my $self = shift;
|
||||
my $req_headers = $self->csv_required_columns;
|
||||
use Devel::Dwarn;
|
||||
Dwarn $req_headers;
|
||||
# TODO catch the boom
|
||||
my @headers;
|
||||
try {
|
||||
@headers = $self->_text_csv->header( $self->_csv_filehandle );
|
||||
} catch {
|
||||
$self->csv_error = $_->[1];
|
||||
};
|
||||
return 0 unless @headers;
|
||||
Dwarn \@headers;
|
||||
@headers = $self->_text_csv->header( $self->_csv_filehandle );
|
||||
my %header_map = ( map { $_ => 1 } @headers );
|
||||
for my $req_header ( @$req_headers ) {
|
||||
next if $header_map{$req_header};
|
||||
|
|
Reference in a new issue