From 44a89c7023caa0c8e17cc0d2513aaae97f2544b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karlo=20Miku=C5=A1?= Date: Sun, 12 Nov 2023 11:24:31 +0100 Subject: [PATCH] Add git data importer --- .gitignore | 1 + .../{FromLocalData2.php => FromGitData.php} | 25 ++++++++++++------- app/Jobs/SetupBar.php | 4 +-- 3 files changed, 19 insertions(+), 11 deletions(-) rename app/Import/{FromLocalData2.php => FromGitData.php} (91%) diff --git a/.gitignore b/.gitignore index b00e0d62..f9a4f32a 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ yarn-error.log notes.sql composer.phar .phpunit.cache +/resources/data diff --git a/app/Import/FromLocalData2.php b/app/Import/FromGitData.php similarity index 91% rename from app/Import/FromLocalData2.php rename to app/Import/FromGitData.php index 90569c06..3bb37cbb 100644 --- a/app/Import/FromLocalData2.php +++ b/app/Import/FromGitData.php @@ -16,14 +16,17 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Storage; -class FromLocalData2 +class FromGitData { public function process(Bar $bar, User $user, array $flags = []): bool { - $this->importBaseData('glasses', resource_path('/data/base_glasses.yml'), $bar->id); - $this->importBaseData('cocktail_methods', resource_path('/data/base_methods.yml'), $bar->id); - $this->importBaseData('utensils', resource_path('/data/base_utensils.yml'), $bar->id); - $this->importBaseData('ingredient_categories', resource_path('/data/base_ingredient_categories.yml'), $bar->id); + /** @var \Illuminate\Support\Facades\Storage */ + $dataDisk = Storage::disk('data-files'); + + $this->importBaseData('glasses', $dataDisk->path('base_glasses.yml'), $bar->id); + $this->importBaseData('cocktail_methods', $dataDisk->path('base_methods.yml'), $bar->id); + $this->importBaseData('utensils', $dataDisk->path('base_utensils.yml'), $bar->id); + $this->importBaseData('ingredient_categories', $dataDisk->path('base_ingredient_categories.yml'), $bar->id); if (in_array('ingredients', $flags)) { $this->importIngredients($bar, $user); @@ -65,10 +68,14 @@ private function importIngredients(Bar $bar, User $user): void /** @var \Illuminate\Support\Facades\Storage */ $uploadsDisk = Storage::disk('uploads'); - $ingredients = []; - foreach ($dataDisk->files('ingredients') as $ingredientFile) { - $ingredients[] = Yaml::parseFile($dataDisk->path($ingredientFile)); - } + $ingredients = Cache::remember('ba:data-import:ingredients', 60 * 60 * 24 * 7, function () use ($dataDisk) { + $ingredients = []; + foreach ($dataDisk->files('ingredients') as $ingredientFile) { + $ingredients[] = Yaml::parseFile($dataDisk->path($ingredientFile)); + } + + return $ingredients; + }); $categories = DB::table('ingredient_categories')->select('id', 'name')->where('bar_id', $bar->id)->get(); diff --git a/app/Jobs/SetupBar.php b/app/Jobs/SetupBar.php index 46ce1665..1c7b38a2 100644 --- a/app/Jobs/SetupBar.php +++ b/app/Jobs/SetupBar.php @@ -8,7 +8,7 @@ use Kami\Cocktail\Models\Bar; use Kami\Cocktail\Models\User; use Illuminate\Queue\SerializesModels; -use Kami\Cocktail\Import\FromLocalData2; +use Kami\Cocktail\Import\FromGitData; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -33,7 +33,7 @@ public function __construct( /** * Execute the job. */ - public function handle(FromLocalData2 $import): void + public function handle(FromGitData $import): void { $import->process($this->bar, $this->user, $this->barOptions); }