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

92 lines
3.1 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;
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
{
/** @var string */
2020-03-14 07:30:55 +01:00
public const SEARCH_ALL = 'all';
2019-10-02 18:00:01 +02:00
/** @var string */
2020-03-14 07:30:55 +01:00
public const SEARCH_DESCRIPTION = 'description';
2019-10-02 18:00:01 +02:00
/** @var string */
2020-03-14 07:30:55 +01:00
public const SEARCH_NOTES = 'notes';
2019-10-02 18:00:01 +02:00
/** @var string */
2020-03-14 07:30:55 +01:00
public const SEARCH_ACCOUNTS = 'accounts';
2019-10-02 18:00:01 +02:00
/** @var array */
private $validFields;
public function __construct()
{
parent::__construct();
$this->validFields = [
self::SEARCH_ALL,
self::SEARCH_DESCRIPTION,
self::SEARCH_NOTES,
self::SEARCH_ACCOUNTS,
];
}
/**
* @param Request $request
*
2020-03-14 07:30:55 +01:00
* @return void
2019-10-02 18:00:01 +02:00
*/
public function search(Request $request, SearchInterface $searcher): JsonResponse
2019-10-02 18:00:01 +02:00
{
$manager = $this->getManager();
$fullQuery = (string) $request->get('query');
$page = 0 === (int) $request->get('page') ? 1 : (int) $request->get('page');
$searcher->parseQuery($fullQuery);
$searcher->setPage($page);
$searcher->setLimit((int) config('firefly.search_result_limit'));
$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));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
2019-10-02 18:00:01 +02:00
}
2020-03-15 08:16:16 +01:00
}