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;
|
2020-06-30 22:09:39 -07:00
|
|
|
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;
|
2020-06-30 22:09:39 -07:00
|
|
|
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:
|
2023-02-12 06:53:36 +01:00
|
|
|
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/search/searchTransactions
|
2021-09-19 17:22:16 +02:00
|
|
|
*
|
2021-09-18 10:20:19 +02:00
|
|
|
* @throws FireflyException
|
2019-10-02 18:00:01 +02:00
|
|
|
*/
|
2020-06-30 22:09:39 -07:00
|
|
|
public function search(Request $request, SearchInterface $searcher): JsonResponse
|
2019-10-02 18:00:01 +02:00
|
|
|
{
|
2024-01-01 14:41:31 +01:00
|
|
|
$manager = $this->getManager();
|
2024-12-22 08:43:12 +01:00
|
|
|
$fullQuery = (string) $request->get('query');
|
|
|
|
$page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page');
|
2024-01-01 14:41:31 +01:00
|
|
|
$pageSize = $this->parameters->get('limit');
|
2020-06-30 22:09:39 -07:00
|
|
|
$searcher->parseQuery($fullQuery);
|
|
|
|
$searcher->setPage($page);
|
2020-11-08 17:16:17 -08:00
|
|
|
$searcher->setLimit($pageSize);
|
2024-01-01 14:41:31 +01:00
|
|
|
$groups = $searcher->searchTransactions();
|
|
|
|
$parameters = ['search' => $fullQuery];
|
|
|
|
$url = route('api.v1.search.transactions').'?'.http_build_query($parameters);
|
2020-06-30 22:09:39 -07:00
|
|
|
$groups->setPath($url);
|
|
|
|
$transactions = $groups->getCollection();
|
|
|
|
|
|
|
|
/** @var TransactionGroupTransformer $transformer */
|
2024-01-01 14:41:31 +01:00
|
|
|
$transformer = app(TransactionGroupTransformer::class);
|
2020-06-30 22:09:39 -07:00
|
|
|
$transformer->setParameters($this->parameters);
|
|
|
|
|
2024-01-01 14:41:31 +01:00
|
|
|
$resource = new Collection($transactions, $transformer, 'transactions');
|
2020-06-30 22:09:39 -07:00
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($groups));
|
|
|
|
|
2024-01-01 14:41:31 +01:00
|
|
|
$array = $manager->createData($resource)->toArray();
|
2023-05-15 06:18:02 +02:00
|
|
|
|
|
|
|
return response()->json($array)->header('Content-Type', self::CONTENT_TYPE);
|
2019-10-02 18:00:01 +02:00
|
|
|
}
|
2020-03-15 08:16:16 +01:00
|
|
|
}
|