fix: Fix search endpoints

This commit is contained in:
Mangiang 2021-10-11 21:11:22 -04:00
parent aae9cbac94
commit 4a2404d365
2 changed files with 17 additions and 11 deletions

View File

@ -44,8 +44,8 @@ class ItemsCollection
if (empty($current_locale_name) || !$current_locale_name[1][0]) continue;
$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']]);
$this->locales = $this->locales->merge($test);
$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($current_locale);
}
Cache::put($this->locales_cache_key, $this->locales);
}
@ -64,6 +64,16 @@ class ItemsCollection
$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 $locale the chosen local. Default to 'en'
@ -72,14 +82,10 @@ class ItemsCollection
public function findItem(string $query, string $locale = 'en'): Collection
{
return $this->items->filter(function ($val, $key) use ($query, $locale) {
$query = Str::lower($query);
return Str::contains($val['_id'], $query)
|| Str::contains($val['_name'], $query)
|| Str::contains($val['_parent'], $query)
|| (($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));
return $this->contains($val['_id'], $query)
|| $this->contains($val['_name'], $query)
|| $this->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);
})->map(function ($item) use ($locale) {
return [
'_id' => $item['_id'],

View File

@ -22,5 +22,5 @@ $router->get('/api/refresh', 'ItemController@refreshAllCache');
$router->get('/api/item/{id}/{locale}', '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');