Skip to content

Commit

Permalink
Add git data importer
Browse files Browse the repository at this point in the history
  • Loading branch information
karlomikus committed Nov 12, 2023
1 parent f95d3e2 commit 44a89c7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ yarn-error.log
notes.sql
composer.phar
.phpunit.cache
/resources/data
25 changes: 16 additions & 9 deletions app/Import/FromLocalData2.php → app/Import/FromGitData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions app/Jobs/SetupBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 44a89c7

Please sign in to comment.