Files
firefly-iii/app/Api/V1/Controllers/Models/Transaction/ListController.php

176 lines
7.3 KiB
PHP
Raw Normal View History

2021-02-16 09:52:39 +01:00
<?php
/*
* ListController.php
* Copyright (c) 2021 james@firefly-iii.org
*
* 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/>.
*/
2021-03-28 11:39:26 +02:00
declare(strict_types=1);
2021-02-16 09:52:39 +01:00
2021-03-28 11:39:26 +02:00
namespace FireflyIII\Api\V1\Controllers\Models\Transaction;
2021-02-16 09:52:39 +01:00
use FireflyIII\Api\V1\Controllers\Controller;
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException;
2021-02-16 09:52:39 +01:00
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
use FireflyIII\Transformers\AttachmentTransformer;
use FireflyIII\Transformers\PiggyBankEventTransformer;
use FireflyIII\Transformers\TransactionLinkTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
2021-03-06 20:52:42 +01:00
use Illuminate\Pagination\LengthAwarePaginator;
2021-02-16 09:52:39 +01:00
use Illuminate\Support\Collection;
2021-03-06 20:52:42 +01:00
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
2021-02-16 09:52:39 +01:00
use League\Fractal\Resource\Collection as FractalCollection;
/**
* Class ListController
*/
class ListController extends Controller
{
private JournalAPIRepositoryInterface $journalAPIRepository;
/**
* TransactionController constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $admin */
$admin = auth()->user();
$this->journalAPIRepository = app(JournalAPIRepositoryInterface::class);
$this->journalAPIRepository->setUser($admin);
return $next($request);
}
);
}
/**
2021-09-19 10:20:28 +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)#/transactions/listAttachmentByTransaction
2021-09-19 10:20:28 +02:00
*
2022-12-29 19:41:57 +01:00
* @param TransactionGroup $transactionGroup
2021-02-16 09:52:39 +01:00
*
* @return JsonResponse
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2021-02-16 09:52:39 +01:00
* @codeCoverageIgnore
*/
public function attachments(TransactionGroup $transactionGroup): JsonResponse
{
2021-03-21 09:15:40 +01:00
$manager = $this->getManager();
2022-12-29 19:41:57 +01:00
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2022-10-30 14:23:00 +01:00
$collection = new Collection();
2021-02-16 09:52:39 +01:00
foreach ($transactionGroup->transactionJournals as $transactionJournal) {
2021-03-06 20:52:42 +01:00
$collection = $this->journalAPIRepository->getAttachments($transactionJournal)->merge($collection);
2021-02-16 09:52:39 +01:00
}
2021-03-06 20:52:42 +01:00
$count = $collection->count();
$attachments = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($attachments, $count, $pageSize, $this->parameters->get('page'));
2022-12-29 19:41:57 +01:00
$paginator->setPath(route('api.v1.transactions.attachments', [$transactionGroup->id]).$this->buildParams());
2021-03-06 20:52:42 +01:00
2021-02-16 09:52:39 +01:00
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($attachments, $transformer, 'attachments');
2021-03-06 20:52:42 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2021-02-16 09:52:39 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
2021-09-19 10:20:28 +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)#/transactions/listEventByTransaction
2021-09-19 10:20:28 +02:00
*
2022-12-29 19:41:57 +01:00
* @param TransactionGroup $transactionGroup
2021-02-16 09:52:39 +01:00
*
* @return JsonResponse
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2021-02-16 09:52:39 +01:00
* @codeCoverageIgnore
*/
public function piggyBankEvents(TransactionGroup $transactionGroup): JsonResponse
{
2021-03-21 09:15:40 +01:00
$manager = $this->getManager();
2022-10-30 14:23:00 +01:00
$collection = new Collection();
2022-12-29 19:41:57 +01:00
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2021-02-16 09:52:39 +01:00
foreach ($transactionGroup->transactionJournals as $transactionJournal) {
2021-03-06 20:52:42 +01:00
$collection = $this->journalAPIRepository->getPiggyBankEvents($transactionJournal)->merge($collection);
2021-02-16 09:52:39 +01:00
}
2021-03-21 09:15:40 +01:00
$count = $collection->count();
2021-03-06 20:52:42 +01:00
$events = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($events, $count, $pageSize, $this->parameters->get('page'));
2022-12-29 19:41:57 +01:00
$paginator->setPath(route('api.v1.transactions.piggy_bank_events', [$transactionGroup->id]).$this->buildParams());
2021-02-16 09:52:39 +01:00
/** @var PiggyBankEventTransformer $transformer */
$transformer = app(PiggyBankEventTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($events, $transformer, 'piggy_bank_events');
2021-03-06 20:52:42 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2021-03-21 09:15:40 +01:00
// /** @var PiggyBankEventTransformer $transformer */
// $transformer = app(PiggyBankEventTransformer::class);
// $transformer->setParameters($this->parameters);
//
// $resource = new FractalCollection($events, $transformer, 'piggy_bank_events');
2021-02-16 09:52:39 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
2021-09-19 10:20:28 +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)#/transactions/listLinksByJournal
2021-09-19 10:20:28 +02:00
*
2022-12-29 19:41:57 +01:00
* @param TransactionJournal $transactionJournal
2021-02-16 09:52:39 +01:00
*
* @return JsonResponse
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2021-02-16 09:52:39 +01:00
* @codeCoverageIgnore
*/
public function transactionLinks(TransactionJournal $transactionJournal): JsonResponse
{
$manager = $this->getManager();
2021-03-21 09:15:40 +01:00
$collection = $this->journalAPIRepository->getJournalLinks($transactionJournal);
2022-12-29 19:41:57 +01:00
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
2021-03-21 09:15:40 +01:00
$count = $collection->count();
2021-03-06 20:52:42 +01:00
$journalLinks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($journalLinks, $count, $pageSize, $this->parameters->get('page'));
2022-12-29 19:41:57 +01:00
$paginator->setPath(route('api.v1.transaction-journals.transaction_links', [$transactionJournal->id]).$this->buildParams());
2021-02-16 09:52:39 +01:00
/** @var TransactionLinkTransformer $transformer */
$transformer = app(TransactionLinkTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($journalLinks, $transformer, 'transaction_links');
2021-03-06 20:52:42 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2021-02-16 09:52:39 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
2021-03-28 11:39:26 +02:00
}