Files
firefly-iii/app/Api/V1/Controllers/Models/TransactionLink/ShowController.php

124 lines
4.3 KiB
PHP
Raw Normal View History

2021-03-06 22:15:17 +01:00
<?php
2021-03-28 11:39:26 +02:00
/*
* ShowController.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/>.
*/
declare(strict_types=1);
2021-03-06 22:15:17 +01:00
namespace FireflyIII\Api\V1\Controllers\Models\TransactionLink;
use FireflyIII\Api\V1\Controllers\Controller;
2021-09-18 10:20:19 +02:00
use FireflyIII\Exceptions\FireflyException;
2021-03-06 22:15:17 +01:00
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
use FireflyIII\Transformers\TransactionLinkTransformer;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\Resource\Item;
2023-07-04 13:29:19 +02:00
/**
* Class ShowController
*/
2021-03-06 22:15:17 +01:00
class ShowController extends Controller
{
private LinkTypeRepositoryInterface $repository;
/**
* TransactionLinkController constructor.
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
2021-03-06 22:15:17 +01:00
$this->repository = app(LinkTypeRepositoryInterface::class);
$this->repository->setUser($user);
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)#/links/listTransactionLink
2021-09-19 10:20:28 +02:00
*
* List all transaction links there are.
2021-03-06 22:15:17 +01:00
*
2021-09-18 10:20:19 +02:00
* @throws FireflyException
2021-03-06 22:15:17 +01:00
*/
public function index(Request $request): JsonResponse
{
// create some objects:
$manager = $this->getManager();
2022-04-12 18:19:30 +02:00
// read type from URL
$name = $request->get('name');
2021-03-06 22:15:17 +01:00
// types to get, page size:
$pageSize = $this->parameters->get('limit');
$linkType = $this->repository->findByName($name);
2021-03-06 22:15:17 +01:00
// get list of transaction links. Count it and split it.
2022-03-29 14:56:27 +02:00
$collection = $this->repository->getJournalLinks($linkType);
$count = $collection->count();
2021-03-06 22:15:17 +01:00
$journalLinks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($journalLinks, $count, $pageSize, $this->parameters->get('page'));
2023-12-20 19:35:52 +01:00
$paginator->setPath(route('api.v1.transaction-links.index').$this->buildParams());
2021-03-06 22:15:17 +01:00
/** @var TransactionLinkTransformer $transformer */
$transformer = app(TransactionLinkTransformer::class);
2021-03-06 22:15:17 +01:00
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($journalLinks, $transformer, 'transaction_links');
2021-03-06 22:15:17 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
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)#/links/getTransactionLink
2021-09-19 10:20:28 +02:00
*
2021-03-06 22:15:17 +01:00
* List single resource.
*/
public function show(TransactionJournalLink $journalLink): JsonResponse
{
$manager = $this->getManager();
2021-03-06 22:15:17 +01:00
/** @var TransactionLinkTransformer $transformer */
$transformer = app(TransactionLinkTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($journalLink, $transformer, 'transaction_links');
2021-03-06 22:15:17 +01:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}
2021-03-28 11:39:26 +02:00
}