Skip to content

Commit 0f5b0a5

Browse files
ngaspariStyleCIBot
andauthored
RemoteType - search (#104)
* RemoteType - search - search remote type CF - add is_searchable attribute * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 997b2cf commit 0f5b0a5

File tree

6 files changed

+104
-8
lines changed

6 files changed

+104
-8
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('custom_fields', function (Blueprint $table) {
17+
$table->boolean('is_searchable')->default(true);
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('custom_fields', function (Blueprint $table) {
29+
$table->dropColumn('is_searchable');
30+
});
31+
}
32+
};

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
Route::match(['put', 'patch'], 'remote/{remote_type}', [RemoteCustomFieldController::class, 'update'])->name('remote.update');
4848
Route::get('remote/{remote_type}/resolve', [RemoteCustomFieldController::class, 'resolve'])->name('remote.resolve');
4949
Route::get('remote/{remote_type}/resolve/{identifier_value}', [RemoteCustomFieldController::class, 'resolveByIdentifierValue'])->name('remote.resolveByIdentifierValue');
50+
Route::get('remote/{remote_type}/search/{q?}', [RemoteCustomFieldController::class, 'search'])->name('remote.search');
5051

5152
Route::get('selection', [SelectionCustomFieldController::class, 'index'])->name('selection.index');
5253
Route::post('selection/{plain_type}', [SelectionCustomFieldController::class, 'store'])->name('selection.store');

src/App/Http/Controllers/RemoteCustomFieldController.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Asseco\CustomFields\App\Http\Requests\RemoteTypeRequest;
1212
use Asseco\CustomFields\App\Traits\TransformsOutput;
1313
use Illuminate\Http\JsonResponse;
14+
use Illuminate\Http\Request;
1415
use Illuminate\Support\Arr;
1516
use Illuminate\Support\Facades\DB;
1617

@@ -120,7 +121,7 @@ public function resolve(RemoteType $remoteType): JsonResponse
120121
*/
121122
public function resolveByIdentifierValue(RemoteType $remoteType, string $identifierValue): JsonResponse
122123
{
123-
$data = $remoteType->getRemoteData();
124+
$data = $remoteType->getRemoteData($identifierValue);
124125

125126
$data = $remoteType->data_path ? Arr::get($data, $remoteType->data_path) : $data;
126127

@@ -130,4 +131,20 @@ public function resolveByIdentifierValue(RemoteType $remoteType, string $identif
130131

131132
return response()->json($transformed);
132133
}
134+
135+
public function search(Request $request, RemoteType $remoteType, string $q = ''): JsonResponse
136+
{
137+
// check query parameter
138+
$q = $q ?: $request->input('q');
139+
if (!empty($q)) {
140+
$data = $remoteType->searchRemoteData($q);
141+
} else {
142+
$data = $remoteType->getRemoteData();
143+
}
144+
145+
$data = $remoteType->data_path ? Arr::get($data, $remoteType->data_path) : $data;
146+
$transformed = $this->transform($data, $remoteType->mappings);
147+
148+
return response()->json($transformed);
149+
}
133150
}

src/App/Models/CustomField.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
* @method static Builder remote()
3030
* @method static Builder selection()
3131
*
32+
* @property string $id
33+
* @property string $name
34+
* @property string $label
35+
* @property string $placeholder
36+
* @property string $model
37+
* @property bool $required
38+
* @property bool $hidden
39+
* @property bool $is_searchable
40+
* @property string $group
41+
* @property int $order
42+
* @property string $renderer
43+
*
3244
* Class CustomField
3345
*/
3446
class CustomField extends Model implements CustomFieldContract

src/App/Models/RemoteType.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class RemoteType extends ParentType implements \Asseco\CustomFields\App\Contract
2929
'mappings' => 'array',
3030
];
3131

32+
const DEFAULT_IDENTIFIER_PROPERTY = 'id';
33+
const DEFAULT_SEARCH_QUERY_PARAMETER = 'q';
34+
3235
protected static function newFactory()
3336
{
3437
return RemoteTypeFactory::new();
@@ -44,23 +47,55 @@ public function getNameAttribute()
4447
return 'remote';
4548
}
4649

47-
public function getRemoteData()
50+
private function fetchData(?string $value = null, bool $search = false)
4851
{
49-
$cacheKey = 'remote_custom_field_' . $this->id;
52+
$qParam = $this->identifier_property ?: self::DEFAULT_IDENTIFIER_PROPERTY;
53+
if ($search) {
54+
$qParam = self::DEFAULT_SEARCH_QUERY_PARAMETER;
55+
}
56+
57+
$body = $this->body;
58+
$url = $this->url;
59+
60+
if ($value) {
61+
// get by ID
62+
if ($this->method == 'POST') {
63+
empty($body) ? ($body = [$qParam => $value]) : ($body[$qParam] = $value);
64+
} else {
65+
$parsed = parse_url($url);
66+
parse_str($parsed['query'] ?? '', $params);
67+
$params[$qParam] = $value;
68+
$url = $parsed['scheme'] . '://' . $parsed['host'];
69+
if (!empty($parsed['port'])) {
70+
$url .= ':' . $parsed['port'];
71+
}
72+
$url .= $parsed['path'] . '?' . http_build_query($params);
73+
}
74+
}
75+
76+
return Http::withHeaders($this->getHeaders() ?: [])
77+
->withBody($body, 'application/json')
78+
->{$this->method}($url)->throw()->json();
79+
}
5080

81+
public function getRemoteData(?string $identifierValue = null)
82+
{
83+
$cacheKey = 'remote_custom_field_' . $this->id;
5184
if (config('asseco-custom-fields.should_cache_remote') && Cache::has($cacheKey)) {
5285
return Cache::get($cacheKey);
5386
}
5487

55-
$response = Http::withHeaders($this->getHeaders() ?: [])
56-
->withBody($this->body, 'application/json')
57-
->{$this->method}($this->url)->throw()->json();
58-
88+
$response = $this->fetchData($identifierValue, false);
5989
Cache::put($cacheKey, $response, config('asseco-custom-fields.remote_cache_ttl'));
6090

6191
return $response;
6292
}
6393

94+
public function searchRemoteData(string $searchString)
95+
{
96+
return $this->fetchData($searchString, true);
97+
}
98+
6499
protected function getHeaders()
65100
{
66101
return $this->headers;

src/App/Traits/TransformsOutput.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ protected function transform(array $response, ?array $mappings): array
2828
protected function mapSingle(array $mappings, array $item): array
2929
{
3030
$data = [];
31-
3231
foreach ($mappings as $remoteKey => $localKey) {
3332
if (!array_key_exists($remoteKey, $item)) {
3433
continue;

0 commit comments

Comments
 (0)