diff --git a/app/Filament/Resources/EggResource/Pages/ListEggs.php b/app/Filament/Resources/EggResource/Pages/ListEggs.php index 036ec005e..b5db133a4 100644 --- a/app/Filament/Resources/EggResource/Pages/ListEggs.php +++ b/app/Filament/Resources/EggResource/Pages/ListEggs.php @@ -35,17 +35,19 @@ class ListEggs extends ListRecords /** @var EggImporterService $eggImportService */ $eggImportService = resolve(EggImporterService::class); - try { - $eggImportService->handle($eggFile); - } catch (Exception $exception) { - Notification::make() - ->title('Egg Import Failed') - ->danger() - ->send(); + foreach ($eggFile as $file) { + try { + $eggImportService->handle($file); + } catch (Exception $exception) { + Notification::make() + ->title('Egg Import Failed') + ->danger() + ->send(); - report($exception); + report($exception); - return; + return; + } } Notification::make() diff --git a/app/Services/Eggs/Sharing/EggImporterService.php b/app/Services/Eggs/Sharing/EggImporterService.php index 45648870a..0a949474f 100644 --- a/app/Services/Eggs/Sharing/EggImporterService.php +++ b/app/Services/Eggs/Sharing/EggImporterService.php @@ -5,6 +5,7 @@ namespace App\Services\Eggs\Sharing; use Ramsey\Uuid\Uuid; use Illuminate\Support\Arr; use App\Models\Egg; +use Illuminate\Http\UploadedFile; use App\Models\EggVariable; use Illuminate\Database\ConnectionInterface; use App\Services\Eggs\EggParserService; @@ -20,31 +21,25 @@ class EggImporterService * * @throws \App\Exceptions\Service\InvalidFileUploadException|\Throwable */ - public function handle(array $files): array + public function handle(UploadedFile $file): Egg { - $eggs = []; + $parsed = $this->parser->handle($file); - foreach ($files as $file) { - $parsed = $this->parser->handle($file); + return $this->connection->transaction(function () use ($parsed) { + $egg = (new Egg())->forceFill([ + 'uuid' => Uuid::uuid4()->toString(), + 'author' => Arr::get($parsed, 'author'), + 'copy_script_from' => null, + ]); - $egg = $this->connection->transaction(function () use ($parsed) { - $egg = (new Egg())->forceFill([ - 'uuid' => Uuid::uuid4()->toString(), - 'author' => Arr::get($parsed, 'author'), - 'copy_script_from' => null, - ]); + $egg = $this->parser->fillFromParsed($egg, $parsed); + $egg->save(); - $egg = $this->parser->fillFromParsed($egg, $parsed); - $egg->save(); + foreach ($parsed['variables'] ?? [] as $variable) { + EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id])); + } - foreach ($parsed['variables'] ?? [] as $variable) { - EggVariable::query()->forceCreate(array_merge($variable, ['egg_id' => $egg->id])); - } - - return $egg; - }); - } - - return $eggs; + return $egg; + }); } }