取り込み件数を出力

This commit is contained in:
shibafu 2020-05-22 22:11:49 +09:00
parent 3a2d0e67aa
commit 531067fb9c
2 changed files with 8 additions and 4 deletions

View File

@ -96,9 +96,9 @@ class SettingController extends Controller
set_time_limit(0); set_time_limit(0);
$importer = new CheckinCsvImporter(Auth::user(), $file->path()); $importer = new CheckinCsvImporter(Auth::user(), $file->path());
$importer->execute(); $imported = $importer->execute();
return redirect()->route('setting.import')->with('status', 'インポートに性交しました。'); return redirect()->route('setting.import')->with('status', "{$imported}件のインポートに性交しました。");
} catch (CsvImportException $e) { } catch (CsvImportException $e) {
return redirect()->route('setting.import')->with('import_errors', $e->getErrors()); return redirect()->route('setting.import')->with('import_errors', $e->getErrors());
} }

View File

@ -27,7 +27,7 @@ class CheckinCsvImporter
$this->filename = $filename; $this->filename = $filename;
} }
public function execute() public function execute(): int
{ {
// Guess charset // Guess charset
$charset = $this->guessCharset($this->filename); $charset = $this->guessCharset($this->filename);
@ -40,7 +40,7 @@ class CheckinCsvImporter
} }
// Import // Import
DB::transaction(function () use ($csv) { return DB::transaction(function () use ($csv) {
$errors = []; $errors = [];
if (!in_array('日時', $csv->getHeader(), true)) { if (!in_array('日時', $csv->getHeader(), true)) {
@ -51,6 +51,7 @@ class CheckinCsvImporter
throw new CsvImportException(...$errors); throw new CsvImportException(...$errors);
} }
$imported = 0;
foreach ($csv->getRecords() as $offset => $record) { foreach ($csv->getRecords() as $offset => $record) {
$line = $offset + 1; $line = $offset + 1;
$ejaculation = new Ejaculation(['user_id' => $this->user->id]); $ejaculation = new Ejaculation(['user_id' => $this->user->id]);
@ -87,6 +88,7 @@ class CheckinCsvImporter
$ejaculation->tags()->sync(collect($tags)->pluck('id')); $ejaculation->tags()->sync(collect($tags)->pluck('id'));
} }
DB::commit(); DB::commit();
$imported++;
} catch (QueryException $e) { } catch (QueryException $e) {
DB::rollBack(); DB::rollBack();
if ($e->errorInfo[0] === '23505') { if ($e->errorInfo[0] === '23505') {
@ -103,6 +105,8 @@ class CheckinCsvImporter
if (!empty($errors)) { if (!empty($errors)) {
throw new CsvImportException(...$errors); throw new CsvImportException(...$errors);
} }
return $imported;
}); });
} }