-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from karlomikus/develop
Exports, filters, fixes
- Loading branch information
Showing
22 changed files
with
740 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kami\Cocktail\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Kami\Cocktail\Models\Bar; | ||
use Kami\Cocktail\Models\Export; | ||
use Kami\Cocktail\Jobs\StartRecipesExport; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
use Kami\Cocktail\Http\Resources\ExportResource; | ||
use Symfony\Component\HttpFoundation\BinaryFileResponse; | ||
|
||
class ExportController extends Controller | ||
{ | ||
public function index(Request $request): JsonResource | ||
{ | ||
$exports = Export::orderBy('created_at', 'desc') | ||
->where('created_user_id', $request->user()->id) | ||
->get(); | ||
|
||
return ExportResource::collection($exports); | ||
} | ||
|
||
public function store(Request $request): ExportResource | ||
{ | ||
$type = $request->post('type', 'json'); | ||
$bar = Bar::findOrFail($request->post('bar_id')); | ||
|
||
if ($request->user()->cannot('createExport', $bar)) { | ||
abort(403); | ||
} | ||
|
||
$export = new Export(); | ||
$export->withFilename(); | ||
$export->bar_id = $bar->id; | ||
$export->is_done = false; | ||
$export->created_user_id = $request->user()->id; | ||
$export->save(); | ||
|
||
StartRecipesExport::dispatch($bar->id, $type, $export); | ||
|
||
return new ExportResource($export); | ||
} | ||
|
||
public function delete(Request $request, int $id): Response | ||
{ | ||
$export = Export::findOrFail($id); | ||
|
||
if ($request->user()->cannot('delete', $export)) { | ||
abort(403); | ||
} | ||
|
||
$export->delete(); | ||
|
||
return response(null, 204); | ||
} | ||
|
||
public function download(Request $request, int $id): BinaryFileResponse | ||
{ | ||
$export = Export::findOrFail($id); | ||
|
||
if ($request->user()->cannot('download', $export)) { | ||
abort(403); | ||
} | ||
|
||
return response()->download($export->getFullPath()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kami\Cocktail\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
/** | ||
* @mixin \Kami\Cocktail\Models\Export | ||
*/ | ||
class ExportResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'filename' => $this->filename, | ||
'created_at' => $this->created_at, | ||
'bar_name' => $this->bar->name, | ||
'is_done' => (bool) $this->is_done, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kami\Cocktail\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Kami\Cocktail\Models\Cocktail; | ||
use Illuminate\Support\Facades\Log; | ||
use Kami\Cocktail\Models\Ingredient; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\Artisan; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Kami\Cocktail\Search\SearchActionsAdapter; | ||
|
||
class RefreshSearchIndex implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct(private readonly bool $shouldClearIndexes = false) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(SearchActionsAdapter $searchActions): void | ||
{ | ||
$searchActions = $searchActions->getActions(); | ||
|
||
// Clear indexes | ||
if ($this->shouldClearIndexes) { | ||
Log::info('Clearing search indexes'); | ||
Artisan::call('scout:flush', ['model' => Cocktail::class]); | ||
Artisan::call('scout:flush', ['model' => Ingredient::class]); | ||
} | ||
|
||
// Update settings | ||
Log::info('Updating search settings'); | ||
$searchActions->updateIndexSettings(); | ||
|
||
Log::info('Building search indexes'); | ||
|
||
Artisan::call('scout:import', ['model' => Cocktail::class]); | ||
Artisan::call('scout:import', ['model' => Ingredient::class]); | ||
|
||
Log::info('Search indexes updated'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Kami\Cocktail\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Kami\Cocktail\Models\Export; | ||
use Kami\Cocktail\ExportTypeEnum; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Kami\Cocktail\External\Export\Recipes; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\Attributes\WithoutRelations; | ||
|
||
class StartRecipesExport implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public function __construct( | ||
private readonly int $barId, | ||
private readonly string $type, | ||
#[WithoutRelations] | ||
private readonly Export $export, | ||
) { | ||
} | ||
|
||
public function handle(Recipes $exporter): void | ||
{ | ||
$type = ExportTypeEnum::tryFrom($this->type) ?? ExportTypeEnum::JSON; | ||
|
||
$exporter->process($this->barId, $this->export->getFullPath(), $type); | ||
|
||
$this->export->markAsDone(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.