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/Controller/Admin/ImportFrom.pm

70 lines
1.8 KiB
Perl
Raw Permalink Normal View History

2019-07-02 14:21:01 +00:00
package Pear::LocalLoop::Controller::Admin::ImportFrom;
use Mojo::Base 'Mojolicious::Controller';
2019-07-05 14:30:31 +00:00
use Moo;
2019-07-05 17:44:46 +00:00
use Try::Tiny;
use Mojo::File qw/ path /;
2019-07-05 14:30:31 +00:00
2019-07-04 13:16:49 +00:00
sub index {
my $c = shift;
2019-07-05 14:30:31 +00:00
$c->app->max_request_size(104857600);
2019-07-04 13:16:49 +00:00
}
sub post_suppliers {
my $c = shift;
2019-07-05 14:30:31 +00:00
unless ($c->param('suppliers_csv')) {
$c->flash( error => "No CSV file given" );
return $c->redirect_to( '/admin/import_from' );
}
# Check file size
if ($c->req->is_limit_exceeded) {
$c->flash( error => "CSV file size is too large" );
return $c->redirect_to( '/admin/import_from' );
}
2019-07-08 15:41:59 +00:00
my $file = $c->param('suppliers_csv');
2019-07-08 15:41:59 +00:00
my $filename = path($c->app->config->{upload_path}, time.'suppliers.csv' );
2019-07-08 15:41:59 +00:00
$file->move_to($filename);
2019-07-08 15:41:59 +00:00
my $job_id = $c->minion->enqueue('csv_supplier_import' => [$filename] );
2019-07-08 15:41:59 +00:00
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
2019-07-05 14:30:31 +00:00
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
2019-07-04 13:16:49 +00:00
return $c->redirect_to( '/admin/import_from' );
}
sub post_transactions {
my $c = shift;
2019-07-05 14:30:31 +00:00
unless ($c->param('transactions_csv')) {
$c->flash( error => "No CSV file given" );
return $c->redirect_to( '/admin/import_from' );
}
# Check file size
if ($c->req->is_limit_exceeded) {
$c->flash( error => "CSV file size is too large" );
return $c->redirect_to( '/admin/import_from' );
}
my $file = $c->param('transactions_csv');
my $filename = path($c->app->config->{upload_path}, time.'transactions.csv' );
$file->move_to($filename);
my $job_id = $c->minion->enqueue('csv_transaction_import' => [$filename] );
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
return $c->redirect_to( '/admin/import_from' );
2019-07-04 13:16:49 +00:00
}
1;