fix: Fix search endpoints
This commit is contained in:
parent
aae9cbac94
commit
4a2404d365
@ -44,8 +44,8 @@ class ItemsCollection
|
|||||||
if (empty($current_locale_name) || !$current_locale_name[1][0]) continue;
|
if (empty($current_locale_name) || !$current_locale_name[1][0]) continue;
|
||||||
|
|
||||||
$trimmed_current_locale_name = trim($current_locale_name[1][0]);
|
$trimmed_current_locale_name = trim($current_locale_name[1][0]);
|
||||||
$test = collect([$trimmed_current_locale_name => json_decode(file_get_contents("${raw_locales_global_base_url}/${trimmed_current_locale_name}.json"), true)['templates']]);
|
$current_locale = collect([$trimmed_current_locale_name => json_decode(file_get_contents("${raw_locales_global_base_url}/${trimmed_current_locale_name}.json"), true)['templates']]);
|
||||||
$this->locales = $this->locales->merge($test);
|
$this->locales = $this->locales->merge($current_locale);
|
||||||
}
|
}
|
||||||
Cache::put($this->locales_cache_key, $this->locales);
|
Cache::put($this->locales_cache_key, $this->locales);
|
||||||
}
|
}
|
||||||
@ -64,6 +64,16 @@ class ItemsCollection
|
|||||||
$this->refreshLocalesCache();
|
$this->refreshLocalesCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the query is in the key
|
||||||
|
*/
|
||||||
|
private function contains(string $key, string $query) : bool {
|
||||||
|
$key = Str::lower(trim($key));
|
||||||
|
$query = Str::lower(trim($query));
|
||||||
|
|
||||||
|
return Str::contains($key, $query);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $query the content of the query eg. 'AK'
|
* @param string $query the content of the query eg. 'AK'
|
||||||
* @param string $locale the chosen local. Default to 'en'
|
* @param string $locale the chosen local. Default to 'en'
|
||||||
@ -72,14 +82,10 @@ class ItemsCollection
|
|||||||
public function findItem(string $query, string $locale = 'en'): Collection
|
public function findItem(string $query, string $locale = 'en'): Collection
|
||||||
{
|
{
|
||||||
return $this->items->filter(function ($val, $key) use ($query, $locale) {
|
return $this->items->filter(function ($val, $key) use ($query, $locale) {
|
||||||
$query = Str::lower($query);
|
return $this->contains($val['_id'], $query)
|
||||||
return Str::contains($val['_id'], $query)
|
|| $this->contains($val['_name'], $query)
|
||||||
|| Str::contains($val['_name'], $query)
|
|| $this->contains($val['_parent'], $query)
|
||||||
|| Str::contains($val['_parent'], $query)
|
|| (($this->locales[$locale][$key]??false) ? ($this->contains($this->locales[$locale][$key]['Name'], $query) || $this->contains($this->locales[$locale][$key]['ShortName'], $query)) : false);
|
||||||
|| (($this->locales[$locale][$key] ?? false)
|
|
||||||
&& $this->locales[$locale][$key]['Name']
|
|
||||||
&& Str::contains(Str::lower($this->locale[$locale][$key]['Name']), $query)
|
|
||||||
&& Str::contains(Str::lower($this->locale[$locale][$key]['ShortName']), $query));
|
|
||||||
})->map(function ($item) use ($locale) {
|
})->map(function ($item) use ($locale) {
|
||||||
return [
|
return [
|
||||||
'_id' => $item['_id'],
|
'_id' => $item['_id'],
|
||||||
|
@ -22,5 +22,5 @@ $router->get('/api/refresh', 'ItemController@refreshAllCache');
|
|||||||
|
|
||||||
$router->get('/api/item/{id}/{locale}', 'ItemController@getItem');
|
$router->get('/api/item/{id}/{locale}', 'ItemController@getItem');
|
||||||
$router->get('/api/item/{id}', 'ItemController@getItem');
|
$router->get('/api/item/{id}', 'ItemController@getItem');
|
||||||
$router->post('/api/search/{locale?}', 'ItemController@search');
|
$router->post('/api/search/{locale}', 'ItemController@search');
|
||||||
$router->post('/api/search', 'ItemController@search');
|
$router->post('/api/search', 'ItemController@search');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user