firefly-iii/app/Api/V1/Controllers/TransactionLinkController.php

214 lines
7.3 KiB
PHP
Raw Normal View History

2018-06-28 10:02:13 -05:00
<?php
/**
2018-12-08 00:57:29 -06:00
* TransactionLinkController.php
2018-06-28 10:02:13 -05:00
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Api\V1\Controllers;
use FireflyIII\Api\V1\Requests\TransactionLinkRequest;
2018-06-28 10:02:13 -05:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
2018-12-08 14:26:20 -06:00
use FireflyIII\Support\Http\Api\TransactionFilter;
2018-12-15 15:03:05 -06:00
use FireflyIII\Transformers\TransactionLinkTransformer;
2018-06-28 10:02:13 -05:00
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;
2018-07-05 11:02:02 -05:00
/**
2018-12-08 00:57:29 -06:00
* Class TransactionLinkController
2018-07-05 11:02:02 -05:00
*/
2018-12-08 00:57:29 -06:00
class TransactionLinkController extends Controller
2018-06-28 10:02:13 -05:00
{
2018-12-08 14:26:20 -06:00
use TransactionFilter;
2018-12-08 00:57:29 -06:00
/** @var JournalRepositoryInterface The journal repository */
2018-06-28 10:02:13 -05:00
private $journalRepository;
/** @var LinkTypeRepositoryInterface The link type repository */
2018-06-28 10:02:13 -05:00
private $repository;
/**
* TransactionLinkController constructor.
2019-09-04 10:39:39 -05:00
*
* @codeCoverageIgnore
*/
2018-06-28 10:02:13 -05:00
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
/** @var User $user */
$user = auth()->user();
$this->repository = app(LinkTypeRepositoryInterface::class);
$this->journalRepository = app(JournalRepositoryInterface::class);
$this->repository->setUser($user);
$this->journalRepository->setUser($user);
return $next($request);
}
);
}
/**
* Delete the resource.
*
2018-07-02 13:02:20 -05:00
* @param TransactionJournalLink $link
2018-06-28 10:02:13 -05:00
*
* @return JsonResponse
* @codeCoverageIgnore
2018-06-28 10:02:13 -05:00
*/
2018-07-02 13:02:20 -05:00
public function delete(TransactionJournalLink $link): JsonResponse
2018-06-28 10:02:13 -05:00
{
2018-07-02 13:02:20 -05:00
$this->repository->destroyLink($link);
2018-06-28 10:02:13 -05:00
return response()->json([], 204);
}
/**
* List all of them.
*
* @param Request $request
*
* @return JsonResponse
* @codeCoverageIgnore
2018-06-28 10:02:13 -05:00
*/
public function index(Request $request): JsonResponse
{
// create some objects:
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-06-28 10:02:13 -05:00
// read type from URI
2019-06-10 13:14:00 -05:00
$name = $request->get('name');
2018-06-28 10:02:13 -05:00
// types to get, page size:
$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;
$linkType = $this->repository->findByName($name);
2018-12-08 00:57:29 -06:00
// get list of transaction links. Count it and split it.
2018-06-28 10:02:13 -05:00
$collection = $this->repository->getJournalLinks($linkType);
$count = $collection->count();
$journalLinks = $collection->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize);
// make paginator:
$paginator = new LengthAwarePaginator($journalLinks, $count, $pageSize, $this->parameters->get('page'));
2018-12-08 00:57:29 -06:00
$paginator->setPath(route('api.v1.transaction_links.index') . $this->buildParams());
2018-06-28 10:02:13 -05:00
2018-12-15 15:03:05 -06:00
/** @var TransactionLinkTransformer $transformer */
$transformer = app(TransactionLinkTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new FractalCollection($journalLinks, $transformer, 'transaction_links');
2018-06-28 10:02:13 -05:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* List single resource.
*
* @param TransactionJournalLink $journalLink
*
* @return JsonResponse
* @codeCoverageIgnore
2018-06-28 10:02:13 -05:00
*/
2019-09-04 10:39:39 -05:00
public function show(TransactionJournalLink $journalLink): JsonResponse
2018-06-28 10:02:13 -05:00
{
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-12-15 15:03:05 -06:00
/** @var TransactionLinkTransformer $transformer */
$transformer = app(TransactionLinkTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($journalLink, $transformer, 'transaction_links');
2018-06-28 10:02:13 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* Store new object.
*
* @param TransactionLinkRequest $request
2018-06-28 10:02:13 -05:00
*
* @return JsonResponse
* @throws FireflyException
*/
public function store(TransactionLinkRequest $request): JsonResponse
2018-06-28 10:02:13 -05:00
{
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-06-28 10:02:13 -05:00
$data = $request->getAll();
$inward = $this->journalRepository->findNull($data['inward_id'] ?? 0);
$outward = $this->journalRepository->findNull($data['outward_id'] ?? 0);
if (null === $inward || null === $outward) {
throw new FireflyException('Source or destination is NULL.');
}
$data['direction'] = 'inward';
$journalLink = $this->repository->storeLink($data, $inward, $outward);
2018-12-15 15:03:05 -06:00
/** @var TransactionLinkTransformer $transformer */
$transformer = app(TransactionLinkTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($journalLink, $transformer, 'transaction_links');
2018-06-28 10:02:13 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
* Update object.
*
2018-12-15 15:03:05 -06:00
* @param TransactionLinkRequest $request
2018-06-28 10:02:13 -05:00
* @param TransactionJournalLink $journalLink
*
* @return JsonResponse
* @throws FireflyException
*/
public function update(TransactionLinkRequest $request, TransactionJournalLink $journalLink): JsonResponse
2018-06-28 10:02:13 -05:00
{
2019-09-04 10:39:39 -05:00
$manager = $this->getManager();
2018-06-28 10:02:13 -05:00
$data = $request->getAll();
$data['inward'] = $this->journalRepository->findNull($data['inward_id'] ?? 0);
$data['outward'] = $this->journalRepository->findNull($data['outward_id'] ?? 0);
if (null === $data['inward'] || null === $data['outward']) {
throw new FireflyException('Source or destination is NULL.');
}
$data['direction'] = 'inward';
$journalLink = $this->repository->updateLink($journalLink, $data);
2018-12-15 15:03:05 -06:00
/** @var TransactionLinkTransformer $transformer */
$transformer = app(TransactionLinkTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($journalLink, $transformer, 'transaction_links');
2018-06-28 10:02:13 -05:00
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
}