Files
firefly-iii/app/Api/V1/Controllers/Search/TransactionController.php

77 lines
2.9 KiB
PHP
Raw Normal View History

2019-10-02 18:00:01 +02:00
<?php
2020-06-30 19:05:35 +02:00
2019-10-02 18:00:01 +02:00
/**
* TransactionController.php
2020-06-30 19:05:35 +02:00
* Copyright (c) 2020 james@firefly-iii.org
2019-10-02 18:00:01 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2019-10-02 18:00:01 +02:00
namespace FireflyIII\Api\V1\Controllers\Search;
use FireflyIII\Api\V1\Controllers\Controller;
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Search\SearchInterface;
use FireflyIII\Transformers\TransactionGroupTransformer;
use Illuminate\Http\JsonResponse;
2019-10-02 18:00:01 +02:00
use Illuminate\Http\Request;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
2019-10-02 18:00:01 +02:00
/**
* Class TransactionController
*/
class TransactionController extends Controller
{
/**
2021-09-19 17:22:16 +02:00
* This endpoint is documented at:
* https://api-docs.firefly-iii.org/#/search/searchTransactions
*
2020-07-01 19:53:48 +02:00
* @param Request $request
* @param SearchInterface $searcher
2019-10-02 18:00:01 +02:00
*
2020-07-01 19:53:48 +02:00
* @return JsonResponse
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2019-10-02 18:00:01 +02:00
*/
public function search(Request $request, SearchInterface $searcher): JsonResponse
2019-10-02 18:00:01 +02:00
{
2020-10-18 08:00:49 +02:00
$manager = $this->getManager();
2022-03-29 14:56:27 +02:00
$fullQuery = (string) $request->get('query');
$page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page');
$pageSize = (int) app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$pageSize = 0 === (int) $request->get('limit') ? $pageSize : (int) $request->get('limit');
$searcher->parseQuery($fullQuery);
$searcher->setPage($page);
$searcher->setLimit($pageSize);
$groups = $searcher->searchTransactions();
$parameters = ['search' => $fullQuery];
$url = route('api.v1.search.transactions') . '?' . http_build_query($parameters);
$groups->setPath($url);
$transactions = $groups->getCollection();
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Collection($transactions, $transformer, 'transactions');
$resource->setPaginator(new IlluminatePaginatorAdapter($groups));
2020-10-03 16:51:44 +02:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
2019-10-02 18:00:01 +02:00
}
2020-03-15 08:16:16 +01:00
}