mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-11-03 20:55:05 +00:00 
			
		
		
		
	Add currency limit, without currency code #3150
This commit is contained in:
		@@ -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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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']);
 | 
			
		||||
    }
 | 
			
		||||
);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user