Files
firefly-iii/app/Api/V1/Controllers/Models/Transaction/ListController.php
2021-02-16 09:52:39 +01:00

135 lines
4.6 KiB
PHP

<?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/>.
*/
namespace FireflyIII\Api\V1\Controllers\Models\Transaction;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalAPIRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Transformers\AttachmentTransformer;
use FireflyIII\Transformers\PiggyBankEventTransformer;
use FireflyIII\Transformers\TransactionLinkTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
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);
}
);
}
/**
* @param TransactionGroup $transactionGroup
*
* @return JsonResponse
* @codeCoverageIgnore
*/
public function attachments(TransactionGroup $transactionGroup): JsonResponse
{
$manager = $this->getManager();
$attachments = new Collection;
foreach ($transactionGroup->transactionJournals as $transactionJournal) {
$attachments = $this->journalAPIRepository->getAttachments($transactionJournal)->merge($attachments);
}
/** @var AttachmentTransformer $transformer */
$transformer = app(AttachmentTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($attachments, $transformer, 'attachments');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
* @param TransactionGroup $transactionGroup
*
* @return JsonResponse
* @codeCoverageIgnore
*/
public function piggyBankEvents(TransactionGroup $transactionGroup): JsonResponse
{
$manager = $this->getManager();
$events = new Collection;
foreach ($transactionGroup->transactionJournals as $transactionJournal) {
$events = $this->journalAPIRepository->getPiggyBankEvents($transactionJournal)->merge($events);
}
/** @var PiggyBankEventTransformer $transformer */
$transformer = app(PiggyBankEventTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($events, $transformer, 'piggy_bank_events');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
/**
* @param TransactionJournal $transactionJournal
*
* @return JsonResponse
* @codeCoverageIgnore
*/
public function transactionLinks(TransactionJournal $transactionJournal): JsonResponse
{
$manager = $this->getManager();
$journalLinks = $this->journalAPIRepository->getJournalLinks($transactionJournal);
/** @var TransactionLinkTransformer $transformer */
$transformer = app(TransactionLinkTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($journalLinks, $transformer, 'transaction_links');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
}