44
common/Search/Controllers/NormalizedModelsController.php
Executable file
44
common/Search/Controllers/NormalizedModelsController.php
Executable file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Search\Controllers;
|
||||
|
||||
use Common\Core\BaseController;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class NormalizedModelsController extends BaseController
|
||||
{
|
||||
public function show(string $modelType, int $modelId)
|
||||
{
|
||||
$namespace = modelTypeToNamespace($modelType);
|
||||
$with = request('with') ? explode(',', request('with')) : [];
|
||||
|
||||
$model = app($namespace)->findOrFail($modelId);
|
||||
$model->load($with);
|
||||
|
||||
$this->authorize('show', $model);
|
||||
|
||||
return $this->success(['model' => $model->toNormalizedArray()]);
|
||||
}
|
||||
|
||||
public function index(string $modelType)
|
||||
{
|
||||
$namespace = modelTypeToNamespace($modelType);
|
||||
$query = request('query');
|
||||
$with = request('with') ? explode(',', request('with')) : [];
|
||||
|
||||
$this->authorize('index', $namespace);
|
||||
|
||||
$model = app($namespace);
|
||||
if ($query) {
|
||||
$model = $model->mysqlSearch($query);
|
||||
}
|
||||
|
||||
$results = $model
|
||||
->take(15)
|
||||
->get()
|
||||
->load($with)
|
||||
->map(fn(Model $model) => $model->toNormalizedArray());
|
||||
|
||||
return $this->success(['results' => $results]);
|
||||
}
|
||||
}
|
||||
37
common/Search/Controllers/SearchSettingsController.php
Executable file
37
common/Search/Controllers/SearchSettingsController.php
Executable file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Search\Controllers;
|
||||
|
||||
use Artisan;
|
||||
use Common\Core\BaseController;
|
||||
use Common\Search\ImportRecordsIntoScout;
|
||||
use Str;
|
||||
|
||||
class SearchSettingsController extends BaseController
|
||||
{
|
||||
public function getSearchableModels()
|
||||
{
|
||||
$models = ImportRecordsIntoScout::getSearchableModels();
|
||||
|
||||
$models = array_map(function (string $model) {
|
||||
return [
|
||||
'model' => $model,
|
||||
'name' => Str::plural(last(explode('\\', $model))),
|
||||
];
|
||||
}, $models);
|
||||
|
||||
return $this->success(['models' => $models]);
|
||||
}
|
||||
|
||||
public function import()
|
||||
{
|
||||
$this->middleware('isAdmin');
|
||||
|
||||
(new ImportRecordsIntoScout())->execute(
|
||||
request('model'),
|
||||
request('driver'),
|
||||
);
|
||||
|
||||
return $this->success(['output' => nl2br(Artisan::output())]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user