diff --git a/app/Api/V1/Controllers/Autocomplete/BillController.php b/app/Api/V1/Controllers/Autocomplete/BillController.php new file mode 100644 index 0000000000..9d383ca146 --- /dev/null +++ b/app/Api/V1/Controllers/Autocomplete/BillController.php @@ -0,0 +1,83 @@ +. + */ + +declare(strict_types=1); + +namespace FireflyIII\Api\V1\Controllers\Autocomplete; + + +use FireflyIII\Api\V1\Controllers\Controller; +use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest; +use FireflyIII\Models\Bill; +use FireflyIII\Repositories\Bill\BillRepositoryInterface; +use FireflyIII\User; +use Illuminate\Http\JsonResponse; + +/** + * Class BillController + */ +class BillController extends Controller +{ + private BillRepositoryInterface $repository; + + /** + * TransactionController constructor. + * + * @codeCoverageIgnore + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + $this->repository = app(BillRepositoryInterface::class); + $this->repository->setUser($user); + + return $next($request); + } + ); + } + + /** + * @param AutocompleteRequest $request + * + * @return JsonResponse + */ + public function bills(AutocompleteRequest $request): JsonResponse + { + $data = $request->getData(); + $result = $this->repository->searchBill($data['query']); + $filtered = $result->map( + static function (Bill $item) { + return [ + 'id' => $item->id, + 'name' => $item->name, + ]; + } + ); + + + return response()->json($filtered->toArray()); + } + +} diff --git a/routes/api.php b/routes/api.php index 447649769f..676d826fc7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -62,6 +62,7 @@ Route::group( static function () { // Auto complete routes Route::get('accounts', ['uses' => 'AccountController@accounts', 'as' => 'accounts']); + Route::get('bills', ['uses' => 'BillController@bills', 'as' => 'bills']); } ); diff --git a/routes/web.php b/routes/web.php index 553c7715ab..a3998dd8d0 100644 --- a/routes/web.php +++ b/routes/web.php @@ -603,14 +603,9 @@ Route::group( ['middleware' => 'user-full-auth', 'namespace' => 'FireflyIII\Http\Controllers', 'prefix' => 'json', 'as' => 'json.'], static function () { - // for auto complete - //Route::get('accounts', ['uses' => 'Json\AutoCompleteController@accounts', 'as' => 'autocomplete.accounts']); - Route::get('revenue-accounts', ['uses' => 'Json\AutoCompleteController@revenueAccounts', 'as' => 'autocomplete.revenue-accounts']); - Route::get('expense-accounts', ['uses' => 'Json\AutoCompleteController@expenseAccounts', 'as' => 'autocomplete.expense-accounts']); - Route::get('asset-accounts', ['uses' => 'Json\AutoCompleteController@assetAccounts', 'as' => 'autocomplete.asset-accounts']); + // for auto complete Route::get('budgets', ['uses' => 'Json\AutoCompleteController@budgets', 'as' => 'autocomplete.budgets']); Route::get('object-groups', ['uses' => 'Json\AutoCompleteController@objectGroups', 'as' => 'autocomplete.object-groups']); - Route::get('bills', ['uses' => 'Json\AutoCompleteController@bills', 'as' => 'autocomplete.bills']); Route::get('categories', ['uses' => 'Json\AutoCompleteController@categories', 'as' => 'autocomplete.categories']); Route::get('currencies', ['uses' => 'Json\AutoCompleteController@currencies', 'as' => 'autocomplete.currencies']); Route::get('piggy-banks', ['uses' => 'Json\AutoCompleteController@piggyBanks', 'as' => 'autocomplete.piggy-banks']);