This repository has been archived on 2023-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
Foodloop-Server/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm

55 lines
1 KiB
Perl
Raw Permalink Normal View History

package Pear::LocalLoop::Import::LCCCsv::Postcodes;
use Moo;
use Geo::UK::Postcode::Regex;
extends qw/Pear::LocalLoop::Import::LCCCsv/;
has '+csv_required_columns' => (
2021-03-20 12:09:50 +00:00
builder => sub {
return [
qw/
postcode
ward
/
];
},
);
sub import_csv {
2021-03-20 12:09:50 +00:00
my ($self) = @_;
2021-03-20 12:09:50 +00:00
$self->check_headers;
2019-09-10 08:32:15 +00:00
2021-03-20 12:09:50 +00:00
while ( my $row = $self->get_csv_line ) {
$self->_row_to_result($row);
}
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
sub _row_to_result {
2021-03-20 12:09:50 +00:00
my ( $self, $row ) = @_;
2021-03-20 12:09:50 +00:00
my $postcode_obj = Geo::UK::Postcode::Regex->parse( $row->{postcode} );
2021-03-20 12:09:50 +00:00
my $ward = $self->schema->resultset('GbWard')
->find_or_create( ward => $row->{ward} );
2021-03-20 12:09:50 +00:00
my $postcode_r = $self->schema->resultset('GbPostcode')->find(
{
outcode => $postcode_obj->{outcode},
incode => $postcode_obj->{incode},
}
);
2021-03-20 12:09:50 +00:00
return unless $postcode_r;
return if $postcode_r->ward;
2019-09-06 16:31:09 +00:00
2021-03-20 12:09:50 +00:00
$postcode_r->update( { ward_id => $ward->id } );
2021-03-20 23:26:52 +00:00
2021-03-20 15:02:00 +00:00
return 1;
}
1;