Add currency limit, without currency code #3150

This commit is contained in:
James Cole 2020-07-21 06:29:24 +02:00
parent 74ed80aaef
commit bb841e66c7
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D
4 changed files with 45 additions and 9 deletions

View File

@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
@ -56,18 +57,50 @@ class CurrencyController extends Controller
}
/**
* TODO add limit
*
* @param AutocompleteRequest $request
*
* @return JsonResponse
*/
public function currencyWithCode(AutocompleteRequest $request): JsonResponse
public function currenciesWithCode(AutocompleteRequest $request): JsonResponse
{
$data = $request->getData();
$collection = $this->repository->searchCurrency($data['query'], $data['limit']);
$result = [];
/** @var TransactionCurrency $currency */
foreach ($collection as $currency) {
$result[] = [
'id' => $currency->id,
'name' => sprintf('%s (%s)', $currency->name, $currency->code),
'code' => $currency->code,
'symbol' => $currency->symbol,
'decimal_places' => $currency->decimal_places,
];
}
return response()->json($result);
}
/**
* @param AutocompleteRequest $request
*
* @return JsonResponse
*/
public function currencies(AutocompleteRequest $request): JsonResponse
{
$data = $request->getData();
$result = $this->repository->searchCurrency($data['query'])->toArray();
foreach ($result as $index => $item) {
$result[$index]['name'] = sprintf('%s (%s)', $item['name'], $item['code']);
$collection = $this->repository->searchCurrency($data['query'], $data['limit']);
$result = [];
/** @var TransactionCurrency $currency */
foreach ($collection as $currency) {
$result[] = [
'id' => $currency->id,
'name' => $currency->name,
'code' => $currency->code,
'symbol' => $currency->symbol,
'decimal_places' => $currency->decimal_places,
];
}
return response()->json($result);

View File

@ -476,17 +476,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search): Collection
public function searchCurrency(string $search, int $limit): Collection
{
$query = TransactionCurrency::where('enabled', 1);
if ('' !== $search) {
$query->where('name', 'LIKE', sprintf('%%%s%%', $search));
}
return $query->get();
return $query->take($limit)->get();
}
/**

View File

@ -230,10 +230,11 @@ interface CurrencyRepositoryInterface
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search): Collection;
public function searchCurrency(string $search, int $limit): Collection;
/**
* @param User $user

View File

@ -66,6 +66,7 @@ Route::group(
Route::get('budgets', ['uses' => 'BudgetController@budgets', 'as' => 'budgets']);
Route::get('categories', ['uses' => 'CategoryController@categories', 'as' => 'categories']);
Route::get('currencies', ['uses' => 'CurrencyController@currencies', 'as' => 'currencies']);
Route::get('currencies-with-code', ['uses' => 'CurrencyController@currenciesWithCode', 'as' => 'currenciesWithCode']);
Route::get('object-groups', ['uses' => 'ObjectGroupController@objectGroups', 'as' => 'object-groups']);
}
);